pub struct Ref<T> { /* private fields */ }Expand description
A reference to data stored in a garbage collector.
Unlike a Root<T>, this type is not guaranteed to have access to its
underlying data. If no Collectable reachable via all active Roots
marks this allocation, it will be collected.
Because of this, direct access to the data is not provided. To obtain a
reference, call Ref::load().
§Loading a reference
Ref::load() is used to provide a reference to data stored in the garbage
collector.
use refuse::{CollectionGuard, Ref};
let guard = CollectionGuard::acquire();
let data = Ref::new(42, &guard);
assert_eq!(data.load(&guard), Some(&42));References returned from Ref::load() are tied to the lifetime of the
guard. This ensures that a reference to data can only be held between
moments when the garbage collector can be run. For example these usages are
prevented by the compiler:
let guard = CollectionGuard::acquire();
let data = Ref::new(42, &guard);
let reference = data.load(&guard).unwrap();
drop(guard);
// error[E0505]: cannot move out of `guard` because it is borrowed
assert_eq!(reference, &42);let mut guard = CollectionGuard::acquire();
let data = Ref::new(42, &guard);
let reference = data.load(&guard).unwrap();
guard.yield_to_collector();
// error[E0502]: cannot borrow `guard` as mutable because it is also borrowed as immutable
assert_eq!(reference, &42);Implementations§
source§impl<T> Ref<T>where
T: Collectable,
impl<T> Ref<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 “weak” reference to
it.
Examples found in repository?
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
std::thread::scope(|s| {
for _ in 0..16 {
s.spawn(|| {
let mut guard = CollectionGuard::acquire();
for _ in 0..100 {
for _ in 0..100 {
Ref::new([0; 32], &guard);
}
guard.yield_to_collector();
}
});
}
});
}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);
}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 load<'guard>(
&self,
guard: &'guard CollectionGuard<'_>
) -> Option<&'guard T>
pub fn load<'guard>( &self, guard: &'guard CollectionGuard<'_> ) -> Option<&'guard T>
Loads a reference to the underlying data. Returns None if the data has
been collected and is no longer available.
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 as_root(&self, guard: &CollectionGuard<'_>) -> Option<Root<T>>
pub fn as_root(&self, guard: &CollectionGuard<'_>) -> Option<Root<T>>
Loads a root reference to the underlying data. Returns None if the
data has been collected and is no longer available.
Trait Implementations§
source§impl<T> Ord for Ref<T>
impl<T> Ord for Ref<T>
source§impl<T> PartialEq<&AnyRef> for Ref<T>where
T: Collectable,
impl<T> PartialEq<&AnyRef> for Ref<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 Ref<T>where
T: Collectable,
impl<T> PartialEq<AnyRef> for Ref<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 Ref<T>
impl<T> PartialEq for Ref<T>
source§impl<T> PartialOrd for Ref<T>
impl<T> PartialOrd for Ref<T>
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