-
-
Save yhara/11767a8880a3f60c31b92b5eb5527caa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//use std::time::Duration; | |
use std::task::Poll; | |
use std::future::{poll_fn, Future }; | |
use tokio::process::Command; | |
fn main() { | |
// async fn foo() { | |
// println!("1"); | |
// println!("{:?}", Command::new("date").output().await.unwrap().stdout); | |
// println!("{:?}", Command::new("date").output().await.unwrap().stdout); | |
// println!("2"); | |
// } | |
// let mut future = Box::pin(foo()); | |
let mut state = 0; | |
let mut inner_future = None; | |
let mut future = Box::pin(poll_fn(move |context| { | |
loop { | |
match state { | |
0 => { | |
println!("1"); | |
inner_future = Some(Box::pin(Command::new("date").output())); | |
state = 1; | |
}, | |
1 => { | |
match inner_future.as_mut().unwrap().as_mut().poll(context) { | |
Poll::Ready(v) => { | |
println!("{:?}", v); | |
inner_future = Some(Box::pin(Command::new("date").output())); | |
state = 2; | |
}, | |
Poll::Pending => return Poll::Pending, | |
} | |
} | |
2 => { | |
match inner_future.as_mut().unwrap().as_mut().poll(context) { | |
Poll::Ready(v) => { | |
println!("{:?}", v); | |
println!("2"); | |
return Poll::Ready(()); | |
} | |
Poll::Pending => return Poll::Pending, | |
} | |
} | |
_ => panic!("invalid state {state}") | |
} | |
} | |
})); | |
let poller = poll_fn(move |context| { | |
println!("[main poll]"); | |
let tmp = future.as_mut().poll(context); //.as_mut().unwrap().as_mut().poll(context); | |
dbg!(&tmp); | |
tmp | |
}); | |
tokio::runtime::Builder::new_current_thread() | |
.enable_all() | |
.build() | |
.unwrap() | |
.block_on(poller); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment