Struct DropEvent

Source
pub struct DropEvent(/* private fields */);
Expand description

Same as Event but notifies when dropped.

Implementations§

Source§

impl DropEvent

Source

pub fn new() -> Self

Methods from Deref<Target = Event>§

pub fn is_notified(&self) -> bool

Tell whether any listeners are currently notified.

§Examples
use event_listener::{Event, Listener};

let event = Event::new();
let listener = event.listen();
assert!(!event.is_notified());

event.notify(1);
assert!(event.is_notified());

pub fn listen(&self) -> EventListener<T>

Returns a guard listening for a notification.

This method emits a SeqCst fence after registering a listener. For now, this method is an alias for calling [EventListener::new()], pinning it to the heap, and then inserting it into a list.

§Examples
use event_listener::Event;

let event = Event::new();
let listener = event.listen();
§Caveats

The above example is equivalent to this code:

use event_listener::{Event, EventListener};

let event = Event::new();
let mut listener = Box::pin(EventListener::new());
listener.listen(&event);

It creates a new listener, pins it to the heap, and inserts it into the linked list of listeners. While this type of usage is simple, it may be desired to eliminate this heap allocation. In this case, consider using the [EventListener::new] constructor directly, which allows for greater control over where the [EventListener] is allocated. However, users of this new method must be careful to ensure that the [EventListener] is listening before waiting on it; panics may occur otherwise.

pub fn notify(&self, notify: impl IntoNotification<Tag = T>) -> usize

Notifies a number of active listeners.

The number is allowed to be zero or exceed the current number of listeners.

The [Notification] trait is used to define what kind of notification is delivered. The default implementation (implemented on usize) is a notification that only notifies at least the specified number of listeners.

In certain cases, this function emits a SeqCst fence before notifying listeners.

This function returns the number of [EventListener]s that were notified by this call.

§Caveats

If the std feature is disabled, the notification will be delayed under high contention, such as when another thread is taking a while to notify the event. In this circumstance, this function will return 0 instead of the number of listeners actually notified. Therefore if the std feature is disabled the return value of this function should not be relied upon for soundness and should be used only as a hint.

If the std feature is enabled, no spurious returns are possible, since the std implementation uses system locking primitives to ensure there is no unavoidable contention.

§Examples

Use the default notification strategy:

use event_listener::Event;

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify(1);

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify(2);

Notify without emitting a SeqCst fence. This uses the relaxed notification strategy. This is equivalent to calling [Event::notify_relaxed()].

use event_listener::{IntoNotification, Event};
use std::sync::atomic::{self, Ordering};

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify(1.relaxed());

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// We should emit a fence manually when using relaxed notifications.
atomic::fence(Ordering::SeqCst);

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify(2.relaxed());

Notify additional listeners. In contrast to [Event::notify()], this method will notify n additional listeners that were previously unnotified. This uses the additional notification strategy. This is equivalent to calling [Event::notify_additional()].

use event_listener::{IntoNotification, Event};

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify(1.additional());

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify(1.additional());
event.notify(1.additional());

Notifies with the additional and relaxed strategies at the same time. This is equivalent to calling [Event::notify_additional_relaxed()].

use event_listener::{IntoNotification, Event};
use std::sync::atomic::{self, Ordering};

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify(1.additional().relaxed());

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// We should emit a fence manually when using relaxed notifications.
atomic::fence(Ordering::SeqCst);

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify(1.additional().relaxed());
event.notify(1.additional().relaxed());

pub fn total_listeners(&self) -> usize

Get the number of listeners currently listening to this [Event].

This call returns the number of [EventListener]s that are currently listening to this event. It does this by acquiring the internal event lock and reading the listener count. Therefore it is only available for std-enabled platforms.

§Caveats

This function returns just a snapshot of the number of listeners at this point in time. Due to the nature of multi-threaded CPUs, it is possible that this number will be inaccurate by the time that this function returns.

It is possible for the actual number to change at any point. Therefore, the number should only ever be used as a hint.

§Examples
use event_listener::Event;

let event = Event::new();

assert_eq!(event.total_listeners(), 0);

let listener1 = event.listen();
assert_eq!(event.total_listeners(), 1);

let listener2 = event.listen();
assert_eq!(event.total_listeners(), 2);

drop(listener1);
drop(listener2);
assert_eq!(event.total_listeners(), 0);

pub fn notify_relaxed(&self, n: usize) -> usize

Notifies a number of active listeners without emitting a SeqCst fence.

The number is allowed to be zero or exceed the current number of listeners.

In contrast to [Event::notify_additional()], this method only makes sure at least n listeners among the active ones are notified.

Unlike [Event::notify()], this method does not emit a SeqCst fence.

This method only works for untagged events. In other cases, it is recommended to instead use [Event::notify()] like so:

use event_listener::{IntoNotification, Event};
let event = Event::new();

// Old way:
event.notify_relaxed(1);

// New way:
event.notify(1.relaxed());
§Examples
use event_listener::{Event, IntoNotification};
use std::sync::atomic::{self, Ordering};

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify_relaxed(1);

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// We should emit a fence manually when using relaxed notifications.
atomic::fence(Ordering::SeqCst);

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify_relaxed(2);

pub fn notify_additional(&self, n: usize) -> usize

Notifies a number of active and still unnotified listeners.

The number is allowed to be zero or exceed the current number of listeners.

In contrast to [Event::notify()], this method will notify n additional listeners that were previously unnotified.

This method emits a SeqCst fence before notifying listeners.

This method only works for untagged events. In other cases, it is recommended to instead use [Event::notify()] like so:

use event_listener::{IntoNotification, Event};
let event = Event::new();

// Old way:
event.notify_additional(1);

// New way:
event.notify(1.additional());
§Examples
use event_listener::Event;

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify_additional(1);

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify_additional(1);
event.notify_additional(1);

pub fn notify_additional_relaxed(&self, n: usize) -> usize

Notifies a number of active and still unnotified listeners without emitting a SeqCst fence.

The number is allowed to be zero or exceed the current number of listeners.

In contrast to [Event::notify()], this method will notify n additional listeners that were previously unnotified.

Unlike [Event::notify_additional()], this method does not emit a SeqCst fence.

This method only works for untagged events. In other cases, it is recommended to instead use [Event::notify()] like so:

use event_listener::{IntoNotification, Event};
let event = Event::new();

// Old way:
event.notify_additional_relaxed(1);

// New way:
event.notify(1.additional().relaxed());
§Examples
use event_listener::Event;
use std::sync::atomic::{self, Ordering};

let event = Event::new();

// This notification gets lost because there are no listeners.
event.notify(1);

let listener1 = event.listen();
let listener2 = event.listen();
let listener3 = event.listen();

// We should emit a fence manually when using relaxed notifications.
atomic::fence(Ordering::SeqCst);

// Notifies two listeners.
//
// Listener queueing is fair, which means `listener1` and `listener2`
// get notified here since they start listening before `listener3`.
event.notify_additional_relaxed(1);
event.notify_additional_relaxed(1);

Trait Implementations§

Source§

impl Deref for DropEvent

Source§

type Target = Event

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for DropEvent

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, D> Encode<Ambiguous1, D> for T
where D: ResourceDialect,

Source§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
Source§

impl<T, D> Encode<Ambiguous2, D> for T
where D: ResourceDialect,

Source§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V