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
impl Anon
Sourcepub fn from_ptr(ptr: *const u8, size: usize, typeid: TypeId) -> Self
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.
Sourcepub fn uninit() -> Self
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.
Sourcepub fn init<T>(&mut self, val: T)where
T: Any + 'static,
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.
pub fn inner(self) -> Vec<u8> ⓘ
Sourcepub fn typeid(&self) -> TypeId
pub fn typeid(&self) -> TypeId
Returns the TypeId of the types this anon stores.
Will return TypeId::of::<i32>()
if uninit.
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
Get a mutable slice that points to the inner value.
Sourcepub fn cast_ref<T>(&self) -> &Twhere
T: Any + 'static,
pub fn cast_ref<T>(&self) -> &Twhere
T: Any + 'static,
Cast the inner value to T.
Inernally, the inner value is Vec
§Usage
use anon_vec::Anon;
let x: i32 = 5;
let anon = Anon::new(x);
let v: &i32 = anon.cast_ref::<i32>();
Sourcepub fn cast_mut<T>(&mut self) -> &mut Twhere
T: Any + 'static,
pub fn cast_mut<T>(&mut self) -> &mut Twhere
T: Any + 'static,
Cast the inner value to T.
Inernally, the inner value is Vec
§Usage
use anon_vec::Anon;
let x: i32 = 5;
let mut anon = Anon::new(x);
let v: &mut i32 = anon.cast_mut::<i32>();