-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f58a3e1
commit 5a95505
Showing
6 changed files
with
83 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters