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

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

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

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

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

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

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

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

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

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

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

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

impl<T: ?Sized, Space> DerefMut 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 + Hash, Space> Hash for SmallBox<T, Space>[src]

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

Feeds a slice of this type into the given [Hasher]. Read more

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

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

Blanket Implementations

impl<T> From for T[src]

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

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

type Owned = T

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

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

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

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

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.