You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've realized the design for dyn needs a little more work. There are a couple ways to handle this:
Double boxing
(This is what we do right now)
#[diplomat::opaque]structFoo(Box<dynBar>);implFoo{// double boxed!fnnew() -> Box<Self>{}// &self is a double pointerfndo_thingy(&self);}
This is inefficient, but requires no additional work. It's acceptable for prototyping but probably not for the final code.
Conversion helpers
The biggest challenge is that conversion between Box<Foo> and Box<dyn Bar> is not directly available without unsafe if struct Foo(dyn Bar), and Box<Foo> is hard to construct on its own.
What can be done is
#[diplomat::opaque]// generates `repr(transparent)`// generates `Foo::from_boxed(Box<dyn Bar>) -> Box<Foo>`// which is a simple transmutestructFoo(dynBar);implFoo{fnnew() -> Box<Foo>{// construct a Box<dyn Bar>Foo::from_boxed(bar)}fndo_thingy(&self){// use &self.0}}
We can even add additional magic where you can write
implFoo{fnnew() -> Box<dynBar>{...}}
and it will recognize the return type as compatible with Box<Foo> and pretend it is Box<Foo> over the C boundary
dyn_impl
We could directly add an impl to dyn Bar, except for the fact that we cannot impl on types not defined in this crate.
#[diplomat::opaque]typeFoo = dynBar;#[diplomat::dyn_impl]implFoo{fnnew() -> Box<Foo>;fndo_thingy(&self);}// generates a `trait DiplomatFooImpl` and `impl DiplomatFooImpl for Foo`
DynBox<T> ?
(Shadaj had an idea about this but I'm not sure if it's sound, feel free to fill in)
The text was updated successfully, but these errors were encountered:
We've realized the design for
dyn
needs a little more work. There are a couple ways to handle this:Double boxing
(This is what we do right now)
This is inefficient, but requires no additional work. It's acceptable for prototyping but probably not for the final code.
Conversion helpers
The biggest challenge is that conversion between
Box<Foo>
andBox<dyn Bar>
is not directly available withoutunsafe
ifstruct Foo(dyn Bar)
, andBox<Foo>
is hard to construct on its own.What can be done is
We can even add additional magic where you can write
and it will recognize the return type as compatible with
Box<Foo>
and pretend it isBox<Foo>
over the C boundarydyn_impl
We could directly add an impl to
dyn Bar
, except for the fact that we cannotimpl
on types not defined in this crate.DynBox<T>
?(Shadaj had an idea about this but I'm not sure if it's sound, feel free to fill in)
The text was updated successfully, but these errors were encountered: