Wow! This is some dark arts man! If you can link me to some learning material that teaches me how it's all put together, that would be amazing. I want to learn this!
So I made some tweaks, I gave CbFuture a new()
impl<T> CbFuture<T> {
pub fn new() -> Self {
Self(Rc::new(CallbackFutureInner::<T>::default()))
}
(Curiously, It's not necessary to turbofish CallbackFutureInner)
I also couldn't rely on #[derive(Default)], So I created it:
impl<T> Default for CallbackFutureInner<T> {
fn default() -> Self {
Self {
waker: Cell::new(None),
result: Cell::new(None),
}
}
}
Now, when actually using it, how am I supposed to use it?
fn load_image(path: &str) -> impl std::future::Future {
use callback_future::CbFuture;
let cb_future = CbFuture::<Vec<u8>>::new();
fs::load_file(path, move |response| {
cb_future.publish(response.unwrap())
});
cb_future
}
This gives me this error:
error[E0382]: use of moved value: `cb_future`
--> src/main.rs:92:9
|
88 | let cb_future = CbFuture::<Vec<u8>>::new();
| --------- move occurs because `cb_future` has type `CbFuture<Vec<u8>>`, which does not implement the `Copy` trait
89 | fs::load_file(path, move |response| {
| --------------- value moved into closure here
90 | cb_future.publish(response.unwrap())
| --------- variable moved due to use in closure
91 | });
92 | cb_future
| ^^^^^^^^^ value used here after move
I don't think we want CbFuture to impl Copy do we?
Edit: I'm doing some reading; it looks like RefCell instead of Cell, may fix this. But it is late right now, I'll have a look tomorrow. 