Split code example into individual match cases; add code for example

This commit is contained in:
Philipp Oppermann
2020-02-20 14:40:47 +01:00
parent 7ce491df53
commit 2ff011ffba

View File

@@ -387,13 +387,7 @@ TODO
```rust ```rust
impl Future for ExampleStateMachine { ExampleStateMachine::Start(state) => {
type Output = String; // return type of `example`
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
loop {
match self { // TODO: handle pinning
ExampleStateMachine::Start(state) => {
// from body of `example` // from body of `example`
let foo_txt_future = async_read_file("foo.txt"); let foo_txt_future = async_read_file("foo.txt");
// `.await` operation // `.await` operation
@@ -402,8 +396,10 @@ impl Future for ExampleStateMachine {
foo_txt_future, foo_txt_future,
}; };
*self = ExampleStateMachine::WaitingOnFooTxt(state); *self = ExampleStateMachine::WaitingOnFooTxt(state);
} }
ExampleStateMachine::WaitingOnFooTxt(state) => { ```
```rust
ExampleStateMachine::WaitingOnFooTxt(state) => {
match state.foo_txt_future.poll(cx) { match state.foo_txt_future.poll(cx) {
Poll::Pending => return Poll::Pending, Poll::Pending => return Poll::Pending,
Poll::Ready(content) => { Poll::Ready(content) => {
@@ -422,8 +418,10 @@ impl Future for ExampleStateMachine {
} }
} }
} }
} }
ExampleStateMachine::WaitingOnFooTxt(state) => { ```
```rust
ExampleStateMachine::WaitingOnFooTxt(state) => {
match state.bar_txt_future.poll(cx) { match state.bar_txt_future.poll(cx) {
match state.bar_txt_future.poll(cx) { match state.bar_txt_future.poll(cx) {
Poll::Pending => return Poll::Pending, Poll::Pending => return Poll::Pending,
@@ -434,13 +432,20 @@ impl Future for ExampleStateMachine {
} }
} }
} }
} }
ExampleStateMachine::End(_) => { ```
```rust
ExampleStateMachine::End(_) => {
panic!("poll called after Poll::Ready was returned"); panic!("poll called after Poll::Ready was returned");
} }
} ```
}
}
```rust
fn example(min_len: usize) -> ExampleStateMachine {
ExampleStateMachine::Start(StartState {
min_len,
})
} }
``` ```