From 2e3aedf93ef5f788fc43271bf78b8e35d334c263 Mon Sep 17 00:00:00 2001 From: YuanYuYuan Date: Tue, 23 Jan 2024 16:27:12 +0800 Subject: [PATCH 1/2] Align the name of zenoh-plugin-example and set the used unstable feature correctly (#657) --- Cargo.toml | 2 +- plugins/{example-plugin => zenoh-plugin-example}/Cargo.toml | 2 +- plugins/{example-plugin => zenoh-plugin-example}/README.md | 0 plugins/{example-plugin => zenoh-plugin-example}/src/lib.rs | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename plugins/{example-plugin => zenoh-plugin-example}/Cargo.toml (96%) rename plugins/{example-plugin => zenoh-plugin-example}/README.md (100%) rename plugins/{example-plugin => zenoh-plugin-example}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 0cb16194d5..def96aebac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ members = [ "io/zenoh-links/zenoh-link-ws/", "io/zenoh-links/zenoh-link-unixpipe/", "io/zenoh-transport", - "plugins/example-plugin", + "plugins/zenoh-plugin-example", "plugins/zenoh-backend-traits", "plugins/zenoh-plugin-rest", "plugins/zenoh-plugin-storage-manager", diff --git a/plugins/example-plugin/Cargo.toml b/plugins/zenoh-plugin-example/Cargo.toml similarity index 96% rename from plugins/example-plugin/Cargo.toml rename to plugins/zenoh-plugin-example/Cargo.toml index 0a315b7a35..fdf9786a48 100644 --- a/plugins/example-plugin/Cargo.toml +++ b/plugins/zenoh-plugin-example/Cargo.toml @@ -36,7 +36,7 @@ env_logger = { workspace = true } futures = { workspace = true } log = { workspace = true } serde_json = { workspace = true } -zenoh = { workspace = true } +zenoh = { workspace = true, features = ["unstable"] } zenoh-core = { workspace = true } zenoh-plugin-trait = { workspace = true } zenoh-result = { workspace = true } diff --git a/plugins/example-plugin/README.md b/plugins/zenoh-plugin-example/README.md similarity index 100% rename from plugins/example-plugin/README.md rename to plugins/zenoh-plugin-example/README.md diff --git a/plugins/example-plugin/src/lib.rs b/plugins/zenoh-plugin-example/src/lib.rs similarity index 100% rename from plugins/example-plugin/src/lib.rs rename to plugins/zenoh-plugin-example/src/lib.rs From 05c7a3ed7f4258d0d4796dd904d54122da2289ac Mon Sep 17 00:00:00 2001 From: Julien Enoch Date: Tue, 23 Jan 2024 09:30:12 +0100 Subject: [PATCH 2/2] Add a CallbackPair handler implementing Drop and IntoCallbackReceiverPair (#653) * Add a CallbackPair handler implementing Drop and IntoCallbackReceiverPair * fix typo --- zenoh/src/handlers.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/zenoh/src/handlers.rs b/zenoh/src/handlers.rs index 5efc2e4781..69828a5d7f 100644 --- a/zenoh/src/handlers.rs +++ b/zenoh/src/handlers.rs @@ -86,3 +86,41 @@ pub fn locked(fnmut: impl FnMut(T)) -> impl Fn(T) { let lock = std::sync::Mutex::new(fnmut); move |x| zlock!(lock)(x) } + +/// A handler containing 2 callback functions: +/// - `callback`: the typical callback function. `context` will be passed as its last argument. +/// - `drop`: a callback called when this handler is dropped. +/// +/// It is guaranteed that: +/// +/// - `callback` will never be called once `drop` has started. +/// - `drop` will only be called **once**, and **after every** `callback` has ended. +/// - The two previous guarantees imply that `call` and `drop` are never called concurrently. +pub struct CallbackPair +where + DropFn: FnMut() + Send + Sync + 'static, +{ + pub callback: Callback, + pub drop: DropFn, +} + +impl Drop for CallbackPair +where + DropFn: FnMut() + Send + Sync + 'static, +{ + fn drop(&mut self) { + (self.drop)() + } +} + +impl<'a, OnEvent, Event, DropFn> IntoCallbackReceiverPair<'a, Event> + for CallbackPair +where + OnEvent: Fn(Event) + Send + Sync + 'a, + DropFn: FnMut() + Send + Sync + 'static, +{ + type Receiver = (); + fn into_cb_receiver_pair(self) -> (Callback<'a, Event>, Self::Receiver) { + (Dyn::from(move |evt| (self.callback)(evt)), ()) + } +}