Skip to content

Commit

Permalink
v3.0.1
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Oct 29, 2023
1 parent 74e8231 commit 12c0008
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Version 3.0.1

- Emphasize that `listen()` must be called on `EventListener` in documentation. (#90)
- Write useful output in `fmt::Debug` implementations. (#86)

# Version 3.0.0

- Use the `parking` crate instead of threading APIs (#27)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "event-listener"
# When publishing a new version:
# - Update CHANGELOG.md
# - Create "v3.x.y" git tag
version = "3.0.0"
version = "3.0.1"
authors = ["Stjepan Glavina <[email protected]>"]
edition = "2021"
rust-version = "1.59"
Expand Down
48 changes: 28 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ impl<T> Event<T> {
/// [`EventListener`] is `listen`ing before waiting on it; panics may occur otherwise.
#[cold]
pub fn listen(&self) -> Pin<Box<EventListener<T>>> {
let mut listener = Box::pin(EventListener::new(self));
listener.as_mut().listen();
let mut listener = Box::pin(EventListener::new());
listener.as_mut().listen(self);
listener
}

Expand Down Expand Up @@ -782,17 +782,15 @@ impl<T> EventListener<T> {
/// use event_listener::{Event, EventListener};
///
/// let event = Event::new();
/// let listener = EventListener::new(&event);
/// let listener = EventListener::new();
///
/// // Make sure that the listener is pinned and listening before doing anything else.
/// let mut listener = Box::pin(listener);
/// listener.as_mut().listen();
/// listener.as_mut().listen(&event);
/// ```
pub fn new(event: &Event<T>) -> Self {
let inner = event.inner();

pub fn new() -> Self {
let listener = Listener {
event: unsafe { Arc::clone(&ManuallyDrop::new(Arc::from_raw(inner))) },
event: None,
listener: None,
};

Expand All @@ -803,8 +801,8 @@ impl<T> EventListener<T> {
///
/// This method can only be called after the listener has been pinned, and must be called before
/// the listener is polled.
pub fn listen(self: Pin<&mut Self>) {
self.listener().insert();
pub fn listen(self: Pin<&mut Self>, event: &Event<T>) {
self.listener().insert(event);

// Make sure the listener is registered before whatever happens next.
notify::full_fence();
Expand Down Expand Up @@ -936,7 +934,11 @@ impl<T> EventListener<T> {
/// ```
#[inline]
pub fn listens_to(&self, event: &Event<T>) -> bool {
ptr::eq::<Inner<T>>(&**self.inner(), event.inner.load(Ordering::Acquire))
if let Some(inner) = self.inner() {
ptr::eq::<Inner<T>>(&**inner, event.inner.load(Ordering::Acquire))
} else {
false
}
}

/// Returns `true` if both listeners listen to the same `Event`.
Expand All @@ -953,15 +955,19 @@ impl<T> EventListener<T> {
/// assert!(listener1.same_event(&listener2));
/// ```
pub fn same_event(&self, other: &EventListener<T>) -> bool {
ptr::eq::<Inner<T>>(&**self.inner(), &**other.inner())
if let (Some(this), Some(other)) = (self.inner(), other.inner()) {
return ptr::eq::<Inner<T>>(&**this, &**other);
}

false
}

fn listener(self: Pin<&mut Self>) -> Pin<&mut Listener<T, Arc<Inner<T>>>> {
self.project().listener
}

fn inner(&self) -> &Arc<Inner<T>> {
&self.listener.event
fn inner(&self) -> Option<&Arc<Inner<T>>> {
self.listener.event.as_ref()
}
}

Expand All @@ -980,7 +986,7 @@ pin_project_lite::pin_project! {
B: Unpin,
{
// The reference to the original event.
event: B,
event: Option<B>,

// The inner state of the listener.
#[pin]
Expand All @@ -994,9 +1000,10 @@ pin_project_lite::pin_project! {
fn drop(mut this: Pin<&mut Self>) {
// If we're being dropped, we need to remove ourself from the list.
let this = this.project();
let inner = (*this.event).borrow();

inner.remove(this.listener, true);
if let Some(event) = this.event.as_ref() {
let inner = (*this.event).borrow();
inner.remove(this.listener, true);
}
}
}
}
Expand All @@ -1006,11 +1013,12 @@ unsafe impl<T: Send, B: Borrow<Inner<T>> + Unpin + Sync> Sync for Listener<T, B>

impl<T, B: Borrow<Inner<T>> + Unpin> Listener<T, B> {
/// Register this listener with the event.
fn insert(self: Pin<&mut Self>) {
fn insert(self: Pin<&mut Self>, event: B) {
let this = self.project();
let inner = (*this.event).borrow();
let inner = event.borrow();

inner.insert(this.listener);
self.event = Some(event);
}

/// Wait until the provided deadline.
Expand Down

0 comments on commit 12c0008

Please sign in to comment.