Skip to content

Like `Box`, but with an optimization that avoids allocations for small data-structures

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

HellButcher/tinybox-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinybox

Crates.io docs.rs license: MIT/Apache-2.0 Rust CI

TinyBox is like Box, but with an optimization that avoids allocations for small data-structures. This works by storing the value-bits inside the box itself, when the data-structure fits inside a pointer. This is especially usefull for dynamically sized types like traits.

Example

This example stores a value inside the TinyBox without requireing an allocation.

use tinybox::TinyBox;
let boxed = TinyBox::new(1234usize);
assert_eq!(1234, *boxed)

This looks not very usefull, because the value can be stored inside a usize variable without TinyBox. Here is an more useful example that uses dyn-traits. The tinybox! macro is used to coerce the value to a dyn-trait in stable-rust.

use std::any::{Any,TypeId};
use tinybox::{tinybox, TinyBox};
let boxed: TinyBox<dyn Any> = tinybox!(dyn Any => 1234usize);
assert_eq!(TypeId::of::<usize>(), (*boxed).type_id());
assert_eq!(1234, *boxed.downcast::<usize>().unwrap());

no_std

This crate should also work without std. No additional configuration required.

License

This project is licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Like `Box`, but with an optimization that avoids allocations for small data-structures

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages