stepancheg / grpc-rust

Rust implementation of gRPC
MIT License
1.37k stars 124 forks source link

How to use Arc<> in new_service_def() #101

Closed VenusHu closed 6 years ago

VenusHu commented 6 years ago

Hi, I'm new to rust, so I have a problem bothering me, hoping to get help. I define a struct called Peer, wrapped it using Arc, then use a new thread to start PRC server:

fn start_server(p: Arc<Peer>) {
...
server.add_service(PeerSrvServer::new_service_def(p));
...
}

then I get compile error:

the trait bound `std::sync::Arc<demo_with_grpc::peer::Peer>: demo_with_grpc::peerpb_grpc::PeerSrv` is not satisfied

So what's the correct way to write the code? Many thanks!

thedodd commented 6 years ago

You are getting that error because only your underlying Peer struct implements the interface that you are needing. Arc<Peer> is a different type altogether, and according to the compiler, does not implement that interface.

A way to work around this:

VenusHu commented 6 years ago

Solved it. The reason why I need to share the instance with other threads is that my instance needs to be both rpc client and server. I'm constructing a p2p system.

stepancheg commented 6 years ago

@thedodd thank you for help answering!

thedodd commented 6 years ago

Absolutely.