Skip to main content

Object

Struct Object 

Source
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

Source

pub fn new(handle: Arc<Type>) -> Object

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.

Source

pub fn try_new(handle: Arc<Type>) -> Option<Object>

Object::new that returns None instead of panicking.

Source

pub unsafe fn new_uninitialized(handle: Arc<Type>) -> Option<Object>

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.

Source

pub unsafe fn new_raw(handle: Arc<Type>, memory: *mut u8) -> Object

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.

Source

pub unsafe fn from_bytes(handle: Arc<Type>, bytes: &[u8]) -> Option<Object>

Allocates a value by copying a byte image of one.

Returns None when bytes is not exactly the size of the type.

§Safety

bytes must be a valid image of a value of this type, and it is moved, so the caller must not drop the source afterwards.

Source

pub fn with_value<T>(handle: Arc<Type>, value: T) -> Option<Object>
where T: 'static,

Moves a Rust value into an object, or returns None when handle does not describe T.

Source

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.

Source

pub fn consume<T>(self) -> Result<T, Object>
where T: 'static,

Moves the value out as a Rust value, or gives the object back on a type mismatch.

Source

pub unsafe fn into_inner(self) -> (Arc<Type>, *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.

Source

pub fn type_handle(&self) -> &Arc<Type>

Returns the type of the stored value.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn read<T>(&self) -> Option<&T>
where T: 'static,

Borrows the value as a T, or returns None on a type mismatch.

Source

pub fn write<T>(&mut self) -> Option<&mut T>
where T: 'static,

Borrows the value mutably as a T, or returns None on a type mismatch.

Source

pub fn read_field<'a, T>(&'a self, field: &str) -> Option<&'a T>
where T: 'static,

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.

Source

pub fn write_field<'a, T>(&'a mut self, field: &str) -> Option<&'a mut T>
where T: 'static,

Source

pub unsafe fn as_ptr(&self) -> *const u8

Returns the allocation pointer.

§Safety

Nothing is checked. The caller takes over both typing and aliasing.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Object

Source§

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

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

impl Drop for Object

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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> Finalize for T

Source§

unsafe fn finalize_raw(data: *mut ())

Drops the value stored at data in place. 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.