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

Support access to custom types fields in a managed way in dotnet #133

Open
CBenoit opened this issue Feb 2, 2022 · 0 comments
Open

Support access to custom types fields in a managed way in dotnet #133

CBenoit opened this issue Feb 2, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@CBenoit
Copy link
Contributor

CBenoit commented Feb 2, 2022

Currently, there is no safe way to manipulate fields containing a custom non-opaque struct or opaque type inside non-opaque structs using code generated by C# / .NET backend.

I think support for this can be achieved by duplicating fields inside the managed classes (as opposed to holding the raw struct), except we swap the other raw structs and pointers by an appropriate managed object (this is similar to the C++ backend approach).

Since non-opaque structs have Copy semantic (by Diplomat's design), this is trivial to get this working for fields containing other non-opaque structs (everything is copied at FFI boundary).

Support for references to opaque types is a bit more tricky though.

First, as mentioned by @Manishearth in #130 (comment):

Basically, input types should not have:

  • Options not wrapping refs
  • Results
  • Boxes

Emphasis on Boxes. We can't copy an owning pointer that way without risking use-after-free bugs (if two managed opaque types are using the same underlying pointer Drop will run twice for the same value).

We can however support references on opaque types. This should be addressed in dotnet backend too.

Also, a distinction between owning managed classes and borrowing managed classes have to exist.
That is, for:

struct Foo(...)
struct Bar<'f>(...)

impl <'f> Bar<'f> {
  fn new(foo: &'f Foo) -> Bar<'f> {
    ...
  }
}

If Bar<'f> contains &'f Foo, the actual Foo (pointed by both owning C# Foo and borrowing C# Foo) must not be dropped when borrowing C# Foo is destructed.

@Manishearth Manishearth added the enhancement New feature or request label Feb 2, 2024
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