Skip to main content

InlineFuture

Struct InlineFuture 

Source
pub struct InlineFuture<'a, T, const WORDS: usize> { /* private fields */ }
Expand description

Type-erased future stored inline when it fits into WORDS machine words.

A future is stored inline when its size is at most max(WORDS, 1) words and its alignment is at most the alignment of usize. Other futures are boxed. The slot always holds at least one word, so WORDS = 0 boxes every future larger than a pointer.

The future and its output may borrow data for 'a. Like Pin<Box<dyn Future>>, InlineFuture is neither Send nor Sync. Unlike a box, it is not Unpin; pin it before polling, for example with std::pin::pin! or by awaiting it.

Implementations§

Source§

impl<'a, T, const WORDS: usize> InlineFuture<'a, T, WORDS>

Source

pub fn new<F>(fut: F) -> InlineFuture<'a, T, WORDS> ⓘ
where F: Future<Output = T> + 'a,

Erases the type of fut, storing it inline if it fits and boxing it otherwise.

Source

pub fn new_with<F>(f: impl FnOnce() -> F) -> InlineFuture<'a, T, WORDS> ⓘ
where F: Future<Output = T> + 'a,

Erases the type of the future returned by f, storing it inline if it fits and boxing it otherwise.

Unlike new, the storage is prepared before f runs, so the future can be built directly in place instead of on the stack and then copied. This matters for large futures that are boxed.

use ntex_service::dev::InlineFuture;

fn double(value: u32) -> InlineFuture<'static, u32, 0> {
    InlineFuture::new_with(|| async move { value * 2 })
}
Source

pub const fn empty() -> InlineFuture<'a, T, WORDS> ⓘ

Creates an empty placeholder, to be filled by set_with once pinned.

Polling an empty future panics, dropping it does nothing.

Source

pub fn set_with<F>( self: Pin<&mut InlineFuture<'a, T, WORDS>>, f: impl FnOnce() -> F, )
where F: Future<Output = T> + 'a,

Drops the stored future and replaces it with the future returned by f, storing it inline if it fits and boxing it otherwise.

The new future is built directly in the pinned storage, so a large future is never copied. With empty, this lets a callee build its future in storage owned by the caller instead of returning it by value.

If dropping the previous future or calling f panics, the future is left empty.

use std::pin::{Pin, pin};
use ntex_service::dev::InlineFuture;

fn double(value: u32, fut: Pin<&mut InlineFuture<'static, u32, 4>>) {
    fut.set_with(|| async move { value * 2 });
}

let mut fut = pin!(InlineFuture::empty());
double(21, fut.as_mut());
assert_eq!(fut.await, 42);
Source

pub const fn fits<F>() -> bool

Returns true if a future of type F is stored inline.

Trait Implementations§

Source§

impl<T, const WORDS: usize> Debug for InlineFuture<'_, T, WORDS>

Source§

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

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

impl<T, const WORDS: usize> Drop for InlineFuture<'_, T, WORDS>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T, const WORDS: usize> Future for InlineFuture<'_, T, WORDS>

Source§

type Output = T

The type of value produced on completion.
Source§

fn poll( self: Pin<&mut InlineFuture<'_, T, WORDS>>, cx: &mut Context<'_>, ) -> Poll<T>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

§

impl<'a, T, const WORDS: usize> !RefUnwindSafe for InlineFuture<'a, T, WORDS>

§

impl<'a, T, const WORDS: usize> !Send for InlineFuture<'a, T, WORDS>

§

impl<'a, T, const WORDS: usize> !Sync for InlineFuture<'a, T, WORDS>

§

impl<'a, T, const WORDS: usize> !Unpin for InlineFuture<'a, T, WORDS>

§

impl<'a, T, const WORDS: usize> !UnsafeUnpin for InlineFuture<'a, T, WORDS>

§

impl<'a, T, const WORDS: usize> !UnwindSafe for InlineFuture<'a, T, WORDS>

§

impl<'a, T, const WORDS: usize> Freeze for InlineFuture<'a, T, WORDS>

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<F> OnDropFutureExt for F
where F: Future,

Source§

fn on_drop<F>(self, on_drop: F) -> OnDropFuture<Self, F> ⓘ
where F: FnOnce(),

Wraps this future so that on_drop is called if the future is dropped before it completes. The callback is cancelled once the future completes, whatever its output.
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

impl<F, T, E> TryFuture for F
where F: Future<Output = Result<T, E>> + ?Sized,

§

type Ok = T

The type of successful values yielded by this future
§

type Error = E

The type of failures yielded by this future
§

fn try_poll( self: Pin<&mut F>, cx: &mut Context<'_>, ) -> Poll<<F as Future>::Output>

Poll this TryFuture as if it were a Future. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V