smallbox
Box dynamically-sized types on stack. Requires nightly rust.
Store or return trait-object and closure without heap allocation, and fallback to heap when thing goes too large.
Documentation
Usage
First, add the following to your Cargo.toml:
[]
= "0.2"
Next, add this to your crate root:
extern crate smallbox;
Overview
This crate delivers two core type:
StackBox<T>: Represents a fixed-capacity allocation, and on stack stores dynamically-sized type. The new method on this type allows creating a instance from a concrete type, returning Err(value) if the instance is too large for the allocated region. So far, the fixed-capcity is about four words (4 * sizeof(usize))
SmallBox<T>: Takes StackBox<T> as an varience, and fallback to Box<T> when type T is too large for StackBox<T>.
Example
One of the most obvious uses is to allow returning capturing closures without having to box them.
use StackBox;
let closure = make_closure;
assert_eq!;
The other uses is to eliminate heap alloction for small things, only when the object is large enough to allocte. In addition, the inner StackBox<T> or Box<T> can be moved out by explicitely pattern matching on SmallBox<T>.
use SmallBox;
let tiny: = new;
let big: = new;
assert_eq!;
assert_eq!;
match tiny
match big
Roadmap
no_stdsupport- impl
Debug,Display - method that convert SmallBox to Box
- conveniently convert bewteen
SmallBox<T>andStackBox<T> - optional
SmallBox<T>and heap dependency - configurable
StackBox<T>allocation size - dowancasting for
StackBox<Any>andSmallBox<Any>
Contribution
All kinds of contribution are welcome.
- Issue Feel free to open an issue when you find typos, bugs, or have any question.
- Pull requests. Better implementation, more tests, more documents and typo fixes are all welcome.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.