From 2ff011ffba95018013ad753e87116baf11fefb61 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 20 Feb 2020 14:40:47 +0100 Subject: [PATCH] Split code example into individual match cases; add code for `example` --- .../posts/12-async-await/index.md | 107 +++++++++--------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/blog/content/second-edition/posts/12-async-await/index.md b/blog/content/second-edition/posts/12-async-await/index.md index 37733985..e0ddc86a 100644 --- a/blog/content/second-edition/posts/12-async-await/index.md +++ b/blog/content/second-edition/posts/12-async-await/index.md @@ -387,62 +387,67 @@ TODO ```rust -impl Future for ExampleStateMachine { - type Output = String; // return type of `example` - - fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { - loop { - match self { // TODO: handle pinning - ExampleStateMachine::Start(state) => { - // from body of `example` - let foo_txt_future = async_read_file("foo.txt"); - // `.await` operation - let state = WaitingOnFooTxtState { - min_len: state.min_len, - foo_txt_future, - }; - *self = ExampleStateMachine::WaitingOnFooTxt(state); - } - ExampleStateMachine::WaitingOnFooTxt(state) => { - match state.foo_txt_future.poll(cx) { - Poll::Pending => return Poll::Pending, - Poll::Ready(content) => { - // from body of `example` - if content.len() < state.min_len { - let bar_txt_future = async_read_file("bar.txt"); - // `.await` operation - let state = WaitingOnBarTxtState { - content, - bar_txt_future, - }; - *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"); - } +ExampleStateMachine::Start(state) => { + // from body of `example` + let foo_txt_future = async_read_file("foo.txt"); + // `.await` operation + let state = WaitingOnFooTxtState { + min_len: state.min_len, + foo_txt_future, + }; + *self = ExampleStateMachine::WaitingOnFooTxt(state); +} +``` +```rust +ExampleStateMachine::WaitingOnFooTxt(state) => { + match state.foo_txt_future.poll(cx) { + Poll::Pending => return Poll::Pending, + Poll::Ready(content) => { + // from body of `example` + if content.len() < state.min_len { + let bar_txt_future = async_read_file("bar.txt"); + // `.await` operation + let state = WaitingOnBarTxtState { + content, + bar_txt_future, + }; + *self = ExampleStateMachine::WaitingOnBarTxt(state); + } else { + *self = ExampleStateMachine::End(EndState)); + return Poll::Ready(content); } } } } ``` +```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