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:

  1. size_of::<box<known>>() = size_of::<box<unknown>>() = size_of::<box<u32>>(); should thin pointer.
  2. dropping box<unknown> drops content
  3. cloning box<unknown> (assuming clonable s), clones content
  4. 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

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -