Skip to content

Commit

Permalink
Added more conversion functions for AnyRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Aug 9, 2024
1 parent 0b3cdc3 commit 0225728
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,50 @@ impl AnyRoot {
}
}

/// Returns a [`Root<T>`] if the underlying reference points to a `T`.
pub fn downcast_root<T>(&self) -> Option<Root<T>>
where
T: Collectable,
{
if TypeIndex::of::<T>() == self.any.type_index {
// SAFETY: `self` has a root reference to the underlying data,
// ensuring that it cannot be collected while `self` exists. We've
// verified that `T` is the same underlying type. We can return a
// reference bound to `self`'s lifetime safely.
let rooted = unsafe { &*self.rooted.cast::<Rooted<T>>() };

// Increment the strong count for the returned root.
rooted.roots.fetch_add(1, Ordering::Relaxed);

Some(Root {
data: rooted,
reference: self.downcast_ref(),
})
} else {
None
}
}

/// Returns a [`Ref<T>`].
///
/// This function does not do any type checking. If `T` is not the correct
/// type, attempting to load the underyling value will fail.
pub const fn downcast_ref<T>(&self) -> Ref<T>
where
T: Collectable,
{
self.any.downcast_ref()
}

/// Returns a [`Ref<T>`], if `T` matches the type of this reference.
#[must_use]
pub fn downcast_checked<T>(&self) -> Option<Ref<T>>
where
T: Collectable,
{
self.any.downcast_checked()
}

/// Returns an untyped "weak" reference to this root.
pub const fn as_any(&self) -> AnyRef {
self.any
Expand Down Expand Up @@ -2923,10 +2967,7 @@ impl AnyRef {
.load_root(self.bin_id, self.slot_generation, guard)
}

/// Returns a [`Ref<T>`].
///
/// This function does not do any type checking. If `T` is not the correct
/// type, attempting to load the underyling value will fail.
/// Returns a [`Ref<T>`], if `T` matches the type of this reference.
#[must_use]
pub fn downcast_checked<T>(&self) -> Option<Ref<T>>
where
Expand Down

0 comments on commit 0225728

Please sign in to comment.