Skip to content

Commit

Permalink
Merge branch 'main' into simplified_load_plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jan 23, 2024
2 parents 279b3a8 + 05c7a3e commit 0b72c53
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugins/zenoh-plugin-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ futures = { workspace = true }
git-version = { 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 }
Expand Down
38 changes: 38 additions & 0 deletions zenoh/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,41 @@ pub fn locked<T>(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<Callback, DropFn>
where
DropFn: FnMut() + Send + Sync + 'static,
{
pub callback: Callback,
pub drop: DropFn,
}

impl<Callback, DropFn> Drop for CallbackPair<Callback, DropFn>
where
DropFn: FnMut() + Send + Sync + 'static,
{
fn drop(&mut self) {
(self.drop)()
}
}

impl<'a, OnEvent, Event, DropFn> IntoCallbackReceiverPair<'a, Event>
for CallbackPair<OnEvent, DropFn>
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)), ())
}
}

0 comments on commit 0b72c53

Please sign in to comment.