Skip to content

Commit 417658b

Browse files
author
Stjepan Glavina
authored
Merge pull request #15 from kalcutter/eventlistener-discard
EventListener: Add pub fn `discard()`
2 parents c9dba81 + 64a570b commit 417658b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,36 @@ impl EventListener {
522522
self.wait_internal(Some(deadline))
523523
}
524524

525+
/// Drop this listener and discard its notification (if any) without notifying another
526+
/// active listener.
527+
///
528+
/// Returns `true` if a notification was discarded.
529+
///
530+
/// # Examples
531+
/// ```
532+
/// use event_listener::Event;
533+
///
534+
/// let event = Event::new();
535+
/// let listener1 = event.listen();
536+
/// let listener2 = event.listen();
537+
///
538+
/// event.notify(1);
539+
///
540+
/// assert!(listener1.discard());
541+
/// assert!(!listener2.discard());
542+
/// ```
543+
pub fn discard(mut self) -> bool {
544+
// If this listener has never picked up a notification...
545+
if let Some(entry) = self.entry.take() {
546+
let mut list = self.inner.lock();
547+
// Remove the listener from the list and return `true` if it was notified.
548+
if let State::Notified(_) = list.remove(entry, self.inner.cache_ptr()) {
549+
return true;
550+
}
551+
}
552+
false
553+
}
554+
525555
/// Returns `true` if this listener listens to the given `Event`.
526556
///
527557
/// # Examples

0 commit comments

Comments
 (0)