pub struct Events<'a, E>(/* private fields */);Expand description
A batch of events which may be immutably accessed.
Implementations§
Source§impl<E> Events<'_, E>
impl<E> Events<'_, E>
Sourcepub fn for_each<F>(self, f: F)
pub fn for_each<F>(self, f: F)
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}"));Sourcepub fn try_for_each<F, Err>(self, f: F) -> Result<(), Err>
pub fn try_for_each<F, Err>(self, f: F) -> 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);Sourcepub fn try_commit_each<F, Err>(self, f: F) -> Result<(), Err>
pub fn try_commit_each<F, Err>(self, f: F) -> 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>
impl<'a, E> Sync for Events<'a, E>
impl<'a, E> Unpin for Events<'a, E>
impl<'a, E> !UnwindSafe for Events<'a, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more