26
26
//! One example is a sensor that shall send an update every 100ms and the applications requires
27
27
//! the sensor data latest after 120ms. If after 120ms an update
28
28
//! is not available the application must wake up and take counter measures. If the update
29
- //! arrives already after 78ms , the timeout is reset back to 120ms.
29
+ //! arrives within the timeout , the timeout is reset back to 120ms.
30
30
//! * **Tick** - An interval after which the [`WaitSet`](crate::port::waitset::WaitSet)
31
31
//! wakes up and informs the user that the interval time has passed by providing a tick.
32
32
//! This is useful when a [`Publisher`](crate::port::publisher::Publisher) shall send an
164
164
//! // attach all listeners to the waitset
165
165
//! let guard_1 = waitset.attach_notification(&listener_1)?;
166
166
//! let guard_2 = waitset.attach_notification(&listener_2)?;
167
- //! listeners.insert(guard_1.to_attachment_id( ), &listener_1);
168
- //! listeners.insert(guard_2.to_attachment_id( ), &listener_2);
167
+ //! listeners.insert(AttachmentId::from_guard(&guard_1 ), &listener_1);
168
+ //! listeners.insert(AttachmentId::from_guard(&guard_2 ), &listener_2);
169
169
//!
170
170
//! let on_event = |attachment_id| {
171
171
//! if let Some(listener) = listeners.get(&attachment_id) {
@@ -280,6 +280,25 @@ pub struct AttachmentId<Service: crate::service::Service> {
280
280
_data : PhantomData < Service > ,
281
281
}
282
282
283
+ impl < Service : crate :: service:: Service > AttachmentId < Service > {
284
+ /// Creates an [`AttachmentId`] from a [`Guard`] that was returned via
285
+ /// [`WaitSet::attach_interval()`], [`WaitSet::attach_notification()`] or
286
+ /// [`WaitSet::attach_deadline()`].
287
+ pub fn from_guard ( guard : & Guard < Service > ) -> Self {
288
+ match & guard. guard_type {
289
+ GuardType :: Tick ( t) => AttachmentId :: tick ( guard. waitset , t. index ( ) ) ,
290
+ GuardType :: Deadline ( r, t) => AttachmentId :: deadline (
291
+ guard. waitset ,
292
+ unsafe { r. file_descriptor ( ) . native_handle ( ) } ,
293
+ t. index ( ) ,
294
+ ) ,
295
+ GuardType :: Notification ( r) => AttachmentId :: notification ( guard. waitset , unsafe {
296
+ r. file_descriptor ( ) . native_handle ( )
297
+ } ) ,
298
+ }
299
+ }
300
+ }
301
+
283
302
impl < Service : crate :: service:: Service > PartialEq for AttachmentId < Service > {
284
303
fn eq ( & self , other : & Self ) -> bool {
285
304
self . attachment_type == other. attachment_type
@@ -333,7 +352,7 @@ impl<Service: crate::service::Service> AttachmentId<Service> {
333
352
/// Returns true if an event was emitted from a notification or deadline attachment
334
353
/// corresponding to [`Guard`].
335
354
pub fn event_from ( & self , other : & Guard < Service > ) -> bool {
336
- let other_attachment = other . to_attachment_id ( ) ;
355
+ let other_attachment = AttachmentId :: from_guard ( other ) ;
337
356
if let AttachmentIdType :: Deadline ( other_waitset, other_reactor_idx, _) =
338
357
other_attachment. attachment_type
339
358
{
@@ -350,7 +369,7 @@ impl<Service: crate::service::Service> AttachmentId<Service> {
350
369
/// Returns true if the deadline for the attachment corresponding to [`Guard`] was missed.
351
370
pub fn deadline_from ( & self , other : & Guard < Service > ) -> bool {
352
371
if let AttachmentIdType :: Deadline ( ..) = self . attachment_type {
353
- self . attachment_type == other . to_attachment_id ( ) . attachment_type
372
+ self . attachment_type == AttachmentId :: from_guard ( other ) . attachment_type
354
373
} else {
355
374
false
356
375
}
@@ -379,25 +398,6 @@ where
379
398
guard_type : GuardType < ' waitset , ' attachment , Service > ,
380
399
}
381
400
382
- impl < ' waitset , ' attachment , Service : crate :: service:: Service >
383
- Guard < ' waitset , ' attachment , Service >
384
- {
385
- /// Extracts the [`AttachmentId`] from the guard.
386
- pub fn to_attachment_id ( & self ) -> AttachmentId < Service > {
387
- match & self . guard_type {
388
- GuardType :: Tick ( t) => AttachmentId :: tick ( self . waitset , t. index ( ) ) ,
389
- GuardType :: Deadline ( r, t) => AttachmentId :: deadline (
390
- self . waitset ,
391
- unsafe { r. file_descriptor ( ) . native_handle ( ) } ,
392
- t. index ( ) ,
393
- ) ,
394
- GuardType :: Notification ( r) => AttachmentId :: notification ( self . waitset , unsafe {
395
- r. file_descriptor ( ) . native_handle ( )
396
- } ) ,
397
- }
398
- }
399
- }
400
-
401
401
impl < ' waitset , ' attachment , Service : crate :: service:: Service > Drop
402
402
for Guard < ' waitset , ' attachment , Service >
403
403
{
0 commit comments