Events

Struct Events 

Source
pub struct Events<'a, E>(/* private fields */);
Expand description

A batch of events which may be immutably accessed.

Implementations§

Source§

impl<E> Events<'_, E>

Source

pub fn size(&self) -> usize

Returns the size of this batch

Source

pub fn for_each<F>(self, f: F)
where F: FnMut(&E, i64, bool),

Process a batch of immutable events on the buffer using the closure f.

The parameters of f are:

  • event: &E, a reference to the buffer element being accessed.
  • sequence: i64, the position of this event in the sequence.
  • batch_end: bool, indicating whether this is the last event in the requested batch.
§Examples
let (mut producer, mut consumer) = ansa::spsc(64, || 0);

// move the producer so that events are available to the following consumer
producer.wait(20).for_each(|_, _, _| ());

consumer.wait(10).for_each(|event, seq, _| println!("{seq}: {event}"));
Source

pub fn try_for_each<F, Err>(self, f: F) -> Result<(), Err>
where F: FnMut(&E, i64, bool) -> Result<(), Err>,

Try to process a batch of immutable events on the buffer using the closure f.

If an error occurs, leaves the cursor sequence unchanged and returns the error.

The parameters of f are:

  • event: &E, a reference to the buffer element being accessed.
  • sequence: i64, the position of this event in the sequence.
  • batch_end: bool, indicating whether this is the last event in the requested batch.
§Examples
let (mut producer, mut consumer) = ansa::spsc(64, || 0);

// move the producer so that events are available to the following consumer
producer.wait(20).for_each(|_, _, _| ());

consumer.wait(10).try_for_each(|_, seq, _| {
    match seq {
        100 => Err(seq),
        _ => Ok(())
    }
})?;

assert_eq!(consumer.sequence(), 9);

On failure, the cursor will not be moved.

let (mut producer, mut consumer) = ansa::spsc(64, || 0);

// move the producer so that events are available to the following consumer
producer.wait(20).for_each(|_, _, _| ());

let result = consumer.wait(10).try_for_each(|_, seq, _| {
    match seq {
        5 => Err(seq),
        _ => Ok(())
    }
});

assert_eq!(result, Err(5));
// sequence values start at -1, and the first event is at sequence 0
assert_eq!(consumer.sequence(), -1);
Source

pub fn try_commit_each<F, Err>(self, f: F) -> Result<(), Err>
where F: FnMut(&E, i64, bool) -> Result<(), Err>,

Try to process a batch of immutable events on the buffer using the closure f.

If an error occurs, returns the error and updates the cursor sequence to the position of the last successfully processed event. In effect, commits the successful portion of the batch.

The parameters of f are:

  • event: &E, a reference to the buffer element being accessed.
  • sequence: i64, the position of this event in the sequence.
  • batch_end: bool, indicating whether this is the last event in the requested batch.
§Examples
let (mut producer, mut consumer) = ansa::spsc(64, || 0);

// move the producer so that events are available to the following consumer
producer.wait(20).for_each(|_, _, _| ());

consumer.wait(10).try_commit_each(|_, seq, _| {
    match seq {
        100 => Err(seq),
        _ => Ok(())
    }
})?;

assert_eq!(consumer.sequence(), 9);

On failure, the cursor will be moved to the last successful sequence.

let (mut producer, mut consumer) = ansa::spsc(64, || 0);

// move the producer so that events are available to the following consumer
producer.wait(20).for_each(|_, _, _| ());

let result = consumer.wait(10).try_commit_each(|_, seq, _| {
    match seq {
        5 => Err(seq),
        _ => Ok(())
    }
});

assert_eq!(result, Err(5));
assert_eq!(consumer.sequence(), 4);

Auto Trait Implementations§

§

impl<'a, E> Freeze for Events<'a, E>

§

impl<'a, E> !RefUnwindSafe for Events<'a, E>

§

impl<'a, E> Send for Events<'a, E>
where E: Sync + Send,

§

impl<'a, E> Sync for Events<'a, E>
where E: Sync + Send,

§

impl<'a, E> Unpin for Events<'a, E>

§

impl<'a, E> !UnwindSafe for Events<'a, E>

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> 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, 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.