[][src]Struct no_alloc::BoxS

pub struct BoxS<T, M> where
    T: ?Sized,
    M: Memory
{ /* fields omitted */ }

A box that exists entirely on the stack.

Implementations

impl<T, M> BoxS<T, M> where
    M: Memory
[src]

pub fn new(x: T) -> Self[src]

Acquires memory on the stack and places x into it.

The acquired memory is backed by M. If the size or alignment of T is greater than that of M the box cannot be constructed. This will reuslt in a compile fail.

If T is zero-sized then no memory is required; M may also be zero sized in this case.

Examples

Creating a boxed value:

use no_alloc::BoxS;

let boxed: BoxS<isize, [usize; 1]> = BoxS::new(0);

Creating a boxed ZST (zero-sized type):

use no_alloc::BoxS;

let boxed: BoxS<(), [usize; 0]> = BoxS::new(());

Failing to create a boxed value due to size error (this results in a compile error):

This example deliberately fails to compile
use no_alloc::BoxS;

let _impossible = BoxS::<isize, [u8; 0]>::new(0);

Failing to create a boxed value due to alignment error (this results in a compile error):

This example deliberately fails to compile
use core::mem::size_of;
use no_alloc::BoxS;

let _impossible = BoxS::<isize, [u8; size_of::<isize>()]>::new(0);

Coercing to a boxed DST (dynamically-sized type) (requires the coerce_unsized feature):

use core::any::Any;
use no_alloc::BoxS;

let boxed: BoxS<dyn Any, [usize; 1]> = BoxS::new(0_isize);

impl<M> BoxS<dyn Any + 'static, M> where
    M: Memory
[src]

pub fn downcast<T>(self) -> Result<BoxS<T, M>, Self> where
    T: Any
[src]

Attempts to downcast the box to a concrete type.

Examples

use core::any::Any;
use core::fmt;

use no_alloc::{BoxS, Memory};

fn write_if_str<W: fmt::Write, M: Memory>(
    mut wtr: W,
    boxed: BoxS<dyn Any + 'static, M>
) -> fmt::Result {
    if let Ok(s) = boxed.downcast::<&str>() {
        wtr.write_str(&s)?;
    }
    Ok(())
}

impl<M> BoxS<dyn Any + Send + 'static, M> where
    M: Memory
[src]

pub fn downcast<T>(self) -> Result<BoxS<T, M>, Self> where
    T: Any
[src]

Attempts to downcast the box to a concrete type.

Examples

use core::any::Any;
use core::fmt;

use no_alloc::{BoxS, Memory};

fn write_if_str<W: fmt::Write, M: Memory>(
    mut wtr: W,
    boxed: BoxS<dyn Any + Send + 'static, M>
) -> fmt::Result {
    if let Ok(s) = boxed.downcast::<&str>() {
        wtr.write_str(&s)?;
    }
    Ok(())
}

Trait Implementations

impl<T: ?Sized, M> Debug for BoxS<T, M> where
    T: Debug,
    M: Memory
[src]

impl<T, M> Deref for BoxS<T, M> where
    T: ?Sized,
    M: Memory
[src]

type Target = T

The resulting type after dereferencing.

impl<T, M> DerefMut for BoxS<T, M> where
    T: ?Sized,
    M: Memory
[src]

impl<T: ?Sized, M> Display for BoxS<T, M> where
    T: Display,
    M: Memory
[src]

impl<T, M> Drop for BoxS<T, M> where
    T: ?Sized,
    M: Memory
[src]

impl<T, M> Pointer for BoxS<T, M> where
    T: ?Sized,
    M: Memory
[src]

impl<T: ?Sized, M> Send for BoxS<T, M> where
    T: Send,
    M: Memory
[src]

impl<T: ?Sized, M> Sync for BoxS<T, M> where
    T: Sync,
    M: Memory
[src]

Auto Trait Implementations

impl<T: ?Sized, M> Unpin for BoxS<T, M> where
    M: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.