Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure out how to handle dyn ergonomically #13

Open
Manishearth opened this issue Jul 9, 2021 · 0 comments
Open

Figure out how to handle dyn ergonomically #13

Manishearth opened this issue Jul 9, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@Manishearth
Copy link
Contributor

Manishearth commented Jul 9, 2021

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]
struct Foo(Box<dyn Bar>);

impl Foo {
   // double boxed!
   fn new() -> Box<Self> {}
   // &self is a double pointer
   fn do_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 transmute
struct Foo(dyn Bar);

impl Foo {
   fn new() -> Box<Foo> {
     // construct a Box<dyn Bar>
     Foo::from_boxed(bar)
   }
   fn do_thingy(&self) {
      // use &self.0
   }
}

We can even add additional magic where you can write

impl Foo {
    fn new() -> Box<dyn Bar> {...}
}

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]
type Foo = dyn Bar;

#[diplomat::dyn_impl]
impl Foo {
   fn new() -> Box<Foo>;
   fn do_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)

@sffc sffc added the enhancement New feature or request label Aug 3, 2021
@Manishearth Manishearth added this to the Q4 milestone Sep 27, 2021
@Manishearth Manishearth self-assigned this Sep 27, 2021
@Manishearth Manishearth moved this to Todo in Manish's tracker Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants