Struct smallbox::SmallBox

source ·
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>

source

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);
source

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();
source

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());
source

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>

source

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>

source

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, Space> Clone for SmallBox<T, Space>where T: Sized + Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: ?Sized + Debug, Space> Debug for SmallBox<T, Space>

source§

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

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

impl<T: ?Sized, Space> Deref for SmallBox<T, Space>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T: ?Sized, Space> DerefMut for SmallBox<T, Space>

source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
source§

impl<T: ?Sized + Display, Space> Display for SmallBox<T, Space>

source§

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

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

impl<T: ?Sized, Space> Drop for SmallBox<T, Space>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: ?Sized + Hash, Space> Hash for SmallBox<T, Space>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: ?Sized + Ord, Space> Ord for SmallBox<T, Space>

source§

fn cmp(&self, other: &SmallBox<T, Space>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: ?Sized + PartialEq, Space> PartialEq<SmallBox<T, Space>> for SmallBox<T, Space>

source§

fn eq(&self, other: &SmallBox<T, Space>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: ?Sized + PartialOrd, Space> PartialOrd<SmallBox<T, Space>> for SmallBox<T, Space>

source§

fn partial_cmp(&self, other: &SmallBox<T, Space>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
source§

fn lt(&self, other: &SmallBox<T, Space>) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
source§

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 more
source§

fn ge(&self, other: &SmallBox<T, Space>) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

fn gt(&self, other: &SmallBox<T, Space>) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
source§

impl<T: ?Sized, Space> Pointer for SmallBox<T, Space>

source§

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

Formats the value using the given formatter.
source§

impl<T: ?Sized + Eq, Space> Eq for SmallBox<T, Space>

source§

impl<T: ?Sized + Send, Space> Send for SmallBox<T, Space>

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.