Giter VIP home page Giter VIP logo

Comments (5)

kvark avatar kvark commented on August 25, 2024 1

It sounds like you could do this instead:

fn parse_animal_from_file(filename: String) -> Animal {
...
}
let dog_data = Box::alloc().init(parse_animal_from_file("dog.txt"));
print("{:?}", dog_data);
let mut cat_data = dog_data;
*cat_data = parse_animal_from_file("cat.txt");
print("{:?}", cat_data);

That would equally preserve the allocation, without the need to go back to BoxAllocation

from copyless.

kvark avatar kvark commented on August 25, 2024

How would this be different from just overwriting the contents of the box you have, i.e.

let box_alloc = BoxAllocation::init();
    let mut thing = make_big_thing(box_alloc);
    do_something_with_thing(&thing);

    *thing = <something>;
    do_something_with_thing(&thing);

from copyless.

yokljo avatar yokljo commented on August 25, 2024

That example code doesn't call make_big_thing twice while mine does, so it misses the point. make_big_thing takes a BoxAllocation<Thing> because it wants an uninitialised space to put an instance of Thing. If you can't turn a Box back into a BoxAllocation, you would be required to make a brand new allocation each time that make_big_thing is called.

from copyless.

kvark avatar kvark commented on August 25, 2024

Could you make an example of what make_big_thing does that can't be expressed with my code sample?

from copyless.

yokljo avatar yokljo commented on August 25, 2024

Say I have some function that parses data about an animal from a file:

struct Animal {
	age: u32,
	weight: f32,
	height: f32,
	number_of_legs: u32,
	a lot of other fields, making this a huge struct...
}

fn parse_animal_from_file(filename: String) -> Box<Animal> {
	let file = open(filename);
	let age = file.parse_age();
	let weight = file.parse_weight();
	...
	BoxAllocation<Animal>::alloc().init(Animal {age, weight, height, etc.})
}

let dog_data = parse_animal_from_file("dog.txt");
print("{:?}", dog_data);
// Note that dog_data is no longer used past this point.
let cat_data = parse_animal_from_file("cat.txt");
print("{:?}", cat_data);

Then I decide I want to be able to reuse an allocation if possible if I already have one. Why not?
So I rewrite it like so:

fn parse_animal_from_file(filename: String, box_alloc: BoxAllocation<Animal>) -> Box<Animal> {
	let file = open(filename);
	let age = file.parse_age();
	let weight = file.parse_weight();
	...
	box_alloc.init(Animal {age, weight, height, etc.})
}

let box_alloc = BoxAllocation<Thing>::alloc();
let dog_data = parse_animal_from_file("dog.txt", box_alloc);
print("{:?}", dog_data);
let box_alloc = dog_data.turn_back_into_alloc();
let cat_data = parse_animal_from_file("cat.txt", box_alloc);
print("{:?}", cat_data);

But unfortunately this doesn't work because no such turn_back_into_alloc function exists.

from copyless.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.