Struct UnsafeFuture

Source
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

Source

pub fn new<F, R, W, Arg, CR>( future: F, waker: W, consume: fn(R, Arg) -> CR, ) -> Self
where F: Future<Output = R> + Send + 'static, R: Send + 'static, W: WakeSend,

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

§Examples

See UnsafeFuture documentation.

Source

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.

Source

pub unsafe fn is_ready(&self) -> bool

Returns true if associated future data is ready.

§Safety

Undefined behavior if associated FutureData has been dropped.

Source

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.

Source

pub unsafe fn will_wake<W>(self, other: &W) -> bool
where W: WakeSend + PartialEq,

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.

Source

pub unsafe fn set_waker<W>(self, waker: W) -> W
where 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

Source§

fn clone(&self) -> UnsafeFuture

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnsafeFuture

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for UnsafeFuture

Source§

fn eq(&self, other: &UnsafeFuture) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for UnsafeFuture

Source§

impl Eq for UnsafeFuture

Source§

impl Send for UnsafeFuture

Source§

impl StructuralPartialEq for UnsafeFuture

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.