pub struct Object { /* private fields */ }Expand description
A value of any registered type, held in its own allocation.
Construction and destruction go through the TypeHandle: a native type
runs the Rust initializer and destructor, a runtime type walks its fields
and does each one in turn.
Typed access is checked, so Object::read returns None unless the
object really holds that Rust type.
Implementations§
Source§impl Object
impl Object
Sourcepub fn new(handle: TypeHandle) -> Self
pub fn new(handle: TypeHandle) -> Self
Allocates a default value of the given type.
§Panics
Panics when the type has no way to create a default value. Use
Object::try_new to get None instead.
Sourcepub fn try_new(handle: TypeHandle) -> Option<Self>
pub fn try_new(handle: TypeHandle) -> Option<Self>
Object::new that returns None instead of panicking.
Sourcepub unsafe fn new_uninitialized(handle: TypeHandle) -> Option<Self>
pub unsafe fn new_uninitialized(handle: TypeHandle) -> Option<Self>
Allocates room for a value without creating one.
§Safety
The memory holds garbage. It must be filled before the object is read or dropped, since dropping runs the type’s destructor over whatever is there.
Sourcepub unsafe fn new_raw(handle: TypeHandle, memory: *mut u8) -> Self
pub unsafe fn new_raw(handle: TypeHandle, memory: *mut u8) -> Self
Takes ownership of an existing allocation.
§Safety
memory must hold an initialized value of the type, allocated so that
it can be freed with the type’s layout. The object frees it on drop.
Sourcepub unsafe fn from_bytes(handle: TypeHandle, bytes: &[u8]) -> Option<Self>
pub unsafe fn from_bytes(handle: TypeHandle, bytes: &[u8]) -> Option<Self>
Sourcepub fn with_value<T: 'static>(handle: TypeHandle, value: T) -> Option<Self>
pub fn with_value<T: 'static>(handle: TypeHandle, value: T) -> Option<Self>
Moves a Rust value into an object, or returns None when handle does
not describe T.
Sourcepub unsafe fn initialize(&mut self)
pub unsafe fn initialize(&mut self)
Writes a default value into this object’s memory.
§Safety
The memory must not already hold a value, since the old one is not dropped first.
Sourcepub fn consume<T: 'static>(self) -> Result<T, Self>
pub fn consume<T: 'static>(self) -> Result<T, Self>
Moves the value out as a Rust value, or gives the object back on a type mismatch.
Sourcepub unsafe fn into_inner(self) -> (TypeHandle, *mut u8)
pub unsafe fn into_inner(self) -> (TypeHandle, *mut u8)
Splits into the type handle and the allocation, and stops the object from freeing it.
§Safety
The caller becomes responsible for destroying the value and freeing the memory with the type’s layout.
Sourcepub fn type_handle(&self) -> &TypeHandle
pub fn type_handle(&self) -> &TypeHandle
Returns the type of the stored value.
Sourcepub unsafe fn memory(&self) -> &[u8] ⓘ
pub unsafe fn memory(&self) -> &[u8] ⓘ
Returns the value as raw bytes.
§Safety
Reading the bytes of a type that owns resources, or keeping them past the object’s life, is on the caller.
Sourcepub unsafe fn memory_mut(&mut self) -> &mut [u8] ⓘ
pub unsafe fn memory_mut(&mut self) -> &mut [u8] ⓘ
Returns the value as mutable raw bytes.
§Safety
Writing bytes that are not a valid value of the stored type makes every later read, and the eventual drop, undefined.
Sourcepub unsafe fn field_memory<'a>(
&'a self,
query: StructFieldQuery<'a>,
) -> Option<&'a [u8]>
pub unsafe fn field_memory<'a>( &'a self, query: StructFieldQuery<'a>, ) -> Option<&'a [u8]>
Returns the bytes of one field, chosen by query.
For an enum, the field is looked up in the variant the value currently holds.
§Safety
Same conditions as Object::memory.
Sourcepub unsafe fn field_memory_mut<'a>(
&'a mut self,
query: StructFieldQuery<'a>,
) -> Option<&'a mut [u8]>
pub unsafe fn field_memory_mut<'a>( &'a mut self, query: StructFieldQuery<'a>, ) -> Option<&'a mut [u8]>
Returns the mutable bytes of one field, chosen by query.
For an enum, the field is looked up in the variant the value currently holds.
§Safety
Same conditions as Object::memory_mut.
Sourcepub fn read<T: 'static>(&self) -> Option<&T>
pub fn read<T: 'static>(&self) -> Option<&T>
Borrows the value as a T, or returns None on a type mismatch.
Sourcepub fn write<T: 'static>(&mut self) -> Option<&mut T>
pub fn write<T: 'static>(&mut self) -> Option<&mut T>
Borrows the value mutably as a T, or returns None on a type
mismatch.
Sourcepub fn read_field<'a, T: 'static>(&'a self, field: &str) -> Option<&'a T>
pub fn read_field<'a, T: 'static>(&'a self, field: &str) -> Option<&'a T>
Borrows one field by name, or returns None when there is no such
field of that type.
This is how a runtime type’s fields are reached, since there is no Rust struct to go through.
Sourcepub fn write_field<'a, T: 'static>(
&'a mut self,
field: &str,
) -> Option<&'a mut T>
pub fn write_field<'a, T: 'static>( &'a mut self, field: &str, ) -> Option<&'a mut T>
Mutable Object::read_field.
Sourcepub unsafe fn as_ptr(&self) -> *const u8
pub unsafe fn as_ptr(&self) -> *const u8
Returns the allocation pointer.
§Safety
Nothing is checked. The caller takes over both typing and aliasing.
Sourcepub unsafe fn as_mut_ptr(&mut self) -> *mut u8
pub unsafe fn as_mut_ptr(&mut self) -> *mut u8
Returns the mutable allocation pointer.
§Safety
Nothing is checked. The caller takes over both typing and aliasing.
Sourcepub unsafe fn prevent_drop(&mut self)
pub unsafe fn prevent_drop(&mut self)
Stops this object from destroying its value when it is dropped.
§Safety
The value leaks unless its ownership was already handed to someone else.