Skip to main content

DynamicManaged

Struct DynamicManaged 

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

Owner of a value whose type is only known at runtime.

The Managed counterpart for script values: the value lives in its own allocation, identified by a TypeHash and destroyed through a stored drop function. Every typed access checks the hash first.

Implementations§

Source§

impl DynamicManaged

Source

pub fn new<T: Finalize>(data: T) -> Result<Self, T>

Moves a value into a new allocation, giving it back when the allocation fails.

Source

pub fn new_raw( type_hash: TypeHash, lifetime: Lifetime, memory: *mut u8, layout: Layout, finalizer: impl Into<Finalizer>, ) -> Option<Self>

Takes ownership of an existing allocation.

Returns None for a null pointer. The box will free memory and run finalizer on drop.

Source

pub fn new_uninitialized( type_hash: TypeHash, layout: Layout, finalizer: impl Into<Finalizer>, ) -> Self

Allocates room for a value without writing one into it.

Nothing stops the box from running its finalizer on drop, so the caller must fill the memory before it is dropped or read.

Source

pub unsafe fn from_bytes( type_hash: TypeHash, lifetime: Lifetime, bytes: Vec<u8>, layout: Layout, finalizer: impl Into<Finalizer>, ) -> Self

Builds a box by copying a byte image of a value into a new allocation.

§Safety

bytes must be a valid image of a value of the type named by type_hash, matching layout, and it is moved, so the caller must not drop the source afterwards.

Source

pub fn into_inner(self) -> (TypeHash, Lifetime, *mut u8, Layout, Finalizer)

Splits into the parts the box was built from, and stops it from freeing the allocation.

The caller becomes responsible for running the finalizer and freeing the memory.

Source

pub fn into_typed<T>(self) -> Result<Managed<T>, Self>

Moves the value into a typed box, giving self back on a type mismatch or while it is accessed.

Source

pub fn renew(self) -> Self

Replaces the lifetime, killing every handle taken so far.

Source

pub fn type_hash(&self) -> &TypeHash

Returns the type of the stored value.

Source

pub fn lifetime(&self) -> &Lifetime

Returns the borrow state of this value.

Source

pub fn layout(&self) -> &Layout

Returns the layout of the allocation.

Source

pub fn finalizer(&self) -> &Finalizer

Returns how the stored value is destroyed.

Source

pub unsafe fn memory(&self) -> &[u8]

Returns the value as raw bytes.

§Safety

Bypasses the borrow state, so the caller must know that nothing is writing the value.

Source

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

Returns the value as mutable raw bytes.

§Safety

Bypasses the borrow state, and writing bytes that are not a valid value of the stored type makes every later access undefined.

Source

pub fn is<T>(&self) -> bool

Returns true when the stored type is T.

Source

pub fn read<T>(&self) -> Option<ValueReadAccess<'_, T>>

Guards the value for reading, or returns None on a type mismatch or while it is written.

Source

pub fn write<T>(&mut self) -> Option<ValueWriteAccess<'_, T>>

Guards the value for writing, or returns None on a type mismatch or while it is accessed.

Source

pub fn consume<T>(self) -> Result<T, Self>

Takes the value out and frees the allocation.

Gives the box back on a type mismatch or while any access guard is live.

Source

pub fn move_into_ref(self, target: DynamicManagedRefMut) -> Result<(), Self>

Moves the value into the place target points at and frees this allocation.

Gives the box back when the types differ or both sides are the same allocation.

Source

pub fn move_into_lazy(self, target: DynamicManagedLazy) -> Result<(), Self>

Moves the value into the place target points at and frees this allocation.

Gives the box back when the types differ or both sides are the same allocation.

Source

pub fn borrow(&self) -> Option<DynamicManagedRef>

Takes a shared handle, or returns None when an exclusive one is out.

Source

pub fn borrow_mut(&mut self) -> Option<DynamicManagedRefMut>

Takes an exclusive handle, or returns None when any handle is out.

Source

pub fn lazy(&self) -> DynamicManagedLazy

Takes an unclaimed handle.

Source

pub unsafe fn map<T, U: Finalize>(self, f: impl FnOnce(T) -> U) -> Option<Self>

Replaces the value with one built from it, in a new allocation.

§Safety

Handles taken so far are not checked before the value is moved out, and they go dead. Returns None when the stored type is not T.

Source

pub unsafe fn try_map<T, U: Finalize>( self, f: impl FnOnce(T) -> Option<U>, ) -> Option<Self>

DynamicManaged::map that can decline, dropping the value when it does.

§Safety

Same as DynamicManaged::map.

Source

pub unsafe fn as_ptr<T>(&self) -> Option<*const T>

Returns a typed pointer while nothing is accessing the value.

§Safety

The caller takes over checking for conflicting access.

Source

pub unsafe fn as_mut_ptr<T>(&mut self) -> Option<*mut T>

Returns a typed mutable pointer while nothing is accessing the value.

§Safety

The caller takes over checking for conflicting access.

Source

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

Returns the allocation pointer, checking nothing at all.

§Safety

Neither the type nor the borrow state is checked.

Source

pub unsafe fn as_mut_ptr_raw(&mut self) -> *mut u8

Returns the mutable allocation pointer, checking nothing at all.

§Safety

Neither the type nor the borrow state is checked.

Trait Implementations§

Source§

impl Drop for DynamicManaged

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
Source§

impl From<DynamicManaged> for DynamicManagedValue

Source§

fn from(value: DynamicManaged) -> Self

Converts to this type from the input type.
Source§

impl Send for DynamicManaged

Source§

impl Sync for DynamicManaged

Source§

impl TryFrom<DynamicManagedValue> for DynamicManaged

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: DynamicManagedValue) -> Result<Self, Self::Error>

Performs the conversion.

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.