rust - How to create a DST type? -
dst (dynamically sized types) thing in rust now. have used them successfully, flexible last member known compiler (such [u8]).
what looking do, however, create custom dst. say, example:
struct known<s> { dropit: fn (&mut s) -> (), data: s, } struct unknown { dropit: fn (&mut ()) -> (), data: (), } with expected usage being box<known<s>> => box<unknown> => box<known<s>>, middleware need not know concrete types.
note: yes, know any, , no not interested in using it.
i open suggestions in layout of both known , unknown, however:
size_of::<box<known>>() = size_of::<box<unknown>>() = size_of::<box<u32>>(); should thin pointer.- dropping
box<unknown>drops content - cloning
box<unknown>(assuming clonables), clones content - ideally,
fn dup(u: &unknown) -> box<unknown> { box u.clone() }works
i have particular difficulties (3) , (4), solve (3) manually allocating memory (not using box, directly calling malloc) prefer providing idiomatic experience user.
i not find documentation on how inform box of right size allocate.
there 2 types of unsized objects @ present: slices ([t]), adds length member; , trait objects (trait, trait + send, &c.), adds vtable including destructor knows how large object free.
there not mechanism declaring own variety of unsized objects.
Comments
Post a Comment