ihciah / rust2go

Call Golang from Rust
https://en.ihcblog.com/rust2go/
Other
48 stars 10 forks source link

is it possible to return handlers to complex go structs #7

Open Licenser opened 1 month ago

Licenser commented 1 month ago

Hi,

I am wondering if and how it is possible to return complex structures (or points to them more likely) as opaque types from calls.

Say I have a client implemented in go and want to initialize it and then return a handler to the client.

I basically want to do something like this:


struct FancyGoClient {
  client: NonNullPtr<()> // or some other struct
}

impl FancyGoClient {
  fn new(url: &str) -> Self {
    Self{ 
      client: call_to_go_init(url)
    }
  }
  fn do_the_thing(&self) -> u64 {
    call_to_go_do_the_thing(self.client)
  }
}
ihciah commented 1 month ago

In the long run, I plan to split up a bunch of coupled components in this repository, including exposing different ABI interfaces for selection, using interfaces for structure conversion and allowing users to implement them manually, etc. In the short term, to solve your needs, you can manually implement a wrapper on the golang side to convert the structure again.

Licenser commented 1 month ago

Manuall is totally fine, the problem I ran into is that in the rust-side of things I couldn't express "this is a pointer to unknown" as any approach I tried told me parameterized type are a no-no