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,62 +387,67 @@ TODO
```rust ```rust
impl Future for ExampleStateMachine { ExampleStateMachine::Start(state) => {
type Output = String; // return type of `example` // from body of `example`
let foo_txt_future = async_read_file("foo.txt");
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { // `.await` operation
loop { let state = WaitingOnFooTxtState {
match self { // TODO: handle pinning min_len: state.min_len,
ExampleStateMachine::Start(state) => { foo_txt_future,
// from body of `example` };
let foo_txt_future = async_read_file("foo.txt"); *self = ExampleStateMachine::WaitingOnFooTxt(state);
// `.await` operation }
let state = WaitingOnFooTxtState { ```
min_len: state.min_len, ```rust
foo_txt_future, ExampleStateMachine::WaitingOnFooTxt(state) => {
}; match state.foo_txt_future.poll(cx) {
*self = ExampleStateMachine::WaitingOnFooTxt(state); Poll::Pending => return Poll::Pending,
} Poll::Ready(content) => {
ExampleStateMachine::WaitingOnFooTxt(state) => { // from body of `example`
match state.foo_txt_future.poll(cx) { if content.len() < state.min_len {
Poll::Pending => return Poll::Pending, let bar_txt_future = async_read_file("bar.txt");
Poll::Ready(content) => { // `.await` operation
// from body of `example` let state = WaitingOnBarTxtState {
if content.len() < state.min_len { content,
let bar_txt_future = async_read_file("bar.txt"); bar_txt_future,
// `.await` operation };
let state = WaitingOnBarTxtState { *self = ExampleStateMachine::WaitingOnBarTxt(state);
content, } else {
bar_txt_future, *self = ExampleStateMachine::End(EndState));
}; return Poll::Ready(content);
*self = ExampleStateMachine::WaitingOnBarTxt(state);
} else {
*self = ExampleStateMachine::End(EndState));
return Poll::Ready(content);
}
}
}
}
ExampleStateMachine::WaitingOnFooTxt(state) => {
match state.bar_txt_future.poll(cx) {
match state.bar_txt_future.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(bar_txt) => {
*self = ExampleStateMachine::End(EndState));
// from body of `example`
return Poll::Ready(state.content + &bar_txt);
}
}
}
}
ExampleStateMachine::End(_) => {
panic!("poll called after Poll::Ready was returned");
}
} }
} }
} }
} }
``` ```
```rust
ExampleStateMachine::WaitingOnFooTxt(state) => {
match state.bar_txt_future.poll(cx) {
match state.bar_txt_future.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(bar_txt) => {
*self = ExampleStateMachine::End(EndState));
// from body of `example`
return Poll::Ready(state.content + &bar_txt);
}
}
}
}
```
```rust
ExampleStateMachine::End(_) => {
panic!("poll called after Poll::Ready was returned");
}
```
```rust
fn example(min_len: usize) -> ExampleStateMachine {
ExampleStateMachine::Start(StartState {
min_len,
})
}
```
### Pinning ### Pinning