MaybeBox

Struct MaybeBox 

Source
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>

Source

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}
Source

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}
Source

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§

Source§

impl<T: Debug> Debug for MaybeBox<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for MaybeBox<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for MaybeBox<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T> Drop for MaybeBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<T> for MaybeBox<T>

Source§

fn from(t: T) -> MaybeBox<T>

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for MaybeBox<T>

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<U, T: PartialEq<U>> PartialEq<MaybeBox<U>> for MaybeBox<T>

Source§

fn eq(&self, other: &MaybeBox<U>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &MaybeBox<U>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.