Struct OwnedRooted

Source
pub struct OwnedRooted<T>
where T: GcRef,
{ /* private fields */ }
Available on crate features gc and runtime only.
Expand description

A rooted reference to a garbage-collected T with automatic lifetime.

An OwnedRooted<T> is a strong handle to a garbage-collected T, preventing its referent (and anything else transitively referenced) from being collected by the GC until [unroot][crate::OwnedRooted::unroot] is explicitly called.

An OwnedRooted<T> keeps its rooted GC object alive at least until the OwnedRooted<T> itself is dropped. The “de-registration” of the root is automatic and is triggered (in a deferred way) by the drop of this type.

The primary way to create an OwnedRooted<T> is to promote a temporary Rooted<T> into an OwnedRooted<T> via its to_owned_rooted method.

OwnedRooted<T> dereferences to its underlying T, allowing you to call T’s methods.

§Example

let mut store = Store::<Option<OwnedRooted<ExternRef>>>::default();

// Create our `OwnedRooted` in a nested scope to avoid rooting it for
// the duration of the store's lifetime.
let x = {
    let mut scope = RootScope::new(&mut store);
    let x = ExternRef::new(&mut scope, 1234)?;
    x.to_owned_rooted(&mut scope)?
};

// Place `x` into our store.
*store.data_mut() = Some(x);

// Do a bunch stuff that may or may not access, replace, or take `x`...

Implementations§

Source§

impl OwnedRooted<ArrayRef>

Source

pub fn to_anyref(self) -> OwnedRooted<AnyRef>

Upcast this arrayref into an anyref.

Source

pub fn to_eqref(self) -> OwnedRooted<EqRef>

Upcast this arrayref into an eqref.

Source§

impl OwnedRooted<EqRef>

Source

pub fn to_anyref(self) -> OwnedRooted<AnyRef>

Upcast this eqref into an anyref.

Source§

impl<T> OwnedRooted<T>
where T: GcRef,

Source

pub fn to_rooted(&self, context: impl AsContextMut) -> Rooted<T>

Clone this OwnedRooted<T> into a Rooted<T>.

This operation does not consume or unroot this OwnedRooted<T>.

The underlying GC object is re-rooted in the given context’s scope. The resulting Rooted<T> is only valid during the given context’s scope. See the Rooted<T> documentation for more details on rooting scopes.

This operation does not consume or unroot this OwnedRooted<T>.

§Panics

Panics if this object is not associated with the given context’s store.

§Example
let mut store = Store::<()>::default();

let root1: Rooted<_>;

let owned = {
    let mut scope = RootScope::new(&mut store);
    root1 = ExternRef::new(&mut scope, 1234)?;
    root1.to_owned_rooted(&mut scope)?
};

// `root1` is no longer accessible because it was unrooted when `scope`
// was dropped.
assert!(root1.data(&store).is_err());

// But we can re-root `owned` into this scope.
let root2 = owned.to_rooted(&mut store);
assert!(root2.data(&store).is_ok());
Source

pub fn ref_eq( store: impl AsContext, a: &impl RootedGcRef<T>, b: &impl RootedGcRef<T>, ) -> Result<bool>

Are these two GC roots referencing the same underlying GC object?

This function will return true even when a and b are different GC roots (for example because they were rooted in different scopes) if they are rooting the same underlying GC object.

Because this method takes any impl RootedGcRef<T> arguments, it can be used to compare, for example, a Rooted<T> and an OwnedRooted<T>.

§Panics

Panics if either a or b is not associated with the given store.

§Example
let mut store = Store::<()>::default();

let a;
let b;
let x;

{
    let mut scope = RootScope::new(&mut store);

    a = ExternRef::new(&mut scope, "hello")?.to_owned_rooted(&mut scope)?;
    b = a.clone();

    // `a` and `b` are rooting the same object.
    assert!(OwnedRooted::ref_eq(&scope, &a, &b)?);

    // `c` is a different GC root, is in a different scope, and is a
    // `Rooted<T>` instead of a `OwnedRooted<T>`, but is still rooting
    // the same object.
    let c = a.to_rooted(&mut scope);
    assert!(OwnedRooted::ref_eq(&scope, &a, &c)?);

    x = ExternRef::new(&mut scope, "goodbye")?.to_owned_rooted(&mut scope)?;

    // `a` and `x` are rooting different objects.
    assert!(!OwnedRooted::ref_eq(&scope, &a, &x)?);
}
Source

pub fn rooted_hash<H>(&self, state: &mut H)
where H: Hasher,

Hash this root.

Note that, similar to Rooted::rooted_eq, this only operates on the root and not the underlying GC reference. That means that two different rootings of the same object will hash to different values (modulo hash collisions). If this is undesirable, use the ref_hash method instead.

Source

pub fn ref_hash<H>(&self, store: impl AsContext, state: &mut H)
where H: Hasher,

Hash the underlying rooted object reference.

Note that, similar to Rooted::ref_eq, and operates on the underlying rooted GC object reference, not the root. That means that two different rootings of the same object will hash to the same value. If this is undesirable, use the rooted_hash method instead.

Source§

impl OwnedRooted<StructRef>

Source

pub fn to_anyref(self) -> OwnedRooted<AnyRef>

Upcast this structref into an anyref.

Source

pub fn to_eqref(self) -> OwnedRooted<EqRef>

Upcast this structref into an eqref.

Trait Implementations§

Source§

impl<T: GcRef> Clone for OwnedRooted<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: GcRef> Debug for OwnedRooted<T>

Source§

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

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

impl<T: GcRef> Deref for OwnedRooted<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl From<OwnedRooted<ArrayRef>> for OwnedRooted<AnyRef>

Source§

fn from(s: OwnedRooted<ArrayRef>) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedRooted<ArrayRef>> for OwnedRooted<EqRef>

Source§

fn from(s: OwnedRooted<ArrayRef>) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedRooted<EqRef>> for OwnedRooted<AnyRef>

Source§

fn from(e: OwnedRooted<EqRef>) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedRooted<StructRef>> for OwnedRooted<AnyRef>

Source§

fn from(s: OwnedRooted<StructRef>) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedRooted<StructRef>> for OwnedRooted<EqRef>

Source§

fn from(s: OwnedRooted<StructRef>) -> Self

Converts to this type from the input type.
Source§

impl WasmTy for OwnedRooted<AnyRef>

Source§

impl WasmTy for OwnedRooted<ArrayRef>

Source§

impl WasmTy for OwnedRooted<EqRef>

Source§

impl WasmTy for OwnedRooted<ExnRef>

Source§

impl WasmTy for OwnedRooted<ExternRef>

Source§

impl WasmTy for OwnedRooted<StructRef>

Auto Trait Implementations§

§

impl<T> Freeze for OwnedRooted<T>

§

impl<T> RefUnwindSafe for OwnedRooted<T>
where T: RefUnwindSafe,

§

impl<T> Send for OwnedRooted<T>
where T: Send,

§

impl<T> Sync for OwnedRooted<T>
where T: Sync,

§

impl<T> Unpin for OwnedRooted<T>
where T: Unpin,

§

impl<T> UnwindSafe for OwnedRooted<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WasmParams for T
where T: WasmTy,

Source§

type ValRawStorage = <(T,) as WasmParams>::ValRawStorage

Source§

fn typecheck( engine: &Engine, params: impl ExactSizeIterator<Item = ValType>, position: TypeCheckPosition, ) -> Result<(), Error>

Source§

fn vmgcref_pointing_to_object_count(&self) -> usize

Source§

fn store( self, store: &mut AutoAssertNoGc<'_>, func_ty: &FuncType, dst: &mut MaybeUninit<<T as WasmParams>::ValRawStorage>, ) -> Result<(), Error>

Source§

impl<T> WasmResults for T
where T: WasmTy,

Source§

unsafe fn load( store: &mut AutoAssertNoGc<'_>, abi: &<T as WasmParams>::ValRawStorage, ) -> T

Source§

impl<T> WasmRet for T
where T: WasmTy,

Source§

type Fallible = Result<T, Error>

Source§

fn compatible_with_store(&self, store: &StoreOpaque) -> bool

Source§

unsafe fn store( self, store: &mut AutoAssertNoGc<'_>, ptr: &mut [MaybeUninit<ValRaw>], ) -> Result<(), Error>

Source§

fn may_gc() -> bool

Source§

fn func_type(engine: &Engine, params: impl Iterator<Item = ValType>) -> FuncType

Source§

fn into_fallible(self) -> Result<T, Error>

Source§

fn fallible_from_error(error: Error) -> Result<T, Error>

Source§

impl<T, U> RootedGcRef<T> for U
where T: GcRef, U: RootedGcRefImpl<T> + Deref<Target = T>,