bevy_asset::io

Struct StackFuture

Source
#[repr(C)]
pub struct StackFuture<'a, T, const STACK_SIZE: usize> { /* private fields */ }
Expand description

A wrapper that stores a future in space allocated by the container

Often this space comes from the calling function’s stack, but it could just as well come from some other allocation.

A StackFuture can be used to emulate async functions in dyn Trait objects. For example:

trait PseudoAsyncTrait {
    fn do_something(&self) -> StackFuture<'_, (), { 512 }>;
}

impl PseudoAsyncTrait for i32 {
    fn do_something(&self) -> StackFuture<'_, (), { 512 }> {
        StackFuture::from(async {
            // function body goes here
        })
    }
}

async fn use_dyn_async_trait(x: &dyn PseudoAsyncTrait) {
    x.do_something().await;
}

async fn call_with_dyn_async_trait() {
    use_dyn_async_trait(&42).await;
}

This example defines PseudoAsyncTrait with a single method do_something. The do_something method can be called as if it were declared as async fn do_something(&self). To implement do_something, the easiest thing to do is to wrap the body of the function in StackFuture::from(async { ... }), which creates an anonymous future for the body and stores it in a StackFuture.

Because StackFuture does not know the size of the future it wraps, the maximum size of the future must be specified in the STACK_SIZE parameter. In the example here, we’ve used a stack size of 512, which is probably much larger than necessary but would accommodate many futures besides the simple one we’ve shown here.

StackFuture ensures when wrapping a future that enough space is available, and it also respects any alignment requirements for the wrapped future. Note that the wrapped future’s alignment must be less than or equal to that of the overall StackFuture struct.

Implementations§

Source§

impl<'a, T, const STACK_SIZE: usize> StackFuture<'a, T, STACK_SIZE>

Source

pub fn from<F>(future: F) -> StackFuture<'a, T, STACK_SIZE>
where F: Future<Output = T> + Send + 'a,

Creates a StackFuture from an existing future

See the documentation on StackFuture for examples of how to use this.

The size and alignment requirements are statically checked, so it is a compiler error to use this with a future that does not fit within the StackFuture’s size and alignment requirements.

The following example illustrates a compile error for a future that is too large.

// Fails because the future contains a large array and is therefore too big to fit in
// a 16-byte `StackFuture`.
let f = StackFuture::<_, { 16 }>::from(async {
    let x = [0u8; 4096];
    async {}.await;
    println!("{}", x.len());
});

The example below illustrates a compiler error for a future whose alignment is too large.


#[derive(Debug)]
#[repr(align(256))]
struct BigAlignment(usize);

// Fails because the future contains a large array and is therefore too big to fit in
// a 16-byte `StackFuture`.
let f = StackFuture::<_, { 16 }>::from(async {
    let x = BigAlignment(42);
    async {}.await;
    println!("{x:?}");
});
Source

pub fn try_from<F>( future: F, ) -> Result<StackFuture<'a, T, STACK_SIZE>, IntoStackFutureError<F>>
where F: Future<Output = T> + Send + 'a,

Attempts to create a StackFuture from an existing future

If the StackFuture is not large enough to hold future, this function returns an Err with the argument future returned to you.

Panics

If we cannot satisfy the alignment requirements for F, this function will panic.

Source

pub fn from_or_box<F>(future: F) -> StackFuture<'a, T, STACK_SIZE>
where F: Future<Output = T> + Send + 'a,

Available on crate feature alloc only.

Creates a StackFuture from the given future, boxing if necessary

This version will succeed even if the future is larger than STACK_SIZE. If the future is too large, from_or_box will allocate a Box on the heap and store the resulting boxed future in the StackFuture.

The same thing also happens if the wrapped future’s alignment is larger than StackFuture’s alignment.

This function requires the “alloc” crate feature.

Source

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

Determines whether this StackFuture can hold a value of type F

Source

pub const fn has_space_for_val<F>(_: &F) -> bool

Determines whether this StackFuture can hold the referenced value

Source

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

Determines whether this StackFuture’s alignment is compatible with the type F.

Source

pub const fn has_alignment_for_val<F>(_: &F) -> bool

Determines whether this StackFuture’s alignment is compatible with the referenced value.

Trait Implementations§

Source§

impl<'a, T, const STACK_SIZE: usize> Drop for StackFuture<'a, T, STACK_SIZE>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T, const STACK_SIZE: usize> Future for StackFuture<'a, T, STACK_SIZE>

Source§

type Output = T

The type of value produced on completion.
Source§

fn poll( self: Pin<&mut StackFuture<'a, T, STACK_SIZE>>, cx: &mut Context<'_>, ) -> Poll<<StackFuture<'a, T, STACK_SIZE> as Future>::Output>

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 STACK_SIZE: usize> Freeze for StackFuture<'a, T, STACK_SIZE>

§

impl<'a, T, const STACK_SIZE: usize> !RefUnwindSafe for StackFuture<'a, T, STACK_SIZE>

§

impl<'a, T, const STACK_SIZE: usize> Send for StackFuture<'a, T, STACK_SIZE>

§

impl<'a, T, const STACK_SIZE: usize> !Sync for StackFuture<'a, T, STACK_SIZE>

§

impl<'a, T, const STACK_SIZE: usize> !Unpin for StackFuture<'a, T, STACK_SIZE>

§

impl<'a, T, const STACK_SIZE: usize> !UnwindSafe for StackFuture<'a, T, STACK_SIZE>

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<F, T> AssetReaderFuture for F

Source§

type Value = T

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<F> FutureExt for F
where F: Future + ?Sized,

Source§

fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>
where Self: Unpin,

A convenience for calling Future::poll() on !Unpin types.
Source§

fn or<F>(self, other: F) -> Or<Self, F>
where Self: Sized, F: Future<Output = Self::Output>,

Returns the result of self or other future, preferring self if both are ready. Read more
Source§

fn race<F>(self, other: F) -> Race<Self, F>
where Self: Sized, F: Future<Output = Self::Output>,

Available on crate features std and race only.
Returns the result of self or other future, with no preference if both are ready. Read more
Source§

fn catch_unwind(self) -> CatchUnwind<Self>
where Self: Sized + UnwindSafe,

Available on crate feature std only.
Catches panics while polling the future. Read more
Source§

fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
where Self: Sized + Send + 'a,

Available on crate feature alloc only.
Boxes the future and changes its type to dyn Future + Send + 'a. Read more
Source§

fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>
where Self: Sized + 'a,

Available on crate feature alloc only.
Boxes the future and changes its type to dyn Future + 'a. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<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<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<F, T, E> TryFuture for F
where F: Future<Output = Result<T, E>> + ?Sized,

Source§

type Ok = T

The type of successful values yielded by this future
Source§

type Error = E

The type of failures yielded by this future
Source§

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> ConditionalSendFuture for T