pub struct OwningRefMut<O, T: ?Sized> { /* fields omitted */ }
An mutable owning reference.
This wraps an owner O
and a reference &mut 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.
Creates a new owning reference from a owner
initialized to the direct dereference of it.
extern crate owning_ref;
use owning_ref::OwningRefMut;
fn main() {
let owning_ref_mut = OwningRefMut::new(Box::new(42));
assert_eq!(*owning_ref_mut, 42);
}
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.
Converts self
into a new shared 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.
extern crate owning_ref;
use owning_ref::OwningRefMut;
fn main() {
let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));
let owning_ref = owning_ref_mut.map(|array| &array[2]);
assert_eq!(*owning_ref, 3);
}
Converts self
into a new mutable 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.
extern crate owning_ref;
use owning_ref::OwningRefMut;
fn main() {
let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));
let owning_ref_mut = owning_ref_mut.map_mut(|array| &mut array[2]);
assert_eq!(*owning_ref_mut, 3);
}
Tries to convert self
into a new shared 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.
extern crate owning_ref;
use owning_ref::OwningRefMut;
fn main() {
let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));
let owning_ref = owning_ref_mut.try_map(|array| {
if array[2] == 3 { Ok(&array[2]) } else { Err(()) }
});
assert_eq!(*owning_ref.unwrap(), 3);
}
Tries to convert self
into a new mutable 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.
extern crate owning_ref;
use owning_ref::OwningRefMut;
fn main() {
let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));
let owning_ref_mut = owning_ref_mut.try_map_mut(|array| {
if array[2] == 3 { Ok(&mut array[2]) } else { Err(()) }
});
assert_eq!(*owning_ref_mut.unwrap(), 3);
}
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.
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 OwningRefMut<O, T>
to a OwningRefMut<Box<Erased>, T>
.
Erases the concrete base type of the owner with a trait object.
This allows mixing of owned references with different owner base types.
extern crate owning_ref;
use owning_ref::{OwningRefMut, Erased};
fn main() {
let owning_ref_mut_a: OwningRefMut<Box<[i32; 4]>, [i32; 4]>
= OwningRefMut::new(Box::new([1, 2, 3, 4]));
let owning_ref_mut_b: OwningRefMut<Box<Vec<(i32, bool)>>, Vec<(i32, bool)>>
= OwningRefMut::new(Box::new(vec![(0, false), (1, true)]));
let owning_ref_mut_a: OwningRefMut<Box<[i32; 4]>, i32>
= owning_ref_mut_a.map_mut(|a| &mut a[0]);
let owning_ref_mut_b: OwningRefMut<Box<Vec<(i32, bool)>>, i32>
= owning_ref_mut_b.map_mut(|a| &mut a[1].0);
let owning_refs_mut: [OwningRefMut<Box<Erased>, i32>; 2]
= [owning_ref_mut_a.erase_owner(), owning_ref_mut_b.erase_owner()];
assert_eq!(*owning_refs_mut[0], 1);
assert_eq!(*owning_refs_mut[1], 1);
}
A getter for the underlying owner.
Discards the reference and retrieves the owner.
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the minimum of two values. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
type Target = T
The resulting type after dereferencing.
Mutably dereferences the value.
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
Create an error for a missing method specialization. Defaults to panicking with type, trait & method names. S
is the encoder/decoder state type, T
is the type being encoded/decoded, and the arguments are the names of the trait and method that should've been overridden. Read more