Skip to content

Commit 51ae073

Browse files
vec protocol
1 parent 3b06f87 commit 51ae073

File tree

7 files changed

+206
-33
lines changed

7 files changed

+206
-33
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ jobs:
1717
rustup update
1818
- name: Build
1919
run: cargo build
20+
- name: Build for no_std
21+
run: cargo build --no-default-features
22+
- name: Build for alloc
23+
run: cargo build --no-default-features --features alloc
2024
- name: Build documentation
2125
run: |
2226
cargo rustdoc

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ edition = "2018"
66

77
[dependencies]
88
void = { version = "1.0.2", default-features = false }
9-
futures = { version = "0.3.4" }
9+
futures = { version = "0.3.4", default-features = false }
10+
11+
[features]
12+
default = ["std", "alloc"]
13+
alloc = []
14+
std = ["alloc"]

src/allocated.rs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
use crate::{ready, Coalesce, Dispatch, Fork, Future, Join, Read, Unravel, Write};
2+
use alloc::vec::{IntoIter, Vec};
3+
use core::{
4+
borrow::BorrowMut,
5+
iter::FromIterator,
6+
marker::PhantomData,
7+
mem::replace,
8+
pin::Pin,
9+
task::{Context, Poll},
10+
};
11+
12+
enum IteratorUnravelState {
13+
Writing,
14+
Flushing,
15+
Done,
16+
}
17+
18+
pub struct IteratorUnravel<
19+
T: IntoIterator,
20+
C: ?Sized + Write<Vec<<C as Dispatch<T::Item>>::Handle>> + Fork<T::Item>,
21+
> {
22+
fork: Option<C::Future>,
23+
handles: Vec<C::Handle>,
24+
state: IteratorUnravelState,
25+
data: T::IntoIter,
26+
}
27+
28+
pub struct IteratorCoalesce<
29+
T,
30+
U: FromIterator<T>,
31+
C: ?Sized + Read<Vec<<C as Dispatch<T>>::Handle>> + Join<T> + Unpin,
32+
> {
33+
handles: Option<IntoIter<C::Handle>>,
34+
join: Option<C::Future>,
35+
data: Vec<T>,
36+
ty: PhantomData<U>,
37+
}
38+
39+
#[derive(Debug)]
40+
pub enum IteratorError<T, U> {
41+
Transport(T),
42+
Dispatch(U),
43+
}
44+
45+
impl<
46+
T: IntoIterator,
47+
C: ?Sized + Write<Vec<<C as Dispatch<T::Item>>::Handle>> + Fork<T::Item> + Unpin,
48+
> Future<C> for IteratorUnravel<T, C>
49+
where
50+
T::IntoIter: Unpin,
51+
C::Handle: Unpin,
52+
C::Future: Unpin,
53+
{
54+
type Ok = ();
55+
type Error = IteratorError<C::Error, <C::Future as Future<C>>::Error>;
56+
57+
fn poll<R: BorrowMut<C>>(
58+
mut self: Pin<&mut Self>,
59+
cx: &mut Context,
60+
mut ctx: R,
61+
) -> Poll<Result<Self::Ok, Self::Error>> {
62+
let ctx = ctx.borrow_mut();
63+
64+
let this = &mut *self;
65+
66+
loop {
67+
if let Some(future) = this.fork.as_mut() {
68+
let handle = ready!(Pin::new(future).poll(cx, &mut *ctx))
69+
.map_err(IteratorError::Dispatch)?;
70+
this.handles.push(handle);
71+
this.fork.take();
72+
} else if let Some(item) = this.data.next() {
73+
this.fork = Some(ctx.fork(item));
74+
} else {
75+
let mut ctx = Pin::new(&mut *ctx);
76+
match this.state {
77+
IteratorUnravelState::Writing => {
78+
ready!(ctx.as_mut().poll_ready(cx)).map_err(IteratorError::Transport)?;
79+
ctx.write(replace(&mut this.handles, Vec::new()))
80+
.map_err(IteratorError::Transport)?;
81+
this.state = IteratorUnravelState::Flushing;
82+
}
83+
IteratorUnravelState::Flushing => {
84+
ready!(ctx.as_mut().poll_flush(cx)).map_err(IteratorError::Transport)?;
85+
this.state = IteratorUnravelState::Done;
86+
}
87+
IteratorUnravelState::Done => panic!("IteratorUnravel polled after completion"),
88+
}
89+
}
90+
}
91+
}
92+
}
93+
94+
impl<
95+
T,
96+
U: FromIterator<T>,
97+
C: ?Sized + Read<Vec<<C as Dispatch<T>>::Handle>> + Join<T> + Unpin,
98+
> Future<C> for IteratorCoalesce<T, U, C>
99+
where
100+
C::Future: Unpin,
101+
C::Handle: Unpin,
102+
U: Unpin,
103+
T: Unpin,
104+
{
105+
type Ok = U;
106+
type Error = IteratorError<C::Error, <C::Future as Future<C>>::Error>;
107+
108+
fn poll<R: BorrowMut<C>>(
109+
mut self: Pin<&mut Self>,
110+
cx: &mut Context,
111+
mut ctx: R,
112+
) -> Poll<Result<Self::Ok, Self::Error>>
113+
where
114+
Self: Sized,
115+
{
116+
let ctx = ctx.borrow_mut();
117+
118+
let this = &mut *self;
119+
120+
loop {
121+
if let Some(handles) = &mut this.handles {
122+
if let Some(future) = this.join.as_mut() {
123+
let handle = ready!(Pin::new(future).poll(cx, &mut *ctx))
124+
.map_err(IteratorError::Dispatch)?;
125+
this.data.push(handle);
126+
this.join.take();
127+
} else if let Some(handle) = handles.next() {
128+
this.join = Some(ctx.join(handle));
129+
} else {
130+
return Poll::Ready(Ok(replace(&mut this.data, Vec::new())
131+
.into_iter()
132+
.collect()));
133+
}
134+
} else {
135+
this.handles = Some(
136+
ready!(Pin::new(&mut *ctx).read(cx))
137+
.map_err(IteratorError::Transport)?
138+
.into_iter(),
139+
);
140+
}
141+
}
142+
}
143+
}
144+
145+
impl<T, C: ?Sized + Write<Vec<<C as Dispatch<T>>::Handle>> + Fork<T> + Unpin> Unravel<C> for Vec<T>
146+
where
147+
T: Unpin,
148+
C::Handle: Unpin,
149+
C::Future: Unpin,
150+
{
151+
type Future = IteratorUnravel<Vec<T>, C>;
152+
153+
fn unravel(self) -> Self::Future {
154+
let data = self.into_iter();
155+
156+
IteratorUnravel {
157+
fork: None,
158+
handles: Vec::with_capacity(data.size_hint().0),
159+
data,
160+
state: IteratorUnravelState::Writing,
161+
}
162+
}
163+
}
164+
165+
impl<T, C: ?Sized + Read<Vec<<C as Dispatch<T>>::Handle>> + Join<T> + Unpin> Coalesce<C> for Vec<T>
166+
where
167+
T: Unpin,
168+
C::Handle: Unpin,
169+
C::Future: Unpin,
170+
{
171+
type Future = IteratorCoalesce<T, Vec<T>, C>;
172+
173+
fn coalesce() -> Self::Future {
174+
IteratorCoalesce {
175+
data: Vec::new(),
176+
handles: None,
177+
join: None,
178+
ty: PhantomData,
179+
}
180+
}
181+
}

src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#![no_std]
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
3+
#[cfg(feature = "alloc")]
4+
extern crate alloc;
25

36
use core::{
47
borrow::BorrowMut,
@@ -12,6 +15,9 @@ mod pass;
1215
mod result;
1316
pub use pass::Pass;
1417

18+
#[cfg(feature = "alloc")]
19+
mod allocated;
20+
1521
pub trait Future<C: ?Sized> {
1622
type Ok;
1723
type Error;
@@ -20,9 +26,7 @@ pub trait Future<C: ?Sized> {
2026
self: Pin<&mut Self>,
2127
cx: &mut Context,
2228
ctx: R,
23-
) -> Poll<Result<Self::Ok, Self::Error>>
24-
where
25-
Self: Sized;
29+
) -> Poll<Result<Self::Ok, Self::Error>>;
2630
}
2731

2832
pub struct Ready<T> {
@@ -37,10 +41,7 @@ impl<C: ?Sized, T: Unpin> Future<C> for Ready<T> {
3741
mut self: Pin<&mut Self>,
3842
_: &mut Context,
3943
_: R,
40-
) -> Poll<Result<Self::Ok, Self::Error>>
41-
where
42-
Self: Sized,
43-
{
44+
) -> Poll<Result<Self::Ok, Self::Error>> {
4445
Poll::Ready(Ok(self.data.take().expect("Ready polled after completion")))
4546
}
4647
}

src/option.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ where
5858
mut self: Pin<&mut Self>,
5959
cx: &mut Context,
6060
mut ctx: R,
61-
) -> Poll<Result<Self::Ok, Self::Error>>
62-
where
63-
Self: Sized,
64-
{
61+
) -> Poll<Result<Self::Ok, Self::Error>> {
6562
let ctx = ctx.borrow_mut();
6663

6764
let this = &mut *self;
@@ -122,10 +119,7 @@ where
122119
mut self: Pin<&mut Self>,
123120
cx: &mut Context,
124121
mut ctx: R,
125-
) -> Poll<Result<Self::Ok, Self::Error>>
126-
where
127-
Self: Sized,
128-
{
122+
) -> Poll<Result<Self::Ok, Self::Error>> {
129123
let ctx = ctx.borrow_mut();
130124

131125
let this = &mut *self;

src/pass.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ where
4343
mut self: Pin<&mut Self>,
4444
cx: &mut Context,
4545
mut ctx: R,
46-
) -> Poll<Result<Self::Ok, Self::Error>>
47-
where
48-
Self: Sized,
49-
{
46+
) -> Poll<Result<Self::Ok, Self::Error>> {
5047
let ctx = ctx.borrow_mut();
5148

5249
let this = &mut *self;
@@ -101,10 +98,7 @@ where
10198
mut self: Pin<&mut Self>,
10299
cx: &mut Context,
103100
mut ctx: R,
104-
) -> Poll<Result<Self::Ok, Self::Error>>
105-
where
106-
Self: Sized,
107-
{
101+
) -> Poll<Result<Self::Ok, Self::Error>> {
108102
let ctx = ctx.borrow_mut();
109103

110104
let this = &mut *self;

src/result.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ where
9191
mut self: Pin<&mut Self>,
9292
cx: &mut Context,
9393
mut ctx: R,
94-
) -> Poll<Result<(), Self::Error>>
95-
where
96-
Self: Sized,
97-
{
94+
) -> Poll<Result<(), Self::Error>> {
9895
let ctx = ctx.borrow_mut();
9996

10097
let this = &mut *self;
@@ -186,10 +183,7 @@ where
186183
mut self: Pin<&mut Self>,
187184
cx: &mut Context,
188185
mut ctx: R,
189-
) -> Poll<Result<Self::Ok, Self::Error>>
190-
where
191-
Self: Sized,
192-
{
186+
) -> Poll<Result<Self::Ok, Self::Error>> {
193187
let ctx = ctx.borrow_mut();
194188

195189
let this = &mut *self;

0 commit comments

Comments
 (0)