Skip to content

Commit 2ecd0fa

Browse files
committed
&self -> Rc<Self> in RPC methods
1 parent 6cb826f commit 2ecd0fa

File tree

13 files changed

+121
-114
lines changed

13 files changed

+121
-114
lines changed

capnp-rpc/examples/calculator/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct PowerFunction;
2929

3030
impl calculator::function::Server for PowerFunction {
3131
async fn call(
32-
&self,
32+
self: std::rc::Rc<Self>,
3333
params: calculator::function::CallParams,
3434
mut results: calculator::function::CallResults,
3535
) -> Result<(), ::capnp::Error> {

capnp-rpc/examples/calculator/server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl ValueImpl {
4545

4646
impl calculator::value::Server for ValueImpl {
4747
async fn read(
48-
&self,
48+
self: std::rc::Rc<Self>,
4949
_params: calculator::value::ReadParams,
5050
mut results: calculator::value::ReadResults,
5151
) -> Result<(), Error> {
@@ -101,7 +101,7 @@ impl FunctionImpl {
101101

102102
impl calculator::function::Server for FunctionImpl {
103103
async fn call(
104-
&self,
104+
self: std::rc::Rc<Self>,
105105
params: calculator::function::CallParams,
106106
mut results: calculator::function::CallResults,
107107
) -> Result<(), Error> {
@@ -135,7 +135,7 @@ pub struct OperatorImpl {
135135

136136
impl calculator::function::Server for OperatorImpl {
137137
async fn call(
138-
&self,
138+
self: std::rc::Rc<Self>,
139139
params: calculator::function::CallParams,
140140
mut results: calculator::function::CallResults,
141141
) -> Result<(), Error> {
@@ -159,7 +159,7 @@ struct CalculatorImpl;
159159

160160
impl calculator::Server for CalculatorImpl {
161161
async fn evaluate(
162-
&self,
162+
self: std::rc::Rc<Self>,
163163
params: calculator::EvaluateParams,
164164
mut results: calculator::EvaluateResults,
165165
) -> Result<(), Error> {
@@ -171,7 +171,7 @@ impl calculator::Server for CalculatorImpl {
171171
}
172172

173173
async fn def_function(
174-
&self,
174+
self: std::rc::Rc<Self>,
175175
params: calculator::DefFunctionParams,
176176
mut results: calculator::DefFunctionResults,
177177
) -> Result<(), Error> {
@@ -185,7 +185,7 @@ impl calculator::Server for CalculatorImpl {
185185
}
186186

187187
async fn get_operator(
188-
&self,
188+
self: std::rc::Rc<Self>,
189189
params: calculator::GetOperatorParams,
190190
mut results: calculator::GetOperatorResults,
191191
) -> Result<(), Error> {

capnp-rpc/examples/hello-world/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct HelloWorldImpl;
3030

3131
impl hello_world::Server for HelloWorldImpl {
3232
async fn say_hello(
33-
&self,
33+
self: std::rc::Rc<Self>,
3434
params: hello_world::SayHelloParams,
3535
mut results: hello_world::SayHelloResults,
3636
) -> Result<(), ::capnp::Error> {

capnp-rpc/examples/pubsub/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct SubscriberImpl;
2828

2929
impl subscriber::Server<::capnp::text::Owned> for SubscriberImpl {
3030
async fn push_message(
31-
&self,
31+
self: std::rc::Rc<Self>,
3232
params: subscriber::PushMessageParams<::capnp::text::Owned>,
3333
_results: subscriber::PushMessageResults<::capnp::text::Owned>,
3434
) -> Result<(), ::capnp::Error> {

capnp-rpc/examples/pubsub/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl PublisherImpl {
8585

8686
impl publisher::Server<::capnp::text::Owned> for PublisherImpl {
8787
async fn subscribe(
88-
&self,
88+
self: Rc<Self>,
8989
params: publisher::SubscribeParams<::capnp::text::Owned>,
9090
mut results: publisher::SubscribeResults<::capnp::text::Owned>,
9191
) -> Result<(), ::capnp::Error> {

capnp-rpc/examples/reconnect/server.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl FooImpl {
4444
}
4545
impl foo::Server for FooImpl {
4646
async fn identity(
47-
&self,
47+
self: std::rc::Rc<Self>,
4848
params: foo::IdentityParams,
4949
mut results: foo::IdentityResults,
5050
) -> Result<(), ::capnp::Error> {
@@ -53,7 +53,11 @@ impl foo::Server for FooImpl {
5353
Ok(())
5454
}
5555

56-
async fn crash(&self, _: foo::CrashParams, _: foo::CrashResults) -> Result<(), ::capnp::Error> {
56+
async fn crash(
57+
self: std::rc::Rc<Self>,
58+
_: foo::CrashParams,
59+
_: foo::CrashResults,
60+
) -> Result<(), ::capnp::Error> {
5761
if let Some(d) = self.disconnect.borrow_mut().take() {
5862
let _ = d.send(());
5963
}

capnp-rpc/examples/streaming/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl ByteStreamImpl {
2929
}
3030

3131
impl byte_stream::Server for ByteStreamImpl {
32-
async fn write(&self, params: byte_stream::WriteParams) -> Result<(), Error> {
32+
async fn write(self: std::rc::Rc<Self>, params: byte_stream::WriteParams) -> Result<(), Error> {
3333
let bytes = params.get()?.get_bytes()?;
3434
self.hasher.borrow_mut().update(bytes);
3535
self.bytes_received
@@ -38,7 +38,7 @@ impl byte_stream::Server for ByteStreamImpl {
3838
}
3939

4040
async fn end(
41-
&self,
41+
self: std::rc::Rc<Self>,
4242
_params: byte_stream::EndParams,
4343
_results: byte_stream::EndResults,
4444
) -> Result<(), Error> {
@@ -66,7 +66,7 @@ impl ReceiverImpl {
6666

6767
impl receiver::Server for ReceiverImpl {
6868
async fn write_stream(
69-
&self,
69+
self: std::rc::Rc<Self>,
7070
_params: receiver::WriteStreamParams,
7171
mut results: receiver::WriteStreamResults,
7272
) -> Result<(), Error> {

capnp-rpc/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ where
379379
C: capnp::capability::FromServer<S>,
380380
{
381381
capnp::capability::FromClientHook::new(Box::new(local::Client::new(
382-
<C as capnp::capability::FromServer<S>>::from_server(s),
382+
<C as capnp::capability::FromServer<S>>::from_server(Rc::new(s)),
383383
)))
384384
}
385385

@@ -393,7 +393,8 @@ pub struct CapabilityServerSet<S, C>
393393
where
394394
C: capnp::capability::FromServer<S>,
395395
{
396-
caps: std::collections::HashMap<usize, Weak<C::Dispatch>>,
396+
caps: std::collections::HashMap<usize, Weak<S>>,
397+
marker: std::marker::PhantomData<C>,
397398
}
398399

399400
impl<S, C> Default for CapabilityServerSet<S, C>
@@ -403,6 +404,7 @@ where
403404
fn default() -> Self {
404405
Self {
405406
caps: std::default::Default::default(),
407+
marker: std::marker::PhantomData,
406408
}
407409
}
408410
}
@@ -417,16 +419,18 @@ where
417419

418420
/// Adds a new capability to the set and returns a client backed by it.
419421
pub fn new_client(&mut self, s: S) -> C {
420-
let dispatch = <C as capnp::capability::FromServer<S>>::from_server(s);
421-
let wrapped = Rc::new(dispatch);
422-
let ptr = Rc::as_ptr(&wrapped) as usize;
423-
self.caps.insert(ptr, Rc::downgrade(&wrapped));
424-
capnp::capability::FromClientHook::new(Box::new(local::Client::from_rc(wrapped)))
422+
let rc = Rc::new(s);
423+
let weak = Rc::downgrade(&rc);
424+
let ptr = Rc::as_ptr(&rc) as usize;
425+
self.caps.insert(ptr, weak);
426+
427+
let dispatch = <C as capnp::capability::FromServer<S>>::from_server(rc);
428+
capnp::capability::FromClientHook::new(Box::new(local::Client::new(dispatch)))
425429
}
426430

427431
/// Looks up a capability and returns its underlying server object, if found.
428432
/// Fully resolves the capability before looking it up.
429-
pub async fn get_local_server(&self, client: &C) -> Option<Rc<C::Dispatch>>
433+
pub async fn get_local_server(&self, client: &C) -> Option<Rc<S>>
430434
where
431435
C: capnp::capability::FromClientHook,
432436
{
@@ -444,7 +448,7 @@ where
444448
/// to call `get_resolved_cap()` before calling this. The advantage of this method
445449
/// over `get_local_server()` is that this one is synchronous and borrows `self`
446450
/// over a shorter span (which can be very important if `self` is inside a `RefCell`).
447-
pub fn get_local_server_of_resolved(&self, client: &C) -> Option<Rc<C::Dispatch>>
451+
pub fn get_local_server_of_resolved(&self, client: &C) -> Option<Rc<S>>
448452
where
449453
C: capnp::capability::FromClientHook,
450454
{

capnp-rpc/src/local.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ impl PipelineHook for Pipeline {
337337

338338
pub struct Client<S>
339339
where
340-
S: capability::Server,
340+
S: capability::Server + Clone,
341341
{
342-
inner: Rc<S>,
342+
inner: S,
343343

344344
/// If a streaming call on this capability has returned an error,
345345
/// this contains a copy of that error.
@@ -348,26 +348,19 @@ where
348348

349349
impl<S> Client<S>
350350
where
351-
S: capability::Server,
351+
S: capability::Server + Clone,
352352
{
353353
pub fn new(server: S) -> Self {
354354
Self {
355-
inner: Rc::new(server),
356-
broken_error: Rc::new(RefCell::new(None)),
357-
}
358-
}
359-
360-
pub fn from_rc(inner: Rc<S>) -> Self {
361-
Self {
362-
inner,
355+
inner: server,
363356
broken_error: Rc::new(RefCell::new(None)),
364357
}
365358
}
366359
}
367360

368361
impl<S> Clone for Client<S>
369362
where
370-
S: capability::Server,
363+
S: capability::Server + Clone,
371364
{
372365
fn clone(&self) -> Self {
373366
Self {
@@ -379,7 +372,7 @@ where
379372

380373
impl<S> ClientHook for Client<S>
381374
where
382-
S: capability::Server + 'static,
375+
S: capability::Server + Clone + 'static,
383376
{
384377
fn add_ref(&self) -> Box<dyn ClientHook> {
385378
Box::new(self.clone())
@@ -434,7 +427,7 @@ where
434427
}
435428

436429
fn get_ptr(&self) -> usize {
437-
Rc::as_ptr(&self.inner) as usize
430+
self.inner.as_ptr()
438431
}
439432

440433
fn get_brand(&self) -> usize {

0 commit comments

Comments
 (0)