Struct Anon

Source
pub struct Anon { /* private fields */ }
Expand description

§Anonymous Type

Emulates dynamic typing for efficient, albeit unsafe, data storage.

Internally, Anon is a Vec<u8>. It works by casting a type to a slice and adding it to the vector. The inner value can then be downcasted to either a &mut T or a &T.

§Safety Disclaimer

Use of Anon is inherently unsafe! Attempting to access and modify the data CAN cause major problems. Be careful!

Any structs that are allocated to Anon should have #[repr(C)] to ensure consistent alignment. Failure to do so may cause the data to be come garbled.

§Usage

Anon provides methods for construction either in-place with a value or uninit for later initialization.

use anon_vec::Anon;
 
// in-place construction
let x: i32 = 5;
let mut anon = Anon::new::<i32>(x);
 
// access the inner value with `cast_ref` and `cast_mut`
let v: &i32 = anon.cast_ref::<i32>();
let v: &mut i32 = anon.cast_mut::<i32>();
 
// uninit construction, to be initialized later. 
let mut anon = Anon::uninit();
 

Implementations§

Source§

impl Anon

Source

pub fn new<T>(val: T) -> Self
where T: Any + 'static,

Creates a new Anonymous Type in-place.

§Usage
use anon_vec::Anon;
 
let x: i32 = 5;
let anon = Anon::new::<i32>(x);
§Memory Safety

Anon will consume the value on Anon::init or Anon::new, so the value will inherit the lifetime of Anon, allowing you to store Vec<T> as anon safely.

Source

pub fn from_ptr(ptr: *const u8, size: usize, typeid: TypeId) -> Self

Creates a new Anonymous Type in-place from a *const u8 which represents T.

Source

pub fn uninit() -> Self

Creates a new, uninitialized Anonymous Type.

§Usage
use anon_vec::Anon;
 
// declare the wrapper
let mut anon = Anon::uninit();
 
// initialize later
let x: i32 = 5;
anon.init::<i32>(x);
§Memory Safety

Anon will consume the value on Anon::init or Anon::new, so the value will inherit the lifetime of Anon, allowing you to store Vec<T> as anon safely.

If you try and access an uninitialized Anon, you will access memory incorrectly.

Source

pub fn init<T>(&mut self, val: T)
where T: Any + 'static,

Initializes an Anon created with Anon::uninit().

§Usage
use anon_vec::Anon;
 
// declare the wrapper
let mut anon = Anon::uninit();
 
// initialize later
let x: i32 = 5;
anon.init::<i32>(x);
§Memory Safety

Anon will consume the value on Anon::init or Anon::new, so the value will inherit the lifetime of Anon, allowing you to store Vec<T> as anon safely.

Source

pub fn inner(self) -> Vec<u8>

Source

pub fn size(&self) -> usize

The Size of this Anon, in bytes.

Source

pub fn typeid(&self) -> TypeId

Returns the TypeId of the types this anon stores. Will return TypeId::of::<i32>() if uninit.

Source

pub fn as_slice(&self) -> &[u8]

Get a slice that points to the inner value.

Source

pub fn as_mut_slice(&mut self) -> &mut [u8]

Get a mutable slice that points to the inner value.

Source

pub fn is_uninit(&self) -> bool

Check whether or not the inner value is empty, or uninit.

Source

pub fn cast_ref<T>(&self) -> &T
where T: Any + 'static,

Cast the inner value to T.

Inernally, the inner value is Vec. To access, the *mut u8 inside the vec is cast to *const T and returned.

§Usage
use anon_vec::Anon;
 
let x: i32 = 5;
let anon = Anon::new(x);
 
let v: &i32 = anon.cast_ref::<i32>();
Source

pub fn cast_mut<T>(&mut self) -> &mut T
where T: Any + 'static,

Cast the inner value to T.

Inernally, the inner value is Vec. To access, the *mut u8 inside the vec is cast to *mut T and returned.

§Usage
use anon_vec::Anon;
 
let x: i32 = 5;
let mut anon = Anon::new(x);
 
let v: &mut i32 = anon.cast_mut::<i32>();
Source

pub fn consume<T>(self) -> T
where T: Any + Clone + 'static,

Consume the Anon, returning the inner value as T.

§Usage
use anon_vec::Anon;
let x: i32 = 5;
let mut anon = Anon::new(x);
 
let v: i32 = anon.consume::<i32>();

Auto Trait Implementations§

§

impl Freeze for Anon

§

impl RefUnwindSafe for Anon

§

impl Send for Anon

§

impl Sync for Anon

§

impl Unpin for Anon

§

impl UnwindSafe for Anon

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<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, 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.