Skip to content

Commit

Permalink
feat: use builder method instead of handler type for undeclaration on…
Browse files Browse the repository at this point in the history
… drop (#1377)

* refactor: add comment for `Session` and `Session::close`

* Update zenoh/src/api/session.rs

Co-authored-by: Luca Cominardi <[email protected]>

* Update zenoh/src/api/session.rs

Co-authored-by: Luca Cominardi <[email protected]>

* Update zenoh/src/api/session.rs

Co-authored-by: Luca Cominardi <[email protected]>

* fix: don't run `Session::close` example

* fix: fix `Session` example

* fix: fix `Session` doc

* feat: use builder method instead of handler type for undeclaration on drop

---------

Co-authored-by: Luca Cominardi <[email protected]>
  • Loading branch information
wyfo and Mallets authored Sep 9, 2024
1 parent 8f15a04 commit 2febb76
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
10 changes: 7 additions & 3 deletions zenoh/src/api/liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use std::{
convert::TryInto,
future::{IntoFuture, Ready},
mem::size_of,
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -172,6 +171,7 @@ impl<'a> Liveliness<'a> {
session: self.session,
key_expr: TryIntoKeyExpr::try_into(key_expr).map_err(Into::into),
handler: DefaultHandler::default(),
undeclare_on_drop: true,
}
}

Expand Down Expand Up @@ -435,6 +435,7 @@ pub struct LivelinessSubscriberBuilder<'a, 'b, Handler> {
pub session: &'a Session,
pub key_expr: ZResult<KeyExpr<'b>>,
pub handler: Handler,
pub undeclare_on_drop: bool,
}

#[zenoh_macros::unstable]
Expand Down Expand Up @@ -469,11 +470,13 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
session,
key_expr,
handler: _,
undeclare_on_drop: _,
} = self;
LivelinessSubscriberBuilder {
session,
key_expr,
handler: callback,
undeclare_on_drop: false,
}
}

Expand Down Expand Up @@ -540,11 +543,13 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
session,
key_expr,
handler: _,
undeclare_on_drop: _,
} = self;
LivelinessSubscriberBuilder {
session,
key_expr,
handler,
undeclare_on_drop: true,
}
}
}
Expand Down Expand Up @@ -581,8 +586,7 @@ where
session: self.session.downgrade(),
state: sub_state,
kind: SubscriberKind::LivelinessSubscriber,
// `size_of::<Handler::Handler>() == 0` means callback-only subscriber
undeclare_on_drop: size_of::<Handler::Handler>() > 0,
undeclare_on_drop: self.undeclare_on_drop,
},
handler,
})
Expand Down
13 changes: 11 additions & 2 deletions zenoh/src/api/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ impl<'a> Publisher<'a> {
MatchingListenerBuilder {
publisher: self,
handler: DefaultHandler::default(),
undeclare_on_drop: true,
}
}

Expand Down Expand Up @@ -639,6 +640,7 @@ impl MatchingStatus {
pub struct MatchingListenerBuilder<'a, 'b, Handler> {
pub(crate) publisher: &'a Publisher<'b>,
pub handler: Handler,
pub undeclare_on_drop: bool,
}

#[zenoh_macros::unstable]
Expand Down Expand Up @@ -675,10 +677,12 @@ impl<'a, 'b> MatchingListenerBuilder<'a, 'b, DefaultHandler> {
let MatchingListenerBuilder {
publisher,
handler: _,
undeclare_on_drop: _,
} = self;
MatchingListenerBuilder {
publisher,
handler: callback,
undeclare_on_drop: false,
}
}

Expand Down Expand Up @@ -745,8 +749,13 @@ impl<'a, 'b> MatchingListenerBuilder<'a, 'b, DefaultHandler> {
let MatchingListenerBuilder {
publisher,
handler: _,
undeclare_on_drop: _,
} = self;
MatchingListenerBuilder { publisher, handler }
MatchingListenerBuilder {
publisher,
handler,
undeclare_on_drop: true,
}
}
}

Expand Down Expand Up @@ -777,7 +786,7 @@ where
inner: MatchingListenerInner {
publisher: self.publisher.clone(),
state,
undeclare_on_drop: size_of::<Handler::Handler>() > 0,
undeclare_on_drop: self.undeclare_on_drop,
},
handler,
})
Expand Down
9 changes: 6 additions & 3 deletions zenoh/src/api/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use std::{
fmt,
future::{IntoFuture, Ready},
mem::size_of,
ops::{Deref, DerefMut},
sync::Arc,
};
Expand Down Expand Up @@ -604,6 +603,7 @@ pub struct QueryableBuilder<'a, 'b, Handler> {
pub(crate) complete: bool,
pub(crate) origin: Locality,
pub(crate) handler: Handler,
pub(crate) undeclare_on_drop: bool,
}

impl<'a, 'b> QueryableBuilder<'a, 'b, DefaultHandler> {
Expand Down Expand Up @@ -634,13 +634,15 @@ impl<'a, 'b> QueryableBuilder<'a, 'b, DefaultHandler> {
complete,
origin,
handler: _,
undeclare_on_drop: _,
} = self;
QueryableBuilder {
session,
key_expr,
complete,
origin,
handler: callback,
undeclare_on_drop: false,
}
}

Expand Down Expand Up @@ -705,13 +707,15 @@ impl<'a, 'b> QueryableBuilder<'a, 'b, DefaultHandler> {
complete,
origin,
handler: _,
undeclare_on_drop: _,
} = self;
QueryableBuilder {
session,
key_expr,
complete,
origin,
handler,
undeclare_on_drop: true,
}
}

Expand Down Expand Up @@ -927,8 +931,7 @@ where
session_id: session.zid(),
session: self.session.downgrade(),
state: qable_state,
// `size_of::<Handler::Handler>() == 0` means callback-only queryable
undeclare_on_drop: size_of::<Handler::Handler>() > 0,
undeclare_on_drop: self.undeclare_on_drop,
},
handler: receiver,
})
Expand Down
2 changes: 2 additions & 0 deletions zenoh/src/api/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ impl Session {
reliability: Reliability::DEFAULT,
origin: Locality::default(),
handler: DefaultHandler::default(),
undeclare_on_drop: true,
}
}

Expand Down Expand Up @@ -784,6 +785,7 @@ impl Session {
complete: false,
origin: Locality::default(),
handler: DefaultHandler::default(),
undeclare_on_drop: true,
}
}

Expand Down
13 changes: 10 additions & 3 deletions zenoh/src/api/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use std::{
fmt,
future::{IntoFuture, Ready},
mem::size_of,
ops::{Deref, DerefMut},
sync::Arc,
};
Expand Down Expand Up @@ -150,6 +149,11 @@ pub struct SubscriberBuilder<'a, 'b, Handler> {
pub handler: Handler,
#[cfg(not(feature = "unstable"))]
pub(crate) handler: Handler,

#[cfg(feature = "unstable")]
pub undeclare_on_drop: bool,
#[cfg(not(feature = "unstable"))]
pub(crate) undeclare_on_drop: bool,
}

impl<'a, 'b> SubscriberBuilder<'a, 'b, DefaultHandler> {
Expand Down Expand Up @@ -181,6 +185,7 @@ impl<'a, 'b> SubscriberBuilder<'a, 'b, DefaultHandler> {
reliability,
origin,
handler: _,
undeclare_on_drop: _,
} = self;
SubscriberBuilder {
session,
Expand All @@ -189,6 +194,7 @@ impl<'a, 'b> SubscriberBuilder<'a, 'b, DefaultHandler> {
reliability,
origin,
handler: callback,
undeclare_on_drop: false,
}
}

Expand Down Expand Up @@ -254,6 +260,7 @@ impl<'a, 'b> SubscriberBuilder<'a, 'b, DefaultHandler> {
reliability,
origin,
handler: _,
undeclare_on_drop: _,
} = self;
SubscriberBuilder {
session,
Expand All @@ -262,6 +269,7 @@ impl<'a, 'b> SubscriberBuilder<'a, 'b, DefaultHandler> {
reliability,
origin,
handler,
undeclare_on_drop: true,
}
}
}
Expand Down Expand Up @@ -339,8 +347,7 @@ where
session: session.downgrade(),
state: sub_state,
kind: SubscriberKind::Subscriber,
// `size_of::<Handler::Handler>() == 0` means callback-only subscriber
undeclare_on_drop: size_of::<Handler::Handler>() > 0,
undeclare_on_drop: self.undeclare_on_drop,
},
handler: receiver,
})
Expand Down

0 comments on commit 2febb76

Please sign in to comment.