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::SmallBox;
use smallbox::space::*;
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. Once the data is moved to heap, it will never be moved back to stack.
§Example
use smallbox::SmallBox;
use smallbox::space::S2;
use smallbox::space::S4;
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::SmallBox;
use smallbox::space::S1;
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::SmallBox;
use smallbox::space::S1;
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);Sourcepub fn from_box(boxed: Box<T>) -> Self
pub fn from_box(boxed: Box<T>) -> Self
Creates a SmallBox from a standard Box.
The data will always be stored on the heap since it’s already allocated there.
This method transfers ownership from the Box to the SmallBox without copying
or moving the data.
§Example
use smallbox::SmallBox;
use smallbox::space::S4;
let boxed = Box::new([1, 2, 3, 4]);
let small_box: SmallBox<_, S4> = SmallBox::from_box(boxed);
assert!(small_box.is_heap());
assert_eq!(*small_box, [1, 2, 3, 4]);Sourcepub fn into_box(boxed: SmallBox<T, Space>) -> Box<T>
pub fn into_box(boxed: SmallBox<T, Space>) -> Box<T>
Converts a SmallBox into a standard Box.
If the data is stored on the stack, it will be moved to the heap. If the data is already on the heap, ownership is transferred without copying or moving the data.
§Example
use smallbox::SmallBox;
use smallbox::space::S4;
let small_box: SmallBox<_, S4> = SmallBox::new([1, 2, 3, 4]);
let boxed: Box<[i32; 4]> = SmallBox::into_box(small_box);
assert_eq!(*boxed, [1, 2, 3, 4]);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::SmallBox;
use smallbox::space::*;
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::SmallBox;
use smallbox::space::*;
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));
}