pub struct MaybeBox<T> { /* private fields */ }Expand description
Hold a value of type T in the space for a usize, only boxing it if necessary.
This can be a useful optimization when dealing with C APIs that allow you to pass around some
arbitrary void *-sized piece of data.
This type is guranteed to be the same size as a usize.
Implementations§
Source§impl<T> MaybeBox<T>
impl<T> MaybeBox<T>
Sourcepub fn new(t: T) -> MaybeBox<T>
pub fn new(t: T) -> MaybeBox<T>
Wrap a T into a MaybeBox<T>. This will allocate if
size_of::<T>() > size_of::<usize>().
Examples found in repository?
examples/example.rs (line 9)
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}Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the MaybeBox<T> and return the inner T.
Examples found in repository?
examples/example.rs (line 12)
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}Sourcepub fn unpack(self) -> Unpacked<T>
pub fn unpack(self) -> Unpacked<T>
Consume the MaybeBox<T> and return the inner T, possibly boxed (if
it was already).
This may be more efficient than calling into_inner and then boxing
the returned value.
Examples found in repository?
examples/example.rs (line 21)
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}Trait Implementations§
impl<T: Eq> Eq for MaybeBox<T>
Auto Trait Implementations§
impl<T> Freeze for MaybeBox<T>
impl<T> RefUnwindSafe for MaybeBox<T>where
T: RefUnwindSafe,
impl<T> Send for MaybeBox<T>where
T: Send,
impl<T> Sync for MaybeBox<T>where
T: Sync,
impl<T> Unpin for MaybeBox<T>where
T: Unpin,
impl<T> UnwindSafe for MaybeBox<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more