-
Notifications
You must be signed in to change notification settings - Fork 54
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
implement z_clone generic for types that support clone. #549
Comments
from #644: In zenoh-pico *_clone methods exists by default for all owned/loaned types, in zenoh-c not, so we missed some of them
|
I'm not sure we should blindly allow clone on everything, since making for example clone of session/subscriber/publisher would likely allow to close/undeclare them from another thread resulting in unexpected behavior. |
As far as I see in rust all these types are clonable. So there is no technical obstacle to implement clone in zenoh-c. |
This is not about publisher, since it does not seem to have any mutable methods, but more about subscriber. Subscriber clone would allow to undeclare it (effect visible in all other clones) - i.e. mutate something from a const pointer in C, or const ref in C++ - which really does not coincide with overall expectation that const pointers/refs do not allow mutable access. The other question is what value such clones bring to c/c++ user, given that everything it provides for subscriber can be achieved just by using pointer/ref to it in a controlled manner (i.e. by properly respecting const semantics). |
The clones were introduced for reply/sample/query to allow moving them out of callbacks. Given that we now have proper move semantics with z_move, z_take, and that reply/sample/query are being passed by value in corresponding rust callbacks, may be it is worth considering returning them as owned pointers in the c-callbacks and get rid of questionable clones altogether ? The user might still have a choice to take or not ownership of owned object through z_move-z_take in case for example he does not want to make an extra call to z_drop. |
The goal of passing loaned value to callback was to allow user to not care about passed parameter and therefore prevent memory leaks. So current solution is: void query_handler(const z_loaned_query_t *query, void *context) { ... } If we change it to void query_handler(z_moved_query_t *query, void *context) { ... } the only way to access the query will be through If we change it to void query_handler(z_owned_query_t *query, void *context) { ... } it is not clear what to do with this query because it's against our contract. Currently we pass pointer to owned object only to constructors, to initialize them. |
So I think the approach with "clone" is ok. |
This is definitely not ok, since this breaks language invariant: the function taking const pointer/ref or const method are not supposed to cause any observable state modification of the argument. I would rather go for |
After live discussion the following decision was made:
Separate tasks are created for this, links in eclipse-zenoh/zenoh#1372 |
Describe the release item
Implement z_clone generic for types that support clone. Similarly to how it is done in pico.
The text was updated successfully, but these errors were encountered: