pub struct Repeater<E>{ /* private fields */ }Expand description
Repeats structs after user defined Duration.
Implementations§
Source§impl<E> Repeater<E>
impl<E> Repeater<E>
Sourcepub fn with_capacity(capacity: usize) -> Self
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
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}Sourcepub fn insert(&mut self, e: E)
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}Sourcepub fn run_with_async_callback<F, Fut>(self, callback: F) -> RepeaterHandle<E>
pub fn run_with_async_callback<F, Fut>(self, callback: F) -> RepeaterHandle<E>
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>
impl<E> Stream for Repeater<E>
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>
impl<E> Unpin for Repeater<E>
impl<E> UnsafeUnpin for Repeater<E>
impl<E> !UnwindSafe for Repeater<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
Source§impl<St> StreamExt for St
impl<St> StreamExt for St
Source§fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
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 moreSource§fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
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>
fn map<T, F>(self, f: F) -> Map<Self, F>
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>
fn map_while<T, F>(self, f: F) -> MapWhile<Self, F>
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 moreSource§fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
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>
fn merge<U>(self, other: U) -> Merge<Self, U>
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>
fn filter<F>(self, f: F) -> Filter<Self, F>
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>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
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,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
Creates a stream which ends after the first
None. Read moreSource§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
Creates a new stream of at most
n items of the underlying stream. Read moreSource§fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
Take elements from this stream while the provided predicate
resolves to
true. Read moreSource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
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 moreSource§fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
Skip elements from the underlying stream while the provided predicate
resolves to
true. Read moreSource§fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F>
fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F>
Tests if every element of the stream matches a predicate. Read more
Source§fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F>
fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F>
Tests if any element of the stream matches a predicate. Read more
Source§fn chain<U>(self, other: U) -> Chain<Self, U>
fn chain<U>(self, other: U) -> Chain<Self, U>
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>
fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, B, F>
A combinator that applies a function to every element in a stream
producing a single, final value. Read more