pub struct BoxS<T, M>{ /* private fields */ }
Expand description
A box that exists entirely on the stack.
Implementations§
Source§impl<T, M> BoxS<T, M>where
M: Memory,
impl<T, M> BoxS<T, M>where
M: Memory,
Sourcepub fn new(x: T) -> Self
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,
impl<M> BoxS<dyn Any + 'static, M>where
M: Memory,
Sourcepub fn downcast<T>(self) -> Result<BoxS<T, M>, Self>where
T: Any,
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,
impl<M> BoxS<dyn Any + Send + 'static, M>where
M: Memory,
Sourcepub fn downcast<T>(self) -> Result<BoxS<T, M>, Self>where
T: Any,
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(())
}