pub struct GenerationalRef<R> { /* private fields */ }
Expand description
A reference to a value in a generational box. This reference acts similarly to std::cell::Ref
, but has extra debug information
to track when all references to the value are created and dropped.
GenerationalRef
implements Deref
which means you can call methods on the inner value just like you would on a reference to the
inner value. If you need to get the inner reference directly, you can call GenerationalRef::deref
.
§Example
let owner = UnsyncStorage::owner();
let value = owner.insert(String::from("hello"));
let reference = value.read();
// You call methods like `as_str` on the reference just like you would with the inner String
assert_eq!(reference.as_str(), "hello");
§Matching on GenerationalRef
You need to get the inner reference with GenerationalRef::deref
before you match the inner value. If you try to match without
calling GenerationalRef::deref
, you will get an error like this:
ⓘ
enum Colors {
Red,
Green
}
let owner = UnsyncStorage::owner();
let value = owner.insert(Colors::Red);
let reference = value.read();
match reference {
// Since we are matching on the `GenerationalRef` type instead of &Colors, we can't match on the enum directly
Colors::Red => {}
Colors::Green => {}
}
error[E0308]: mismatched types
--> packages/generational-box/tests/basic.rs:25:9
|
2 | Red,
| --- unit variant defined here
...
3 | match reference {
| --------- this expression has type `GenerationalRef<Ref<'_, Colors>>`
4 | // Since we are matching on the `GenerationalRef` type instead of &Colors, we can't match on the enum directly
5 | Colors::Red => {}
| ^^^^^^^^^^^ expected `GenerationalRef<Ref<'_, Colors>>`, found `Colors`
|
= note: expected struct `GenerationalRef<Ref<'_, Colors>>`
found enum `Colors`
Instead, you need to deref the reference to get the inner value before you match on it:
use std::ops::Deref;
enum Colors {
Red,
Green
}
let owner = UnsyncStorage::owner();
let value = owner.insert(Colors::Red);
let reference = value.read();
// Deref converts the `GenerationalRef` into a `&Colors`
match reference.deref() {
// Now we can match on the inner value
Colors::Red => {}
Colors::Green => {}
}
Implementations§
Source§impl<T: ?Sized + 'static, R: Deref<Target = T>> GenerationalRef<R>
impl<T: ?Sized + 'static, R: Deref<Target = T>> GenerationalRef<R>
Sourcepub fn map<R2, F: FnOnce(R) -> R2>(self, f: F) -> GenerationalRef<R2>
pub fn map<R2, F: FnOnce(R) -> R2>(self, f: F) -> GenerationalRef<R2>
Map the inner value to a new type
Trait Implementations§
Auto Trait Implementations§
impl<R> Freeze for GenerationalRef<R>where
R: Freeze,
impl<R> !RefUnwindSafe for GenerationalRef<R>
impl<R> Send for GenerationalRef<R>where
R: Send,
impl<R> Sync for GenerationalRef<R>where
R: Sync,
impl<R> Unpin for GenerationalRef<R>where
R: Unpin,
impl<R> !UnwindSafe for GenerationalRef<R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more