Comments (5)
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.
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.
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.
Could you make an example of what make_big_thing
does that can't be expressed with my code sample?
from copyless.
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)
- VecAllocation should have an impl Drop HOT 1
- box! macro? HOT 3
- published version on crates.io has executable attribute on all files HOT 2
- Does not work with nested arrays HOT 1
- More pemissive license? HOT 5
- Change CI to github actions
- Question: Why mem::replace? HOT 7
- Are the `memcpy`s still done with Rust 2018/2021 without using this crate? HOT 1
- Why is this crate deprecated? HOT 1
- SmallVec support
- ArrayVec support
- Memory issue with zero-sized types HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from copyless.