[][src]Struct smallbox::SmallBox

pub struct SmallBox<T: ?Sized, Space> { /* fields omitted */ }

An optimized box that store value on stack or on heap depending on its size

Methods

impl<T: ?Sized, Space> SmallBox<T, Space>[src]

pub fn new(val: T) -> SmallBox<T, Space> where
    T: Sized
[src]

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

pub fn resize<ToSpace>(self) -> SmallBox<T, ToSpace>[src]

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::SmallBox;
use smallbox::space::{S2, S4};

let s: SmallBox::<_, S4> = SmallBox::new([0usize; 4]);
let m: SmallBox::<_, S2> = s.resize();

pub fn is_heap(&self) -> bool[src]

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

pub fn into_inner(self) -> T where
    T: Sized
[src]

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

impl<Space> SmallBox<dyn Any, Space>[src]

pub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self>[src]

Attempt to downcast the box to a concrete type.

Examples

#[macro_use]
extern crate smallbox;

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

impl<Space> SmallBox<dyn Any + Send, Space>[src]

pub fn downcast<T: Any>(self) -> Result<SmallBox<T, Space>, Self>[src]

Attempt to downcast the box to a concrete type.

Examples

#[macro_use]
extern crate smallbox;

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

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

impl<T: ?Sized + Sync, Space> Sync for SmallBox<T, Space>[src]

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

impl<T: Clone, Space> Clone for SmallBox<T, Space> where
    T: Sized
[src]

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

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

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

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

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

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

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

type Target = T

The resulting type after dereferencing.

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

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

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

Auto Trait Implementations

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: RefUnwindSafe + UnwindSafe

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

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]