So, in the async-book it is stated that:
https://rust-lang.github.io/async-book/03_async_await/01_chapter.html
async
bodies and other futures are lazy: they do nothing until they are run
That beeing the case, implementing a future like this, although possible, would be considered a bad practice ?
struct DoSomething;
impl DoSomething {
fn new() -> Self {
thread::spawn(|| {
// Already working before beeing awaited
});
Self
}
}
impl Future for DoSomething {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
todo!()
}
}
as oposed to something like this:
impl DoSomething2 {
fn new() -> Self {
Self { running: false }
}
}
impl Future for DoSomething2 {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if !self.running {
self.running = true;
thread::spawn(|| {
//working
});
Poll::Pending
} else {
// ...
Poll::Ready(())
}
}
}