saschagrunert / microservice-rs

Microservice template using Rust and Cap'n Proto RPCs.
MIT License
26 stars 3 forks source link

Example with Traits #1

Open insanitybit opened 6 years ago

insanitybit commented 6 years ago

Hey, really great project - this was really helpful for me.

One thing that I think might be useful is to show how one would use Cap'n generics and where rust's traits can be used - I have a bit of trouble understanding how I would, as one example, have a client send a type T: Foo over to the Microservice interface.

If you have time I think expanding to include a few more cap'n proto features would be super helpful. As is this was still very helpful so thank you.

saschagrunert commented 6 years ago

Hey, thank you very much for the feedback! :+1:

Indeed it should be possible to use Rusts generics and traits, since the Rust capn proto compiler can generate code for this as well.

If I change the proto definition to:

interface Microservice(T) {
    hello @0 (request :T) -> (response :Text);
}

then the generated Rust code looks like this:

pub mod microservice { /* (T) */
  #![allow(unused_variables)]
  pub type HelloParams<T> = ::capnp::capability::Params<::microservice_capnp::microservice::hello_params::Owned<T>>;
  pub type HelloResults<T> = ::capnp::capability::Results<::microservice_capnp::microservice::hello_results::Owned<T>>;

  pub struct Client<T> {
    pub client: ::capnp::capability::Client,
    _phantom: ::std::marker::PhantomData<(T)>
  }
  [...]
}

So It should be possible to use it with generics.

You can feel free to submit a merge request with the basic changes and we can discuss how the implementation would change. :)

insanitybit commented 6 years ago

Cool, thanks for getting back to me! I really appreciate the explanation - to be honest, there aren't a lot of conference talks or blog posts or anything about this sort of thing so it's great to have this resource.

I may have some time this weekend to work with this and submit the merge, if not I'll probably get to it next weekend.