SmallBox

Struct 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::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);
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. 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();
Source

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

pub fn into_inner(self) -> T
where 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);
Source

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

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>

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

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

Trait Implementations§

Source§

impl<T, Space> Clone for SmallBox<T, Space>
where T: Sized + Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate 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: Default, Space> Default for SmallBox<T, Space>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

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<F: Future + ?Sized, S> Future for SmallBox<F, S>

Source§

type Output = <F as Future>::Output

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. 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) -> Self
where Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests less than (for self and other) and is used by the < operator. Read more
Source§

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

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

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

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. Read more
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, Space> !Freeze for SmallBox<T, Space>

§

impl<T, Space> !RefUnwindSafe for SmallBox<T, Space>

§

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

§

impl<T, Space> UnwindSafe for SmallBox<T, Space>
where T: RefUnwindSafe + UnwindSafe + ?Sized, Space: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
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> ToOwned for T
where T: Clone,

Source§

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 T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.