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

[RPC] add way to get a self-reference from within a method call #87

Open
dwrensha opened this issue Feb 22, 2018 · 1 comment · May be fixed by #456 or #473
Open

[RPC] add way to get a self-reference from within a method call #87

dwrensha opened this issue Feb 22, 2018 · 1 comment · May be fixed by #456 or #473

Comments

@dwrensha
Copy link
Member

capnproto-c++ has thisCap.

I imagine we could achieve something similar by adding a get_self() -> Client method to ParamsHook and Params.

@ranfdev
Copy link

ranfdev commented Nov 21, 2023

I spent a bit of time on this. My objective was to add a method get_self(&self) -> my_module::Client, on Params, where Client is actually a typed Client generated from the capnp tool.

Unfortunately, that solution doesn't work well with structs having generics.
Let's analyze this interface from test.capnp:

interface Interface(Qux) {
  call @0 Inner2(Text) -> (qux :Qux, gen :TestGenerics(TestAllTypes, TestAnyPointer));
  otherCall @1 Inner2(List(Text)) -> Inner2(List(Int16));
}

The generated client takes 3 generics: pub struct Client<Foo,Bar,Qux>, but the generated method parameters only take the generics they actually need, as pub type CallParams<Bar,Foo,> (notice it doesn't have Qux).

Now, to return a typed client from CallParams, I would need to associate a Client<Foo,Bar,Qux> to a CallParams<Bar,Foo>, but there's no way I can do that, because Qux is not specified. There are multiple valid Clients for a single CallParams<Bar,Foo>.

Another solution, which doesn't require adding more generics to every generated parameter, is to have a thread local variable CURRENT_THIS, and set it before every method call. Then, the capnp tool can generate a Server::get_client(&mut self) method to retrieve the content of CURRENT_THIS. Considering that the method takes &mut self, the content of CURRENT_THIS will always be correct for the lifetime of self.

@ranfdev ranfdev linked a pull request Nov 27, 2023 that will close this issue
@dwrensha dwrensha linked a pull request Jan 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants