Struct BoxS

Source
pub struct BoxS<T, M>
where T: ?Sized, M: Memory,
{ /* private fields */ }
Expand description

A box that exists entirely on the stack.

Implementations§

Source§

impl<T, M> BoxS<T, M>
where M: Memory,

Source

pub fn new(x: T) -> Self

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):

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):

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);
Source§

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

Source

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

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(())
}
Source§

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

Source

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

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§

Source§

impl<T, M> Debug for BoxS<T, M>
where T: ?Sized + Debug, M: Memory,

Source§

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

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

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

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

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

Source§

fn deref_mut(&mut self) -> &mut <Self as Deref>::Target

Mutably dereferences the value.
Source§

impl<T, M> Display for BoxS<T, M>
where T: ?Sized + Display, M: Memory,

Source§

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

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

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

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

impl<T, M> Send for BoxS<T, M>
where T: ?Sized + Send, M: Memory,

Source§

impl<T, M> Sync for BoxS<T, M>
where T: ?Sized + Sync, M: Memory,

Auto Trait Implementations§

§

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

§

impl<T, M> RefUnwindSafe for BoxS<T, M>

§

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

§

impl<T, M> UnwindSafe for BoxS<T, M>
where M: UnwindSafe, T: RefUnwindSafe + ?Sized,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.