Skip to content

Commit c9ff21c

Browse files
committed
docs: add undeclaration on drop documentation for Rust
1 parent 7d5f0de commit c9ff21c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

content/docs/migration_1.0/Rust.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,36 @@ session.close().await.unwrap();
107107
subscriber_task.await.unwrap()
108108
```
109109

110+
## Callbacks run in background until session is closed
111+
112+
Session entities, e.g. subscribers, declared with callbacks are no more undeclared when they are dropped; there is no more need to keep a variable around when the intent is to have it run until the session is closed.
113+
114+
```rust
115+
let session = zenoh::open(zenoh::config::peer()).await. unwrap();
116+
session
117+
.declare_subscriber("key/ expression")
118+
.callback(|sample| { println!("Received: {} {:?}", sample. key_expr(), sample. payload()) })
119+
.await
120+
.unwrap();
121+
// subscriber run in background until the session is closed
122+
// no need to keep a variable around
123+
```
124+
125+
If you still want the entity to be undeclared when dropped, you can simply use `with` instead of `callback`; it may just require you to annotate the callback, as type inference is not as good as with `callback` method.
126+
127+
```rust
128+
let session = zenoh::open(zenoh::config::peer()).await. unwrap();
129+
let subscriber = session
130+
.declare_subscriber("key/ expression")
131+
// annotation needed
132+
.with(|sample: Sample| { println!("Received: {} {:?}", sample. key_expr(), sample. payload()) })
133+
.await
134+
.unwrap();
135+
// subscriber is undeclared when dropped
136+
```
137+
138+
*Going into details, a new method `undeclare_on_drop(bool)` – default to `true`, has been added to the builders, and `callback(cb)` is now simply a shortcut to `with(cb).undeclare_on_drop(false)`. You should rarely need to call this method directly however.*
139+
110140
## Value is gone, long live ZBytes
111141

112142
We have replaced `Value` with `ZBytes` and `Encoding` , and added a number of conversion implementations such that user structs can be serialized into `ZBytes`, sent via Zenoh, and de-serialized from `ZBytes` with ease.

0 commit comments

Comments
 (0)