pub struct UnsafeFuture { /* private fields */ }Expand description
A handle to a future data.
Name contains future, but this struct doesn’t implement Future trait.
It provides you poll function instead. You can call poll function on a
handle and get the result if the FutureData is ready.
Plus, this is actually a type-erased pointer to a FutureData so that
owners must deal with the pointer carefully. See the example below to get a
feel for how to use the struct.
§Examples
use my_ecs::ds::{WakeSend, UnsafeFuture, ReadyFuture};
use std::{
future::Future,
task::{Poll, Context},
sync::mpsc::{self, Sender},
pin::Pin,
};
#[derive(Clone)]
struct MyWaker(Sender<UnsafeFuture>);
impl WakeSend for MyWaker {
fn wake_send(&self, handle: UnsafeFuture) {
self.0.send(handle).unwrap();
}
}
struct MyFuture(u32);
impl Future for MyFuture {
type Output = u32;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if this.0 == 0 {
Poll::Ready(10)
} else {
this.0 -= 1;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
let (tx, rx) = mpsc::channel();
let fut = MyFuture(2);
let waker = MyWaker(tx);
let consume = |ret: u32, arg: u32| ret + arg;
let mut u_fut = UnsafeFuture::new(fut, waker, consume);
unsafe {
let mut pending = 0;
while u_fut.poll() == Poll::Pending {
u_fut = rx.recv().unwrap();
pending += 1;
}
let r_fut = ReadyFuture::new(u_fut);
let res: u32 = r_fut.consume(1);
assert_eq!(pending, 2);
assert_eq!(res, consume(10, 1));
}Implementations§
Source§impl UnsafeFuture
impl UnsafeFuture
Sourcepub fn new<F, R, W, Arg, CR>(
future: F,
waker: W,
consume: fn(R, Arg) -> CR,
) -> Self
pub fn new<F, R, W, Arg, CR>( future: F, waker: W, consume: fn(R, Arg) -> CR, ) -> Self
Creates a future data in heap memory and returns its handle.
§Leaks
There will be memory leak if caller doesn’t deallocate the future data. Future data can be deallocated by
- Calling
UnsafeFuture::destroy. - Turning
UnsafeFutureintoReadyFuturethen dropping it.ReadyFuturecallsUnsafeFuture::destroywhen it’s dropped.
§Examples
See UnsafeFuture documentation.
Sourcepub unsafe fn destroy(self)
pub unsafe fn destroy(self)
Drops and deallocates associated future data.
You may need this method when you have to cancel out not ready futures.
§Safety
This method must be called only once for the same handles.
Sourcepub unsafe fn is_ready(&self) -> bool
pub unsafe fn is_ready(&self) -> bool
Returns true if associated future data is ready.
§Safety
Undefined behavior if associated FutureData has been dropped.
Sourcepub unsafe fn poll(self) -> Poll<()>
pub unsafe fn poll(self) -> Poll<()>
Tries to make more progress on the associated future data.
Returning value Poll::Ready means the FutureData is completely
resolved and ready to provide its output. Poll::Pending, on the
other hand, means the FutureData is not yet ready and will wake async
runtime via the waker you inserted at UnsafeFuture::new when it can
make more progress.
§Safety
Associated future data must be alive, not have been dropped.
§Examples
See UnsafeFuture documentation.
Sourcepub unsafe fn will_wake<W>(self, other: &W) -> bool
pub unsafe fn will_wake<W>(self, other: &W) -> bool
Returns true if the given waker is the same as the type you inserted at
UnsafeFuture::new.
§Safety
Waker type W must be the same as the type you inserted at
UnsafeFuture::new.
Sourcepub unsafe fn set_waker<W>(self, waker: W) -> Wwhere
W: WakeSend,
pub unsafe fn set_waker<W>(self, waker: W) -> Wwhere
W: WakeSend,
Sets a new waker to the associated future data.
§Safety
Waker type W must be the same as the type you inserted at
UnsafeFuture::new.
Trait Implementations§
Source§impl Clone for UnsafeFuture
impl Clone for UnsafeFuture
Source§fn clone(&self) -> UnsafeFuture
fn clone(&self) -> UnsafeFuture
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UnsafeFuture
impl Debug for UnsafeFuture
Source§impl PartialEq for UnsafeFuture
impl PartialEq for UnsafeFuture
impl Copy for UnsafeFuture
impl Eq for UnsafeFuture
impl Send for UnsafeFuture
impl StructuralPartialEq for UnsafeFuture
Auto Trait Implementations§
impl Freeze for UnsafeFuture
impl RefUnwindSafe for UnsafeFuture
impl !Sync for UnsafeFuture
impl Unpin for UnsafeFuture
impl UnwindSafe for UnsafeFuture
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more