Skip to content

Commit

Permalink
Version 0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Sep 25, 2024
1 parent f58a3e1 commit 5a95505
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 17 additions & 0 deletions examples/ready.rs
Original file line number Diff line number Diff line change
@@ -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());
}
4 changes: 3 additions & 1 deletion examples/repeat_with.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::future;

use event_iterator::EventIterator;

#[async_main::async_main]
Expand All @@ -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);

Expand Down
12 changes: 2 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod once;
mod once_with;
mod pending;
mod poll_fn;
mod ready;
mod repeat;
mod repeat_with;
mod take;
Expand All @@ -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<E, F: Future<Output = E> + Clone>(event: impl F)
// -> Repeat<F>;
// /// Create an event iterator, endlessly repeating a closure which provides
// /// the futures, using the output as the event.
// pub fn repeat_with<F: Future, G: FnMut() -> F>(repeater: G) -> Repeat<G>;
58 changes: 58 additions & 0 deletions src/ready.rs
Original file line number Diff line number Diff line change
@@ -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<F>(Cell<Option<F>>);

impl<F> fmt::Debug for Ready<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Ready").field(&format_args!("_")).finish()
}
}

impl<F, E> EventIterator for Ready<F>
where
F: FnMut() -> E + Unpin,
{
type Event<'me> = E where Self: 'me;

fn poll_next<'a>(
self: Pin<&'a Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Self::Event<'a>>> {
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<E, F>(f: F) -> Ready<F>
where
F: FnMut() -> E,
{
Ready(Cell::new(Some(f)))
}
5 changes: 2 additions & 3 deletions src/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ where
}

impl<E> EventIterator for Repeat<E>
where E: Clone
where
E: Clone,
{
type Event<'me> = E where Self: 'me;

Expand All @@ -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
Expand Down

0 comments on commit 5a95505

Please sign in to comment.