Skip to main content

XabiFuture

Struct XabiFuture 

Source
#[repr(C)]
pub struct XabiFuture { pub size: usize, pub abi_version: u32, pub instance: *mut c_void, pub poll: unsafe extern "C" fn(*mut c_void, *const XabiWaker, *mut XabiResult) -> i32, pub release: unsafe extern "C" fn(*mut c_void), }
Expand description

Future handle returned by async xabi vtable methods.

Hosts poll this handle through XabiFutureHandle. Exporters can construct a handle from a Rust future with XabiFuture::from_result_bytes.

Fields§

§size: usize

Size of this structure in bytes.

§abi_version: u32

ABI version for this structure.

§instance: *mut c_void

Opaque future state pointer.

§poll: unsafe extern "C" fn(*mut c_void, *const XabiWaker, *mut XabiResult) -> i32

Poll the future.

§release: unsafe extern "C" fn(*mut c_void)

Release the future state.

Implementations§

Source§

impl XabiFuture

Source

pub const ABI_VERSION: u32 = ABI_VERSION

ABI version expected by this structure.

Source

pub const MIN_SIZE: usize

Minimum required size for the current future representation.

Source

pub const FULL_SIZE: usize

Full size of this future representation.

Source

pub fn empty() -> Self

Create an empty invalid future placeholder.

let future = xabi::XabiFuture::empty();
assert!(future.validate().is_err());
Source

pub fn validate(&self) -> Result<()>

Validate the future layout and required fields.

let future = xabi::XabiFuture::from_result_bytes(async {
    Ok::<_, xabi::Error>(b"ready".to_vec())
});
future.validate().unwrap();
unsafe { (future.release)(future.instance) };
Source

pub fn from_result_bytes<F, E>(future: F) -> Self
where F: Future<Output = Result<Vec<u8>, E>> + Send + 'static, E: XabiType + 'static,

Convert a Rust future returning bytes into an xabi future handle.

The future must be Send so host executors may move the foreign future between worker threads.

use std::future::Future;
use std::pin::pin;
use std::sync::Arc;
use std::task::{Context, Poll, Wake, Waker};

struct Noop;
impl Wake for Noop {
    fn wake(self: Arc<Self>) {}
}

let future = xabi::XabiFuture::from_result_bytes(async {
    Ok::<_, xabi::Error>(b"hello".to_vec())
});
let mut future = pin!(xabi::XabiFutureHandle::new(future).unwrap());
let waker = Waker::from(Arc::new(Noop));
let mut cx = Context::from_waker(&waker);

match Future::poll(future.as_mut(), &mut cx) {
    Poll::Ready(Ok(bytes)) => assert_eq!(bytes, b"hello"),
    other => panic!("unexpected poll result: {other:?}"),
}
Source

pub fn from_result_value<F, T, E>(future: F) -> Self
where F: Future<Output = Result<T, E>> + Send + 'static, T: XabiType + 'static, E: XabiType + 'static,

Convert a Rust future returning an xabi value into an ABI future handle.

The success value is encoded with XabiType::into_payload.

Trait Implementations§

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> 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, 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.