capnproto / capnproto-rust

Cap'n Proto for Rust
MIT License
1.99k stars 220 forks source link

accepting reference instead of value #185

Open b00f opened 4 years ago

b00f commented 4 years ago

If you look at the calculator example, function client needs to be passed by value to setter methods. This causes that the object became invalid for the next use. Therefore we need to clone the object. Is it possible to pass them by reference not value?

https://github.com/capnproto/capnproto-rust/blob/8af305247921c4ede4fb21f8dfeef3f7ee6f3ba3/capnp-rpc/examples/calculator/client.rs#L189

dwrensha commented 4 years ago

The clone() call is not a deep copy -- it just increases the reference count, similar to Rc<T>.

We could make the generated set_foo() methods take their argument by reference. That might be less annoying for callers, but it would mean such methods would need to call clone() internally, even if there is no actual need to increase the reference count. So my preference is to keep things how they are.

b00f commented 4 years ago

I fully understand the difficulties that you have as an author (Rust is like a nagging wife and you can't do what ever you want!). In the other side, a user of this library may ask why should I pass this variable by value while I may need it later. Maybe you can think about the 3rd solution when you are stuck between two bad ideas.

Probably it can be linked to #179