Why I'm allowed to move the value through std::mem::replace, while moving through dereferencing is not allowed in Future dynamic objects, when the objective is to pin the values to memory.
use std::time::Duration;
struct TimerFuture {
is_done: bool,
duration: Duration,
}
impl TimerFuture {
fn new(duration: Duration) -> Self {
Self {
is_done: false,
duration,
}
}
}
impl Future for TimerFuture {
type Output = ();
fn poll(
mut self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
// Due to shared reference TimerFuture cannot be moved and it's valid
// in the sense of pinning the value to memory, even a &mut Self would
// do instead of Pin<&mut Self>.
// let _c=*self;
// But I can do below by calling replace function on Pin<&mut Self>
// that moves current TimerFuture to _c, copying new TimerFuture.
// Isn't this invalid when the objective isto pin the value.
let _c = std::mem::replace(&mut *self, TimerFuture::new(Duration::from_millis(500)));
std::task::Poll::Ready(())
}
}