From 5a955057e89b368330e020b54fc6f918ca60af3d Mon Sep 17 00:00:00 2001 From: Jeron Aldaron Lau Date: Wed, 25 Sep 2024 00:34:39 -0500 Subject: [PATCH] Version 0.2.2 --- Cargo.toml | 2 +- examples/ready.rs | 17 ++++++++++++ examples/repeat_with.rs | 4 ++- src/lib.rs | 12 ++------- src/ready.rs | 58 +++++++++++++++++++++++++++++++++++++++++ src/repeat.rs | 5 ++-- 6 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 examples/ready.rs create mode 100644 src/ready.rs diff --git a/Cargo.toml b/Cargo.toml index 43f2931..3f1fcee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "event_iterator" -version = "0.2.1" +version = "0.2.2" edition = "2021" license = "Apache-2.0 OR BSL-1.0 OR MIT" description = "Asynchronous lending iterator" diff --git a/examples/ready.rs b/examples/ready.rs new file mode 100644 index 0000000..d7b20f5 --- /dev/null +++ b/examples/ready.rs @@ -0,0 +1,17 @@ +use event_iterator::EventIterator; + +#[async_main::async_main] +async fn main(_spawner: async_main::LocalSpawner) { + let mut counter = 0; + let ei = event_iterator::ready(|| { + counter += 1; + "event".repeat(counter) + }) + .take(3); + + assert_eq!(ei.next_unpinned().await.as_deref(), Some("event")); + assert_eq!(ei.next_unpinned().await.as_deref(), Some("eventevent")); + assert_eq!(ei.next_unpinned().await.as_deref(), Some("eventeventevent")); + assert!(ei.next_unpinned().await.is_none()); + assert!(ei.next_unpinned().await.is_none()); +} diff --git a/examples/repeat_with.rs b/examples/repeat_with.rs index 0986695..17ed787 100644 --- a/examples/repeat_with.rs +++ b/examples/repeat_with.rs @@ -1,3 +1,5 @@ +use std::future; + use event_iterator::EventIterator; #[async_main::async_main] @@ -6,7 +8,7 @@ async fn main(_spawner: async_main::LocalSpawner) { let ei = event_iterator::repeat_with(move || { // Increment our count. This is why we started at zero. count += 1; - Box::pin(async move { count }) + future::ready(count) }); let ei = ei.take(5); diff --git a/src/lib.rs b/src/lib.rs index 5a63a53..f6116e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,7 @@ mod once; mod once_with; mod pending; mod poll_fn; +mod ready; mod repeat; mod repeat_with; mod take; @@ -81,19 +82,10 @@ pub use self::{ once_with::{once_with, OnceWith}, pending::{pending, Pending}, poll_fn::{poll_fn, PollFn}, + ready::{ready, Ready}, repeat::{repeat, Repeat}, repeat_with::{repeat_with, RepeatWith}, take::Take, take_while::TakeWhile, tear::Tear, }; - -// TODO -// -// /// Create an event iterator, endlessly repeating the same future, using the -// /// output as the event. -// pub fn repeat + Clone>(event: impl F) -// -> Repeat; -// /// Create an event iterator, endlessly repeating a closure which provides -// /// the futures, using the output as the event. -// pub fn repeat_with F>(repeater: G) -> Repeat; diff --git a/src/ready.rs b/src/ready.rs new file mode 100644 index 0000000..8fe7c1a --- /dev/null +++ b/src/ready.rs @@ -0,0 +1,58 @@ +use core::{ + cell::Cell, + fmt, + pin::Pin, + task::{Context, Poll}, +}; + +use crate::EventIterator; + +/// Event iterator that is always ready with an event +/// +/// This event iterator is created by the [`ready()`] function. See its +/// documentation for more. +pub struct Ready(Cell>); + +impl fmt::Debug for Ready { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Ready").field(&format_args!("_")).finish() + } +} + +impl EventIterator for Ready +where + F: FnMut() -> E + Unpin, +{ + type Event<'me> = E where Self: 'me; + + fn poll_next<'a>( + self: Pin<&'a Self>, + _cx: &mut Context<'_>, + ) -> Poll>> { + self.0 + .take() + .map(|mut f| { + let poll = f(); + + self.0.set(Some(f)); + Poll::Ready(Some(poll)) + }) + .unwrap() + } +} + +/// Create an event iterator that is always ready with an event. +/// +/// Polling the event iterator delegates to the wrapped function. +/// +/// # Example +/// +/// ```rust +#[doc = include_str!("../examples/ready.rs")] +/// ``` +pub fn ready(f: F) -> Ready +where + F: FnMut() -> E, +{ + Ready(Cell::new(Some(f))) +} diff --git a/src/repeat.rs b/src/repeat.rs index dcfd6de..0116483 100644 --- a/src/repeat.rs +++ b/src/repeat.rs @@ -22,7 +22,8 @@ where } impl EventIterator for Repeat -where E: Clone +where + E: Clone, { type Event<'me> = E where Self: 'me; @@ -40,8 +41,6 @@ where E: Clone /// Create an event iterator that endlessly repeats a single event. /// -/// This event iterator can be considered [fused](EventIterator::fuse). -/// /// # Example /// /// ```rust