[−][src]Trait into_owned::IntoOwned
A trait for associating a type with its owned variant.
Examples
Implementing the IntoOwned
trait for an owned type is straighitforward.
Maybe a derive macro is supplied in future.
use into_owned::{IntoOwned, Is}; struct A(); impl IntoOwned for A { type Owned = Self; fn as_is<'a>(self) -> Is<'a, Self::Owned> { Is::Owned(self) } }
Since there are blanket implementations for &T
and &mut T
where
T: IntoOwned<Owned = T>
,
manually implementing IntoOwned
for the owned type is sufficient in most cases.
Associated Types
Loading content...Required methods
fn as_is<'a>(self) -> Is<'a, Self::Owned> where
Self: 'a,
Self: 'a,
Returns self
as Is<'a, T>
(T
is Self::Owned
here).
This method is useful when self
is used differently depending on
its state: owned, (immutably) borrowed, or mutably borrowed.
Examples
use into_owned::{IntoOwned, Is}; // returns: // * `0` if `t` is an owned value // * `1` if `t` is an immutably borrowed reference // * `2` if `t` is a mutably borrowed reference fn a<T>(t: T) -> i8 where T: IntoOwned { match t.as_is() { Is::Owned(x) => { // here `x: T::Owned` 0 } Is::Borrowed(x) => { // here `x: &T::Owned` 1 } Is::MutBorrowed(x) => { // here `x: &mut T::Owned` 2 } } } assert_eq!(a(1.0), 0); assert_eq!(a(&1.0), 1); assert_eq!(a(&mut 1.0), 2);
In addition, Is<'a, T>
has a functionality similar to Cow<'a, B>
.
See its documentation for more.
Provided methods
fn into_owned(self) -> Self::Owned where
Self::Owned: Clone,
Self::Owned: Clone,
Converts self
into an owned value.
If Self
and Self::Owned
are the same, usually it just returns self
.
If not, usually it returns an owned value by cloning.
This method requires Self::Owned
to be Clone
.
fn into_gyu<'a>(self) -> Gyu<'a, Self::Owned> where
Self: 'a,
Self: 'a,
Converts self
into Gyu<'a, T>
(T
is Self::Owned
here).
Gyu<'a, T>
has a functionality similar to Is<'a, T>
.
Unlike Is<'a, T>
, it cannot hold a mutably borrowed reference,
and thus it is more similar to Cow<'a, B>
.
See its documentation for more.
fn try_into_gyu_mut<'a>(self) -> Option<GyuMut<'a, Self::Owned>> where
Self: 'a,
Self: 'a,
Converts self
into GyuMut<'a, T>
(T
is Self::Owned
here) if possible,
i.e., self
is not an immutably borrowed reference.
If GyuMut<'a, T>
is necessary, use to_gyu_mut
method instead.
GyuMut<'a, T>
has a functionality similar to Is<'a, T>
.
Unlike Is<'a, T>
, it cannot hold a immutably borrowed reference.
GyuMut<'a, T>
implements more traits which require mutablity.
See its documentation for more.
fn to_gyu_mut<'a>(self) -> GyuMut<'a, Self::Owned> where
Self: 'a,
Self::Owned: Clone,
Self: 'a,
Self::Owned: Clone,
Converts self
into GyuMut<'a, T>
(T
is Self::Owned
here).
Unlike try_into_gyu_mut
method,
it clones self
if it is an immutably borrowed reference.
fn into_cow<'a, B: ?Sized>(self) -> Cow<'a, B> where
Self: 'a,
Self::Owned: Borrow<B>,
B: ToOwned<Owned = Self::Owned>,
Self: 'a,
Self::Owned: Borrow<B>,
B: ToOwned<Owned = Self::Owned>,
Converts self
into Cow<'a, B>
.
This method is available if std
feature is enabled (enabled by default).
Implementations on Foreign Types
impl<'_, T> IntoOwned for &'_ T where
T: Owned,
[src]
T: Owned,
impl<'_, T> IntoOwned for &'_ mut T where
T: Owned,
[src]
T: Owned,
impl IntoOwned for i8
[src]
impl IntoOwned for u8
[src]
impl IntoOwned for i16
[src]
impl IntoOwned for u16
[src]
impl IntoOwned for i32
[src]
impl IntoOwned for u32
[src]
impl IntoOwned for i64
[src]
impl IntoOwned for u64
[src]
impl IntoOwned for isize
[src]
impl IntoOwned for usize
[src]
impl IntoOwned for i128
[src]
impl IntoOwned for u128
[src]
impl IntoOwned for f32
[src]
impl IntoOwned for f64
[src]
Loading content...
Implementors
impl<'_, T> IntoOwned for Gyu<'_, T> where
T: Owned,
[src]
T: Owned,
impl<'_, T> IntoOwned for GyuMut<'_, T> where
T: Owned,
[src]
T: Owned,
impl<'_, T> IntoOwned for Is<'_, T> where
T: Owned,
[src]
T: Owned,