| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
pub trait OwnedToRef { |
| 12 |
|
| 13 |
type Borrowed<'a> |
| 14 |
where |
| 15 |
Self: 'a; |
| 16 |
|
| 17 |
|
| 18 |
fn owned_to_ref(&self) -> Self::Borrowed<'_>; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
pub trait RefToOwned<'a> { |
| 28 |
|
| 29 |
type Owned: OwnedToRef<Borrowed<'a> = Self> |
| 30 |
where |
| 31 |
Self: 'a; |
| 32 |
|
| 33 |
|
| 34 |
fn ref_to_owned(&self) -> Self::Owned; |
| 35 |
} |
| 36 |
|
| 37 |
impl<T> OwnedToRef for Option<T> |
| 38 |
where |
| 39 |
T: OwnedToRef, |
| 40 |
{ |
| 41 |
type Borrowed<'a> |
| 42 |
= Option<T::Borrowed<'a>> |
| 43 |
where |
| 44 |
T: 'a; |
| 45 |
|
| 46 |
#[allow(clippy::redundant_closure_for_method_calls, reason = "MSRV")] |
| 47 |
fn owned_to_ref(&self) -> Self::Borrowed<'_> { |
| 48 |
self.as_ref().map(|o| o.owned_to_ref()) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl<'a, T> RefToOwned<'a> for Option<T> |
| 53 |
where |
| 54 |
T: RefToOwned<'a> + 'a, |
| 55 |
T::Owned: OwnedToRef, |
| 56 |
{ |
| 57 |
type Owned = Option<T::Owned>; |
| 58 |
|
| 59 |
#[allow(clippy::redundant_closure_for_method_calls, reason = "MSRV")] |
| 60 |
fn ref_to_owned(&self) -> Self::Owned { |
| 61 |
self.as_ref().map(|o| o.ref_to_owned()) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
#[cfg(feature = "alloc")] |
| 66 |
mod allocating { |
| 67 |
use super::{OwnedToRef, RefToOwned}; |
| 68 |
use alloc::boxed::Box; |
| 69 |
|
| 70 |
impl<'a> RefToOwned<'a> for &'a [u8] { |
| 71 |
type Owned = Box<[u8]>; |
| 72 |
|
| 73 |
fn ref_to_owned(&self) -> Self::Owned { |
| 74 |
Box::from(*self) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl OwnedToRef for Box<[u8]> { |
| 79 |
type Borrowed<'a> = &'a [u8]; |
| 80 |
|
| 81 |
fn owned_to_ref(&self) -> Self::Borrowed<'_> { |
| 82 |
self.as_ref() |
| 83 |
} |
| 84 |
} |
| 85 |
} |