pub struct SmallBox<T: ?Sized, Space> { /* private fields */ }
Expand description
An optimized box that store value on stack or on heap depending on its size
Implementations§
source§impl<T: ?Sized, Space> SmallBox<T, Space>
impl<T: ?Sized, Space> SmallBox<T, Space>
sourcepub fn new(val: T) -> SmallBox<T, Space>where
T: Sized,
pub fn new(val: T) -> SmallBox<T, Space>where T: Sized,
Box value on stack or on heap depending on its size.
Example
use smallbox::space::*;
use smallbox::SmallBox;
let small: SmallBox<_, S4> = SmallBox::new([0usize; 2]);
let large: SmallBox<_, S4> = SmallBox::new([1usize; 8]);
assert_eq!(small.len(), 2);
assert_eq!(large[7], 1);
assert!(large.is_heap() == true);
sourcepub fn resize<ToSpace>(self) -> SmallBox<T, ToSpace>
pub fn resize<ToSpace>(self) -> SmallBox<T, ToSpace>
Change the capacity of SmallBox
.
This method may move stack-allocated data from stack to heap when inline space is not sufficient. And once the data is moved to heap, it’ll never be moved again.
Example
use smallbox::space::S2;
use smallbox::space::S4;
use smallbox::SmallBox;
let s: SmallBox<_, S4> = SmallBox::new([0usize; 4]);
let m: SmallBox<_, S2> = s.resize();
sourcepub fn is_heap(&self) -> bool
pub fn is_heap(&self) -> bool
Returns true if data is allocated on heap.
Example
use smallbox::space::S1;
use smallbox::SmallBox;
let stacked: SmallBox<usize, S1> = SmallBox::new(0usize);
assert!(!stacked.is_heap());
let heaped: SmallBox<(usize, usize), S1> = SmallBox::new((0usize, 1usize));
assert!(heaped.is_heap());
sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere T: Sized,
Consumes the SmallBox and returns ownership of the boxed value
Examples
use smallbox::space::S1;
use smallbox::SmallBox;
let stacked: SmallBox<_, S1> = SmallBox::new([21usize]);
let val = stacked.into_inner();
assert_eq!(val[0], 21);
let boxed: SmallBox<_, S1> = SmallBox::new(vec![21, 56, 420]);
let val = boxed.into_inner();
assert_eq!(val[1], 56);
source§impl<Space> SmallBox<dyn Any, Space>
impl<Space> SmallBox<dyn Any, Space>
sourcepub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self>
pub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self>
Attempt to downcast the box to a concrete type.
Examples
#[macro_use]
extern crate smallbox;
use core::any::Any;
use smallbox::space::*;
use smallbox::SmallBox;
fn print_if_string(value: SmallBox<dyn Any, S1>) {
if let Ok(string) = value.downcast::<String>() {
println!("String ({}): {}", string.len(), string);
}
}
fn main() {
let my_string = "Hello World".to_string();
print_if_string(smallbox!(my_string));
print_if_string(smallbox!(0i8));
}
source§impl<Space> SmallBox<dyn Any + Send, Space>
impl<Space> SmallBox<dyn Any + Send, Space>
sourcepub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self>
pub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self>
Attempt to downcast the box to a concrete type.
Examples
#[macro_use]
extern crate smallbox;
use core::any::Any;
use smallbox::space::*;
use smallbox::SmallBox;
fn print_if_string(value: SmallBox<dyn Any, S1>) {
if let Ok(string) = value.downcast::<String>() {
println!("String ({}): {}", string.len(), string);
}
}
fn main() {
let my_string = "Hello World".to_string();
print_if_string(smallbox!(my_string));
print_if_string(smallbox!(0i8));
}
Trait Implementations§
source§impl<T: ?Sized + Ord, Space> Ord for SmallBox<T, Space>
impl<T: ?Sized + Ord, Space> Ord for SmallBox<T, Space>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<T: ?Sized + PartialEq, Space> PartialEq<SmallBox<T, Space>> for SmallBox<T, Space>
impl<T: ?Sized + PartialEq, Space> PartialEq<SmallBox<T, Space>> for SmallBox<T, Space>
source§impl<T: ?Sized + PartialOrd, Space> PartialOrd<SmallBox<T, Space>> for SmallBox<T, Space>
impl<T: ?Sized + PartialOrd, Space> PartialOrd<SmallBox<T, Space>> for SmallBox<T, Space>
source§fn le(&self, other: &SmallBox<T, Space>) -> bool
fn le(&self, other: &SmallBox<T, Space>) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl<T: ?Sized + Eq, Space> Eq for SmallBox<T, Space>
impl<T: ?Sized + Send, Space> Send for SmallBox<T, Space>
impl<T: ?Sized + Sync, Space> Sync for SmallBox<T, Space>
Auto Trait Implementations§
impl<T: ?Sized, Space> RefUnwindSafe for SmallBox<T, Space>where Space: RefUnwindSafe, T: RefUnwindSafe,
impl<T: ?Sized, Space> Unpin for SmallBox<T, Space>where Space: Unpin, T: Unpin,
impl<T: ?Sized, Space> UnwindSafe for SmallBox<T, Space>where Space: UnwindSafe, T: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more