example/
example.rs

1extern crate maybe_box;
2
3use maybe_box::MaybeBox;
4
5fn main() {
6    // Wrap a bool into a MaybeBox.
7    // Because a bool is small enough to fit into the size of a pointer, this
8    // will not do any allocation.
9    let mb = MaybeBox::new(true);
10
11    // Extract the data back out again.
12    let my_bool = mb.into_inner();
13    assert_eq!(my_bool, true);
14
15    // Wrap a String into a MaybeBox
16    // Because a String is too big to fit into the size of a pointer, this
17    // *will* do allocation.
18    let mb = MaybeBox::new(String::from("hello"));
19
20    // We can unpack the MaybeBox to see whether it was boxed or not.
21    match mb.unpack() {
22        maybe_box::Unpacked::Inline(_) => panic!("String wasn't boxed?!"),
23        maybe_box::Unpacked::Boxed(b) => {
24            // Unbox our string...
25            let my_string = *b;
26
27            // ... and get the String that we boxed.
28            assert_eq!(&*my_string, "hello");
29        },
30    };
31}
32