pub struct Root<T>where
T: Collectable,{ /* private fields */ }Expand description
A root reference to a T that has been allocated in the garbage collector.
This type behaves very similarly to Arc<T>. It implements Deref<Target = T>, and it is also cheap-to-clone, utilizing atomic reference counting to
track the number of root references currently exist to the underlying value.
While any root references exist for a given allocation, the garbage collector will not collect the allocation.
Implementations§
source§impl<T> Root<T>where
T: Collectable,
impl<T> Root<T>where
T: Collectable,
sourcepub fn new<'a>(value: T, guard: impl AsRef<CollectionGuard<'a>>) -> Self
pub fn new<'a>(value: T, guard: impl AsRef<CollectionGuard<'a>>) -> Self
Stores value in the garbage collector, returning a root reference to
the data.
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
let mut guard = CollectionGuard::acquire();
let message = Ref::new(String::from("Hello!"), &guard);
let error = Root::new(Error { message }, &guard);
// Because `error` is a Root and refers to the message,
guard.collect();
assert_eq!(message.load(&guard).expect("still alive"), "Hello!");
// After we drop the Root, the message will be able to be collected.
drop(error);
guard.collect();
assert_eq!(message.load(&guard), None);
}More examples
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let guard = CollectionGuard::acquire();
// Allocate a vec![Ref(1), Ref(2), Ref(3)].
let values: Vec<Ref<u32>> = (1..=3).map(|value| Ref::new(value, &guard)).collect();
let values = Root::new(values, &guard);
drop(guard);
// Manually execute the garbage collector. Our data will not be freed,
// since `values` is a "root" reference.
refuse::collect();
// Root references allow direct access to their data, even when a
// `CollectionGuard` isn't held.
let (one, two, three) = (values[0], values[1], values[2]);
// Accessing the data contained in a `Ref` requires a guard, however.
let mut guard = CollectionGuard::acquire();
assert_eq!(one.load(&guard), Some(&1));
assert_eq!(two.load(&guard), Some(&2));
assert_eq!(three.load(&guard), Some(&3));
// Dropping our root will allow the collector to free our `Ref`s.
drop(values);
guard.collect();
assert_eq!(one.load(&guard), None);
}sourcepub fn try_from_any<'a>(
root: AnyRoot,
guard: impl AsRef<CollectionGuard<'a>>
) -> Result<Self, AnyRoot>
pub fn try_from_any<'a>( root: AnyRoot, guard: impl AsRef<CollectionGuard<'a>> ) -> Result<Self, AnyRoot>
Try to convert a typeless root reference into a Root<T>.
§Errors
Returns Err(root) if root does not contain a T.
sourcepub fn root_count(&self) -> u64
pub fn root_count(&self) -> u64
Returns the current number of root references to this value, including
self.
sourcepub const fn downgrade_any(&self) -> AnyRef
pub const fn downgrade_any(&self) -> AnyRef
Returns an untyped “weak” reference erased to this root.
sourcepub fn to_any_root(&self) -> AnyRoot
pub fn to_any_root(&self) -> AnyRoot
Returns an untyped root reference.
sourcepub fn into_any_root(self) -> AnyRoot
pub fn into_any_root(self) -> AnyRoot
Returns this root as an untyped root.
Trait Implementations§
source§impl<T> Clone for Root<T>where
T: Collectable,
impl<T> Clone for Root<T>where
T: Collectable,
source§impl<T> Deref for Root<T>where
T: Collectable,
impl<T> Deref for Root<T>where
T: Collectable,
source§impl<T> Drop for Root<T>where
T: Collectable,
impl<T> Drop for Root<T>where
T: Collectable,
source§impl<T> Ord for Root<T>where
T: Collectable + Ord,
impl<T> Ord for Root<T>where
T: Collectable + Ord,
source§impl<T> PartialEq<&AnyRef> for Root<T>where
T: Collectable,
impl<T> PartialEq<&AnyRef> for Root<T>where
T: Collectable,
source§impl<T> PartialEq<&Ref<T>> for Root<T>where
T: Collectable,
impl<T> PartialEq<&Ref<T>> for Root<T>where
T: Collectable,
source§impl<T> PartialEq<&Root<T>> for Ref<T>where
T: Collectable,
impl<T> PartialEq<&Root<T>> for Ref<T>where
T: Collectable,
source§impl<T> PartialEq<AnyRef> for Root<T>where
T: Collectable,
impl<T> PartialEq<AnyRef> for Root<T>where
T: Collectable,
source§impl<T> PartialEq<Ref<T>> for Root<T>where
T: Collectable,
impl<T> PartialEq<Ref<T>> for Root<T>where
T: Collectable,
source§impl<T> PartialEq<Root<T>> for Ref<T>where
T: Collectable,
impl<T> PartialEq<Root<T>> for Ref<T>where
T: Collectable,
source§impl<T> PartialEq for Root<T>where
T: Collectable + PartialEq,
impl<T> PartialEq for Root<T>where
T: Collectable + PartialEq,
source§impl<T> PartialOrd for Root<T>where
T: Collectable + PartialOrd,
impl<T> PartialOrd for Root<T>where
T: Collectable + PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more