Skip to main content

Crate smox

Crate smox 

Source
Expand description

A Box like type that keeps small objects on the stack and can allocate bigger objects as Box, Rc or Arc. DerefMut is copy-on-write when the Smox is backed by Rc or Arc

§Rationale

In generic contexts one often does not know how big the objects will be. If they are small enough they can be kept on the stack for better performance. If they are bigger they need to be allocated on the heap. Smox allows to do this in a transparent way.

§Example

use smox::*;

// For the copy-on-write behavior we need T: Clone
#[derive(Clone)]
struct Generic<T: Clone> {
    value: SmoxRc<T, 16>,
}

impl<T: Clone> Generic<T> {
    fn new(value: T) -> Self {
        Self {
            value: SmoxRc::new_rc(value),
        }
    }
}

fn main() {
    // we have the guarantee that Generic<T> will always be at most 16 bytes large.
    assert!(std::mem::size_of::<Generic<i32>>() <= 16);
    assert!(std::mem::size_of::<Generic<[i32; 100]>>() <= 16);

    let mut small = Generic::new(42i32);
    *small.value += 1;
    assert_eq!(*small.value, 43);

    let big = Generic::new([0i32;100]);
    // Cheap cloning the underlying Rc
    let mut big_clone = big.clone();
    // Modifying big_clone will trigger a copy-on-write
    big_clone.value[0] = 1;

    assert_eq!(big.value[0], 0);
    assert_eq!(big_clone.value[0], 1);
}

Structs§

AsArc
Tags the underlying HeapStorage as Arc<T>
AsBox
Tags the underlying HeapStorage as Box<T>
AsRc
Tags the underlying HeapStorage as Rc<T>

Traits§

HeapStorage
Marker trait for heap storage types.

Functions§

optimal_size
Returns the optimal size for in-place storage of T given a size limit SIZE.

Type Aliases§

SmoxArc
Type alias for Smox using Arc as the heap storage type.
SmoxBox
Type alias for Smox using Box as the heap storage type.
SmoxRc
Type alias for Smox using Rc as the heap storage type.

Unions§

Smox
A smart pointer that stores T either in-place (if it fits within SIZE bytes) or on the heap using the specified HeapStorage type (Box, Rc, or Arc).