Struct TaggedBox

Source
pub struct TaggedBox<T> { /* private fields */ }
Expand description

A tagged box, associated with a variable type (enum, integer, etc.) able to be extracted from the underlying TaggedPointer

Implementations§

Source§

impl<T> TaggedBox<T>

Source

pub fn new<U>(val: U, discriminant: Discriminant) -> Self

Creates a new TaggedBox from a value and its discriminant

§Examples
enum Communication {
    Message(String),
    Letter(Vec<Paper>),
}

let tagged_message: TaggedBox<Communication> = TaggedBox::new(String::from("Foobar is a timeless classic"), 0);
§Make sure to retrieve the correct type from the TaggedBox, or you will encounter undefined behavior!

This is alright, because we stored a String onto the heap and we are retrieving a String with into_inner

unsafe {
    let message = TaggedBox::into_inner::<String>(tagged_message);

    assert_eq!(&message, "Foobar is a timeless classic");
}

This is UB, because we stored a String onto the heap and are retreving a Vec<Paper

unsafe {
    let letter = TaggedBox::into_inner::<Vec<Paper>>(tagged_message); // UB!
}
Source

pub unsafe fn as_ref<U>(&self) -> &U

Returns an immutable reference to the value stored on the heap

§Safety

The type provided as U must be the same type as allocated by new, and any actions done with the value must not move it

§Example
enum Bricks {
    Red(usize),
}

let red_brick: TaggedBox<Bricks> = TaggedBox::new(100_usize, 0);

unsafe {
    // We allocated a usize, so we can retrieve one
    assert_eq!(red_brick.as_ref::<usize>(), &100);
}
Source

pub unsafe fn as_mut_ref<U>(&mut self) -> &mut U

Returns an immutable reference to the value stored on the heap

§Safety

The type provided as U must be the same type as allocated by new, and any actions done with the value must not move it

§Example
enum Bricks {
    Red(usize),
}

let mut red_brick: TaggedBox<Bricks> = TaggedBox::new(100_usize, 0);

unsafe {
    assert_eq!(red_brick.as_ref::<usize>(), &100);

    // We allocated a usize, so we can retrieve one and change it
    *red_brick.as_mut_ref::<usize>() = 300;

    assert_eq!(red_brick.as_ref::<usize>(), &300);
}
Source

pub unsafe fn into_inner<U>(tagged: Self) -> U

Return the boxed value contained in the TaggedPointer

§Safety

The type provided as U must be the same type as allocated by new

§Example
enum Bricks {
    Red(usize),
}

let red_brick: TaggedBox<Bricks> = TaggedBox::new(100_usize, 0);

unsafe {
    // We allocated a usize, so we can retrieve one
    let mut number_bricks: usize = TaggedBox::into_inner(red_brick);

    assert_eq!(number_bricks, 100);
}
Source

pub fn into_raw<U>(tagged: Self) -> *mut U

Consumes the TaggedBox, returning a raw pointer.

The pointer will be properly aligned and non-null, and the caller is responsible for managing the memory allocated by TaggedBox.

§Example
let tagged_box: TaggedBox<InnerValue> = TaggedBox::new([10u8; 10], 8);

// Get the raw pointer to the heap-allocated value
let raw: *mut [u8; 10] = TaggedBox::into_raw(tagged_box);

unsafe {
    assert_eq!(*raw, [10; 10]);
}
Source

pub unsafe fn into_box<U>(tagged: Self) -> Box<U>

Creates a Box from the provided TaggedBox
Trusts that the type provided as U is valid for the allocated layout.

§Safety

The type provided as U must be the same type that the instance of TaggedBox was initialized with.

§Example
let tagged_box: TaggedBox<InnerValue> = TaggedBox::new([10u8; 10], 8);

unsafe {
    // Get the Boxed value
    let boxed: Box<[u8; 10]> = TaggedBox::into_box(tagged_box);
}
Source

pub unsafe fn from_raw<U>(raw: *mut U, discriminant: Discriminant) -> Self

Constructs a TaggedBox from a raw pointer and a discriminant.

Trusts that the provided pointer is valid and non-null, as well as that the memory allocated is the method same as allocated by TaggedBox. TaggedBox uses the standard allocator

§Safety

This function is unsafe because improper use may lead to memory problems. For example, a double-free may occur if the function is called twice on the same raw pointer.

§Example
let tagged_box: TaggedBox<InnerValue> = TaggedBox::new(vec![100_usize, 200, 300], 10);

// Turn the tagged box into a raw pointer and its discriminant
let discriminant = tagged_box.discriminant();
let raw_box: *mut Vec<usize> = TaggedBox::into_raw(tagged_box);

unsafe {
    assert_eq!(*raw_box, vec![100, 200, 300]);

    let tagged_box: TaggedBox<InnerValue> = TaggedBox::from_raw(raw_box, discriminant);
     
    assert_eq!(TaggedBox::into_inner::<Vec<usize>>(tagged_box), vec![100, 200, 300]);
}
Source

pub const fn discriminant(&self) -> Discriminant

Fetches the discriminant of a TaggedBox

§Examples
let tagged_box: TaggedBox<InnerValue> = TaggedBox::new(0x00, 11);

assert_eq!(tagged_box.discriminant(), 11);
Source

pub const fn as_ptr<U>(&self) -> *const U

Retrieves a raw pointer to the data owned by TaggedBox, see TaggedPointer::as_ptr
The caller must ensure that the returned pointer is never written to. If you need to mutate the contents of the tagged pointer, use as_mut_ptr.

enum Bricks {
    Red(usize),
}

let red_brick: TaggedBox<Bricks> = TaggedBox::new(100_usize, 0);

unsafe {
    assert_eq!(*red_brick.as_ptr::<usize>(), 100);
}
Source

pub fn as_mut_ptr<U>(&mut self) -> *mut U

Retrieves a raw pointer to the data owned by TaggedBox, see TaggedPointer::as_mut_ptr
It is your responsibility to make sure that the string slice only gets modified in a way that it remains valid

§Example
enum Bricks {
    Red(usize),
}

let mut red_brick: TaggedBox<Bricks> = TaggedBox::new(100_usize, 0);

unsafe {
    *red_brick.as_mut_ptr::<usize>() = 100_000;

    assert_eq!(TaggedBox::into_inner::<usize>(red_brick), 100_000);
}

Trait Implementations§

Source§

impl<T: TaggableInner> Binary for TaggedBox<T>

Source§

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

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

impl<T> Clone for TaggedBox<T>
where T: TaggableInner + Clone,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for TaggedBox<T>
where T: TaggableInner + Debug + Clone,

Source§

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

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

impl<T> Display for TaggedBox<T>

Source§

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

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

impl<T: TaggableInner> LowerHex for TaggedBox<T>

Source§

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

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

impl<T: TaggableInner> Octal for TaggedBox<T>

Source§

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

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

impl<T> Ord for TaggedBox<T>
where T: TaggableInner + Ord,

Source§

fn cmp(&self, other: &TaggedBox<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for TaggedBox<T>
where T: TaggableInner + PartialEq<T>,

Source§

fn eq(&self, other: &TaggedBox<T>) -> bool

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

fn ne(&self, other: &Rhs) -> bool

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

impl<T> PartialOrd for TaggedBox<T>
where T: TaggableInner + PartialOrd<T>,

Source§

fn partial_cmp(&self, other: &TaggedBox<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: TaggableInner> UpperHex for TaggedBox<T>

Source§

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

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

impl<T> Copy for TaggedBox<T>
where T: TaggableInner + Copy,

Source§

impl<T> Eq for TaggedBox<T>
where T: TaggableInner + Eq,

Auto Trait Implementations§

§

impl<T> Freeze for TaggedBox<T>

§

impl<T> RefUnwindSafe for TaggedBox<T>
where T: RefUnwindSafe,

§

impl<T> Send for TaggedBox<T>
where T: Send,

§

impl<T> Sync for TaggedBox<T>
where T: Sync,

§

impl<T> Unpin for TaggedBox<T>
where T: Unpin,

§

impl<T> UnwindSafe for TaggedBox<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.