Skip to main content

Repeater

Struct Repeater 

Source
pub struct Repeater<E>
where E: RepeaterEntry + Clone + Unpin + Send,
{ /* private fields */ }
Expand description

Repeats structs after user defined Duration.

Implementations§

Source§

impl<E> Repeater<E>
where E: RepeaterEntry + Clone + Unpin + Send + Sync,

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new repeater with given capacity

Examples found in repository?
examples/simple.rs (line 36)
32async fn main() {
33    let callback = |entry, _| async move { println!("Popped {entry:?}") };
34
35    // create and start the repeater
36    let handle = async_repeater::Repeater::with_capacity(25).run_with_async_callback(callback);
37
38    // create entries with the same interval
39    let dur = Duration::from_secs(5);
40    let repetition = vec![Entry::new(0, dur), Entry::new(1, dur), Entry::new(2, dur)];
41
42    // insert the entries into the repeater with 1 second delay in between
43    for repetition in repetition {
44        handle.insert(repetition).await.unwrap();
45        tokio::time::sleep(Duration::from_secs(1)).await;
46    }
47
48    // wait for a ctrl-c.
49    // If this wasn't there, the program would exit immidiatly without running
50    // a single repetition
51    tokio::signal::ctrl_c().await.unwrap();
52    handle.stop().await.unwrap();
53    println!("Stopping cycler");
54}
More examples
Hide additional examples
examples/stream_impl.rs (line 37)
35async fn main() {
36    // create but not start the repeater
37    let mut repeater = Repeater::<Entry>::with_capacity(7);
38
39    // create entries as a stream
40    let repetitions = tokio_stream::iter(vec![
41        Entry::new(0, Duration::from_millis(3000)),
42        Entry::new(1, Duration::from_millis(1000)),
43        Entry::new(2, Duration::from_millis(2000)),
44    ]);
45    tokio::pin!(repetitions);
46
47    // select either an element from the stream or
48    // an element from the repeater or
49    // from the ctrl-c signal
50    loop {
51        tokio::select! {
52            Some(item) = repeater.next() => {
53                println!("Popped: {item:?}");
54                repeater.insert(item);
55            }
56            Some(item) = repetitions.next() => {
57                repeater.insert(item);
58            }
59            _ = tokio::signal::ctrl_c() => {
60                break
61            }
62        }
63    }
64}
Source

pub fn insert(&mut self, e: E)

Insert entry into repeater

If entry with same key already exists, it will be replaced. Replacement will also reset the repetition.

Insertion respects the delay() call

Examples found in repository?
examples/stream_impl.rs (line 54)
35async fn main() {
36    // create but not start the repeater
37    let mut repeater = Repeater::<Entry>::with_capacity(7);
38
39    // create entries as a stream
40    let repetitions = tokio_stream::iter(vec![
41        Entry::new(0, Duration::from_millis(3000)),
42        Entry::new(1, Duration::from_millis(1000)),
43        Entry::new(2, Duration::from_millis(2000)),
44    ]);
45    tokio::pin!(repetitions);
46
47    // select either an element from the stream or
48    // an element from the repeater or
49    // from the ctrl-c signal
50    loop {
51        tokio::select! {
52            Some(item) = repeater.next() => {
53                println!("Popped: {item:?}");
54                repeater.insert(item);
55            }
56            Some(item) = repetitions.next() => {
57                repeater.insert(item);
58            }
59            _ = tokio::signal::ctrl_c() => {
60                break
61            }
62        }
63    }
64}
Source

pub fn remove(&mut self, key: &E::Key)

Remove entry with key from repeater

Source

pub fn clear(&mut self)

Clear the repeater. This removes all entries.

Source

pub fn len(&self) -> usize

Number of entries in the repeater

Source

pub fn is_empty(&self) -> bool

Return true if repeater is empty

Source

pub fn run_with_async_callback<F, Fut>(self, callback: F) -> RepeaterHandle<E>
where F: FnOnce(E, RepeaterHandle<E>) -> Fut + Send + 'static + Clone, Fut: Future<Output = ()> + Send + 'static,

Starts the repeater in a background task

This consumes the repeater and returns a RepeaterHandle which allows communication with the background task.

Examples found in repository?
examples/simple.rs (line 36)
32async fn main() {
33    let callback = |entry, _| async move { println!("Popped {entry:?}") };
34
35    // create and start the repeater
36    let handle = async_repeater::Repeater::with_capacity(25).run_with_async_callback(callback);
37
38    // create entries with the same interval
39    let dur = Duration::from_secs(5);
40    let repetition = vec![Entry::new(0, dur), Entry::new(1, dur), Entry::new(2, dur)];
41
42    // insert the entries into the repeater with 1 second delay in between
43    for repetition in repetition {
44        handle.insert(repetition).await.unwrap();
45        tokio::time::sleep(Duration::from_secs(1)).await;
46    }
47
48    // wait for a ctrl-c.
49    // If this wasn't there, the program would exit immidiatly without running
50    // a single repetition
51    tokio::signal::ctrl_c().await.unwrap();
52    handle.stop().await.unwrap();
53    println!("Stopping cycler");
54}

Trait Implementations§

Source§

impl<E> Stream for Repeater<E>
where E: RepeaterEntry + Clone + Unpin + Send + Sync,

Source§

type Item = E

Values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<<Self as Stream>::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

§

impl<E> Freeze for Repeater<E>

§

impl<E> !RefUnwindSafe for Repeater<E>

§

impl<E> Send for Repeater<E>

§

impl<E> Sync for Repeater<E>
where <E as RepeaterEntry>::Key: Sync, E: Sync,

§

impl<E> Unpin for Repeater<E>

§

impl<E> UnsafeUnpin for Repeater<E>

§

impl<E> !UnwindSafe for Repeater<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<St> StreamExt for St
where St: Stream + ?Sized,

Source§

fn next(&mut self) -> Next<'_, Self>
where Self: Unpin,

Consumes and returns the next value in the stream or None if the stream is finished. Read more
Source§

fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
where Self: Stream<Item = Result<T, E>> + Unpin,

Consumes and returns the next item in the stream. If an error is encountered before the next item, the error is returned instead. Read more
Source§

fn map<T, F>(self, f: F) -> Map<Self, F>
where F: FnMut(Self::Item) -> T, Self: Sized,

Maps this stream’s items to a different type, returning a new stream of the resulting type. Read more
Source§

fn map_while<T, F>(self, f: F) -> MapWhile<Self, F>
where F: FnMut(Self::Item) -> Option<T>, Self: Sized,

Map this stream’s items to a different type for as long as determined by the provided closure. A stream of the target type will be returned, which will yield elements until the closure returns None. Read more
Source§

fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
where F: FnMut(Self::Item) -> Fut, Fut: Future, Self: Sized,

Maps this stream’s items asynchronously to a different type, returning a new stream of the resulting type. Read more
Source§

fn merge<U>(self, other: U) -> Merge<Self, U>
where U: Stream<Item = Self::Item>, Self: Sized,

Combine two streams into one by interleaving the output of both as it is produced. Read more
Source§

fn filter<F>(self, f: F) -> Filter<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Filters the values produced by this stream according to the provided predicate. Read more
Source§

fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
where F: FnMut(Self::Item) -> Option<T>, Self: Sized,

Filters the values produced by this stream while simultaneously mapping them to a different type according to the provided closure. Read more
Source§

fn fuse(self) -> Fuse<Self>
where Self: Sized,

Creates a stream which ends after the first None. Read more
Source§

fn take(self, n: usize) -> Take<Self>
where Self: Sized,

Creates a new stream of at most n items of the underlying stream. Read more
Source§

fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Take elements from this stream while the provided predicate resolves to true. Read more
Source§

fn skip(self, n: usize) -> Skip<Self>
where Self: Sized,

Creates a new stream that will skip the n first items of the underlying stream. Read more
Source§

fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Skip elements from the underlying stream while the provided predicate resolves to true. Read more
Source§

fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F>
where Self: Unpin, F: FnMut(Self::Item) -> bool,

Tests if every element of the stream matches a predicate. Read more
Source§

fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F>
where Self: Unpin, F: FnMut(Self::Item) -> bool,

Tests if any element of the stream matches a predicate. Read more
Source§

fn chain<U>(self, other: U) -> Chain<Self, U>
where U: Stream<Item = Self::Item>, Self: Sized,

Combine two streams into one by first returning all values from the first stream then all values from the second stream. Read more
Source§

fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, B, F>
where Self: Sized, F: FnMut(B, Self::Item) -> B,

A combinator that applies a function to every element in a stream producing a single, final value. Read more
Source§

fn collect<T>(self) -> Collect<Self, T>
where T: FromStream<Self::Item>, Self: Sized,

Drain stream pushing all emitted values into a collection. Read more
Source§

fn peekable(self) -> Peekable<Self>
where Self: Sized,

Turns the stream into a peekable stream, whose next element can be peeked at without being consumed. Read more
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.