Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New design for EventIterator #2

Draft
wants to merge 15 commits into
base: v0
Choose a base branch
from
Draft
59 changes: 30 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ categories = ["asynchronous", "no-std", "rust-patterns"]
readme = "README.md"
rust-version = "1.70"

[dependencies.pin-project-lite]
version = "0.2"

[dev-dependencies.async_main]
version = "0.4"
features = ["pasts"]
Expand Down
6 changes: 3 additions & 3 deletions examples/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use event_iterator::{Empty, EventIterator};

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei: Empty<u32> = event_iterator::empty();
let mut ei: Empty<u32> = event_iterator::empty();

assert!(ei.next_unpinned().await.is_none());
assert!(ei.next_unpinned().await.is_none());
assert!(ei.next().await.is_none());
assert!(ei.next().await.is_none());
}
11 changes: 5 additions & 6 deletions examples/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei = event_iterator::from_iter(['a', 'b', 'c']);
let ei = ei.enumerate();
let mut ei = event_iterator::from_iter_ref(['a', 'b', 'c']).enumerate();

assert_eq!(ei.next_unpinned().await, Some((0, 'a')));
assert_eq!(ei.next_unpinned().await, Some((1, 'b')));
assert_eq!(ei.next_unpinned().await, Some((2, 'c')));
assert_eq!(ei.next_unpinned().await, None);
assert_eq!(ei.next().await, Some((0, &'a')));
assert_eq!(ei.next().await, Some((1, &'b')));
assert_eq!(ei.next().await, Some((2, &'c')));
assert_eq!(ei.next().await, None);
}
7 changes: 4 additions & 3 deletions examples/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei = event_iterator::from_iter([1, 2, 3, 4, 5]).filter(|&x| x > 1);
let mut ei =
event_iterator::from_iter_ref([1, 2, 3, 4, 5]).filter(|&x| x > 1);
let mut events = Vec::new();

while let Some(event) = ei.next_unpinned().await {
events.push(event);
while let Some(event) = ei.next().await {
events.push(event.clone());
}

println!("{events:?}");
Expand Down
23 changes: 0 additions & 23 deletions examples/filter_map.rs

This file was deleted.

24 changes: 24 additions & 0 deletions examples/filter_map_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let mut ei =
event_iterator::from_iter_ref(["1", "two", "NaN", "four", "5"])
.filter_map_ref(|&s| s.parse().ok());

assert_eq!(ei.next().await, Some(&1));
assert_eq!(ei.next().await, Some(&5));
assert_eq!(ei.next().await, None);

// Here’s the same example, but with `filter()` and `map_ref()`:

let mut ei =
event_iterator::from_iter_ref(["1", "two", "NaN", "four", "5"])
.map_ref(|&s| s.parse())
.filter(|s| s.is_ok())
.map_ref(|s| s.clone().unwrap());

assert_eq!(ei.next().await, Some(&1));
assert_eq!(ei.next().await, Some(&5));
assert_eq!(ei.next().await, None);
}
14 changes: 7 additions & 7 deletions examples/from_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ async fn main(_spawner: async_main::LocalSpawner) {
})
});

assert_eq!(ei.next_unpinned().await, Some(1));
assert_eq!(ei.next_unpinned().await, Some(2));
assert_eq!(ei.next_unpinned().await, Some(3));
assert_eq!(ei.next_unpinned().await, Some(4));
assert_eq!(ei.next_unpinned().await, Some(5));
assert_eq!(ei.next_unpinned().await, None);
assert_eq!(ei.next_unpinned().await, None);
assert_eq!(ei.next().await, Some(1));
assert_eq!(ei.next().await, Some(2));
assert_eq!(ei.next().await, Some(3));
assert_eq!(ei.next().await, Some(4));
assert_eq!(ei.next().await, Some(5));
assert_eq!(ei.next().await, None);
assert_eq!(ei.next().await, None);
}
4 changes: 2 additions & 2 deletions examples/from_iter.rs → examples/from_iter_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei = event_iterator::from_iter([1, 2, 3, 4, 5]);
let mut ei = event_iterator::from_iter_ref([1, 2, 3, 4, 5]);

while let Some(i) = ei.next_unpinned().await {
while let Some(&i) = ei.next().await {
println!("{i}");
}
}
12 changes: 6 additions & 6 deletions examples/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei = event_iterator::from_iter([1, 2, 3]).fuse();
let mut ei = event_iterator::from_iter_ref([1, 2, 3]).fuse();

assert_eq!(futures::poll!(ei.next_unpinned()), Poll::Ready(Some(1)));
assert_eq!(futures::poll!(ei.next_unpinned()), Poll::Ready(Some(2)));
assert_eq!(futures::poll!(ei.next_unpinned()), Poll::Ready(Some(3)));
assert_eq!(futures::poll!(ei.next_unpinned()), Poll::Ready(None));
assert_eq!(futures::poll!(ei.next_unpinned()), Poll::Ready(None));
assert_eq!(futures::poll!(ei.next()), Poll::Ready(Some(&1)));
assert_eq!(futures::poll!(ei.next()), Poll::Ready(Some(&2)));
assert_eq!(futures::poll!(ei.next()), Poll::Ready(Some(&3)));
assert_eq!(futures::poll!(ei.next()), Poll::Ready(None));
assert_eq!(futures::poll!(ei.next()), Poll::Ready(None));
}
4 changes: 2 additions & 2 deletions examples/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei = event_iterator::from_iter([1, 2, 3, 4, 5]).inspect(|&x| {
let mut ei = event_iterator::from_iter_ref([1, 2, 3, 4, 5]).inspect(|&x| {
let uwu = "uwu".repeat(x);

println!("{uwu}");
});

while ei.next_unpinned().await.is_some() {}
while ei.next().await.is_some() {}
}
22 changes: 18 additions & 4 deletions examples/map.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
use event_iterator::EventIterator;
use event_iterator::{EventIterator, LendAs};

#[derive(Copy, Clone)]
struct Uwu<'a>(&'a i32);

#[derive(Copy, Clone)]
struct LendAsUwu;

impl LendAs for LendAsUwu {
type From<'a> = &'a i32;
type Into<'a> = Uwu<'a>;

fn lend_as<'a>(self, from: Self::From<'a>) -> Self::Into<'a> {
Uwu(from)
}
}

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei =
event_iterator::from_iter([1, 2, 3, 4, 5]).map(|x| "uwu".repeat(x));
let mut ei = event_iterator::from_iter_ref([1, 2, 3, 4, 5]).map(LendAsUwu);

while let Some(i) = ei.next_unpinned().await {
while let Some(Uwu(i)) = ei.next().await {
println!("{i}");
}
}
13 changes: 13 additions & 0 deletions examples/map_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let mut ei = event_iterator::from_iter_ref([1, 2, 3, 4, 5])
.map_ref(|&x| "uwu".repeat(x));

while let Some(i) = ei.next().await {
let i: &String = i;

println!("{i}");
}
}
12 changes: 5 additions & 7 deletions examples/next.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::pin::pin;

use event_iterator::EventIterator;

#[async_main::async_main]
async fn main(_spawner: async_main::LocalSpawner) {
let ei = pin!(event_iterator::from_iter([3, 1, 2]));
let mut ei = event_iterator::from_iter_ref([3, 1, 2]);

assert_eq!(Some(3), ei.as_ref().next().await);
assert_eq!(Some(1), ei.as_ref().next().await);
assert_eq!(Some(2), ei.as_ref().next().await);
assert_eq!(None, ei.as_ref().next().await);
assert_eq!(Some(&3), ei.next().await);
assert_eq!(Some(&1), ei.next().await);
assert_eq!(Some(&2), ei.next().await);
assert_eq!(None, ei.next().await);
}
Loading