Struct rustc_data_structures::owning_ref::OwningRef
source · Expand description
An owning reference.
This wraps an owner O
and a reference &T
pointing
at something reachable from O::Target
while keeping
the ability to move self
around.
The owner is usually a pointer that points at some base type.
For more details and examples, see the module and method docs.
Fields
owner: O
reference: *const T
Implementations
sourceimpl<O, T: ?Sized> OwningRef<O, T>
impl<O, T: ?Sized> OwningRef<O, T>
sourcepub fn new(o: O) -> Selfwhere
O: StableAddress,
O: Deref<Target = T>,
pub fn new(o: O) -> Selfwhere
O: StableAddress,
O: Deref<Target = T>,
Creates a new owning reference from an owner initialized to the direct dereference of it.
Example
use rustc_data_structures::owning_ref::OwningRef;
fn main() {
let owning_ref = OwningRef::new(Box::new(42));
assert_eq!(*owning_ref, 42);
}
sourcepub unsafe fn new_assert_stable_address(o: O) -> Selfwhere
O: Deref<Target = T>,
pub unsafe fn new_assert_stable_address(o: O) -> Selfwhere
O: Deref<Target = T>,
Like new
, but doesn’t require O
to implement the StableAddress
trait.
Instead, the caller is responsible to make the same promises as implementing the trait.
This is useful for cases where coherence rules prevents implementing the trait without adding a dependency to this crate in a third-party library.
sourcepub fn map<F, U: ?Sized>(self, f: F) -> OwningRef<O, U>where
O: StableAddress,
F: FnOnce(&T) -> &U,
pub fn map<F, U: ?Sized>(self, f: F) -> OwningRef<O, U>where
O: StableAddress,
F: FnOnce(&T) -> &U,
Converts self
into a new owning reference that points at something reachable
from the previous one.
This can be a reference to a field of U
, something reachable from a field of
U
, or even something unrelated with a 'static
lifetime.
Example
use rustc_data_structures::owning_ref::OwningRef;
fn main() {
let owning_ref = OwningRef::new(Box::new([1, 2, 3, 4]));
// create an owning reference that points at the
// third element of the array.
let owning_ref = owning_ref.map(|array| &array[2]);
assert_eq!(*owning_ref, 3);
}
sourcepub fn try_map<F, U: ?Sized, E>(self, f: F) -> Result<OwningRef<O, U>, E>where
O: StableAddress,
F: FnOnce(&T) -> Result<&U, E>,
pub fn try_map<F, U: ?Sized, E>(self, f: F) -> Result<OwningRef<O, U>, E>where
O: StableAddress,
F: FnOnce(&T) -> Result<&U, E>,
Tries to convert self
into a new owning reference that points
at something reachable from the previous one.
This can be a reference to a field of U
, something reachable from a field of
U
, or even something unrelated with a 'static
lifetime.
Example
use rustc_data_structures::owning_ref::OwningRef;
fn main() {
let owning_ref = OwningRef::new(Box::new([1, 2, 3, 4]));
// create an owning reference that points at the
// third element of the array.
let owning_ref = owning_ref.try_map(|array| {
if array[2] == 3 { Ok(&array[2]) } else { Err(()) }
});
assert_eq!(*owning_ref.unwrap(), 3);
}
sourcepub unsafe fn map_owner<F, P>(self, f: F) -> OwningRef<P, T>where
O: StableAddress,
P: StableAddress,
F: FnOnce(O) -> P,
pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRef<P, T>where
O: StableAddress,
P: StableAddress,
F: FnOnce(O) -> P,
Converts self
into a new owning reference with a different owner type.
The new owner type needs to still contain the original owner in some way so that the reference into it remains valid. This function is marked unsafe because the user needs to manually uphold this guarantee.
sourcepub fn map_owner_box(self) -> OwningRef<Box<O>, T>
pub fn map_owner_box(self) -> OwningRef<Box<O>, T>
Converts self
into a new owning reference where the owner is wrapped
in an additional Box<O>
.
This can be used to safely erase the owner of any OwningRef<O, T>
to an OwningRef<Box<Erased>, T>
.
sourcepub fn erase_owner<'a>(self) -> OwningRef<O::Erased, T>where
O: IntoErased<'a>,
pub fn erase_owner<'a>(self) -> OwningRef<O::Erased, T>where
O: IntoErased<'a>,
Erases the concrete base type of the owner with a trait object.
This allows mixing of owned references with different owner base types.
Example
use rustc_data_structures::owning_ref::{OwningRef, Erased};
fn main() {
// N.B., using the concrete types here for explicitness.
// For less verbose code type aliases like `BoxRef` are provided.
let owning_ref_a: OwningRef<Box<[i32; 4]>, [i32; 4]>
= OwningRef::new(Box::new([1, 2, 3, 4]));
let owning_ref_b: OwningRef<Box<Vec<(i32, bool)>>, Vec<(i32, bool)>>
= OwningRef::new(Box::new(vec![(0, false), (1, true)]));
let owning_ref_a: OwningRef<Box<[i32; 4]>, i32>
= owning_ref_a.map(|a| &a[0]);
let owning_ref_b: OwningRef<Box<Vec<(i32, bool)>>, i32>
= owning_ref_b.map(|a| &a[1].0);
let owning_refs: [OwningRef<Box<dyn Erased>, i32>; 2]
= [owning_ref_a.erase_owner(), owning_ref_b.erase_owner()];
assert_eq!(*owning_refs[0], 1);
assert_eq!(*owning_refs[1], 1);
}
sourcepub fn erase_send_owner<'a>(self) -> OwningRef<O::Erased, T>where
O: IntoErasedSend<'a>,
pub fn erase_send_owner<'a>(self) -> OwningRef<O::Erased, T>where
O: IntoErasedSend<'a>,
Erases the concrete base type of the owner with a trait object which implements Send
.
This allows mixing of owned references with different owner base types.
sourcepub fn erase_send_sync_owner<'a>(self) -> OwningRef<O::Erased, T>where
O: IntoErasedSendSync<'a>,
pub fn erase_send_sync_owner<'a>(self) -> OwningRef<O::Erased, T>where
O: IntoErasedSendSync<'a>,
Erases the concrete base type of the owner with a trait object
which implements Send
and Sync
.
This allows mixing of owned references with different owner base types.
sourcepub fn into_inner(self) -> O
pub fn into_inner(self) -> O
Discards the reference and retrieves the owner.
Trait Implementations
sourceimpl<O, T: ?Sized> Clone for OwningRef<O, T>where
O: CloneStableAddress,
impl<O, T: ?Sized> Clone for OwningRef<O, T>where
O: CloneStableAddress,
sourceimpl<O, T: ?Sized> From<OwningRefMut<O, T>> for OwningRef<O, T>where
O: StableAddress,
O: DerefMut<Target = T>,
impl<O, T: ?Sized> From<OwningRefMut<O, T>> for OwningRef<O, T>where
O: StableAddress,
O: DerefMut<Target = T>,
sourcefn from(other: OwningRefMut<O, T>) -> Self
fn from(other: OwningRefMut<O, T>) -> Self
sourceimpl<O, T: ?Sized> Ord for OwningRef<O, T>where
T: Ord,
impl<O, T: ?Sized> Ord for OwningRef<O, T>where
T: Ord,
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl<O, T: ?Sized> PartialEq<OwningRef<O, T>> for OwningRef<O, T>where
T: PartialEq,
impl<O, T: ?Sized> PartialEq<OwningRef<O, T>> for OwningRef<O, T>where
T: PartialEq,
sourceimpl<O, T: ?Sized> PartialOrd<OwningRef<O, T>> for OwningRef<O, T>where
T: PartialOrd,
impl<O, T: ?Sized> PartialOrd<OwningRef<O, T>> for OwningRef<O, T>where
T: PartialOrd,
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl<O, T: ?Sized> CloneStableDeref for OwningRef<O, T>where
O: CloneStableAddress,
impl<O, T: ?Sized> Eq for OwningRef<O, T>where
T: Eq,
impl<O, T: ?Sized> Send for OwningRef<O, T>where
O: Send,
for<'a> &'a T: Send,
impl<O, T: ?Sized> StableDeref for OwningRef<O, T>
impl<O, T: ?Sized> Sync for OwningRef<O, T>where
O: Sync,
for<'a> &'a T: Sync,
Auto Trait Implementations
impl<O, T: ?Sized> RefUnwindSafe for OwningRef<O, T>where
O: RefUnwindSafe,
T: RefUnwindSafe,
impl<O, T: ?Sized> Unpin for OwningRef<O, T>where
O: Unpin,
impl<O, T: ?Sized> UnwindSafe for OwningRef<O, T>where
O: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<'a, T> Captures<'a> for Twhere
T: ?Sized,
impl<T> Erased for T
Layout
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.