Skip to content

Instantly share code, notes, and snippets.

@niksaak
Created September 25, 2020 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niksaak/e29590df2a23b3736ce996199c63ff82 to your computer and use it in GitHub Desktop.
Save niksaak/e29590df2a23b3736ce996199c63ff82 to your computer and use it in GitHub Desktop.
pub struct LoadFile<'a> {
path: Option<&'a str>,
data: Mutex<Option<mq::fs::Response>>,
}
impl LoadFile {
pub fn new(path: &str) -> LoadFile {
LoadFile {
path: Some(path),
data: Mutex::new(None),
}
}
}
impl Future for LoadFile {
fn poll(self: Pin<&mut Self>, ctx: &mut task::Context) -> Poll<Self::Output> {
if let Some(path) = path.take() {
let waker = ctx.waker().clone();
mq::fs::load_file(path, move |response| {
*self.data.lock() = Some(response);
waker.wake();
});
match self.data.lock() {
None => Poll::Pending,
Some(response) => Poll::Ready(response),
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment