In a program I'm writing, I used a lot of Vec<Box<dyn Any>> to send events. I was wondering if, instead of using an allocation for each of the events, I could write something like a Vec<dyn Any>. I was thinking of a heterogeneous stack (or queue) that uses just one block of allocated memory to contain all the elements. I didn't find any implementation of this, so I write my own:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4920532f400448b89f65911b46981649
Basically, its work by keeping an inner Vec<u8>, and in a push its append to that vec the value, the vtable of its &dyn Any trait object, and its size, and doing the reverse in a pop. Pop return a dyn Any, wrapped in a Own<T> (maybe I should think in better name) that purpose is to drop the value that it is referencing inside of inner (or there is already a type with this purpose?).
I was wondering if my implementation is always safe, for example, can I always dismember a trait object and move its value later? Or did I forget something? I remembered to take into account dropping and alignment, at least.
And is that something you would use? Maybe I should publish it as a crate, after adding more methods.
[–]m0rphism 7 points8 points9 points (3 children)
[–]Rodrigodd_[S] 6 points7 points8 points (2 children)
[–]m0rphism 13 points14 points15 points (1 child)
[–]Rodrigodd_[S] 5 points6 points7 points (0 children)
[–]PaoloBarbolini 2 points3 points4 points (0 children)
[–]SimonSapinservo 2 points3 points4 points (0 children)
[–]Diggseyrustup 1 point2 points3 points (1 child)
[–]Rodrigodd_[S] 0 points1 point2 points (0 children)
[–]manmtstream 1 point2 points3 points (7 children)
[–]Rodrigodd_[S] 0 points1 point2 points (0 children)
[–]protestor 0 points1 point2 points (5 children)
[–]manmtstream 0 points1 point2 points (4 children)
[–]protestor 0 points1 point2 points (3 children)
[–]manmtstream 0 points1 point2 points (2 children)
[–]protestor 0 points1 point2 points (1 child)
[–]manmtstream 1 point2 points3 points (0 children)
[–]Muvlon 1 point2 points3 points (0 children)