Trait borrowme::ToOwned

source ·
pub trait ToOwned {
    type Owned;

    // Required method
    fn to_owned(&self) -> Self::Owned;
}
Expand description

Convert to owned.

This works similarly to ToOwned with a few relaxed constaints. It is recommended that you use to_owned instead of importing this trait.


What about std::borrow::ToOwned?

std::borrow::ToOwned a trait which requires that the resulting Owned type can be borrowed back into a reference of itself. This can’t be implemented for compound borrowing (See borrowme::Borrow). So because we can’t implement std::borrow::Borrow<Self>, we can’t implemented std::borrow::ToOwned either.

To showcase this, let’s try to implement std::borrow::ToOwned for a type which has a lifetime parameter:

struct Word<'a>(&'a str);
struct OwnedWord(String);

impl ToOwned for Word<'_> {
    type Owned = OwnedWord;

    fn to_owned(&self) -> OwnedWord {
        OwnedWord(String::from(self.0))
    }
}
error[E0277]: the trait bound `OwnedWord: std::borrow::Borrow<Word<'_>>` is not satisfied
  --> src/lib.rs:27:18
   |
11 |     type Owned = OwnedWord;
   |                  ^^^^^^^^^ the trait `std::borrow::Borrow<Word<'_>>` is not implemented for `OwnedWord`

So in this crate we define a different ToOwned trait which does not require the produced value to be Borrow<Self>.

With this, we can implement the conversion:

use borrowme::ToOwned;

impl ToOwned for Word<'_> {
    type Owned = OwnedWord;

    fn to_owned(&self) -> OwnedWord {
        OwnedWord(self.0.to_string())
    }
}

Required Associated Types§

source

type Owned

The owned type this is being converted to.

Required Methods§

source

fn to_owned(&self) -> Self::Owned

Perform a covnersion from a reference to owned value.

Implementations on Foreign Types§

source§

impl ToOwned for CStr

§

type Owned = CString

source§

fn to_owned(&self) -> Self::Owned

source§

impl<B> ToOwned for Cow<'_, B>where B: 'static + ?Sized + ToOwned,

§

type Owned = Cow<'static, B>

source§

fn to_owned(&self) -> <Self as ToOwned>::Owned

source§

impl ToOwned for OsStr

§

type Owned = OsString

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for Option<T>where T: ToOwned,

§

type Owned = Option<<T as ToOwned>::Owned>

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for Vec<T>where T: ToOwned,

§

type Owned = Vec<<T as ToOwned>::Owned, Global>

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for &Twhere T: ?Sized + ToOwned,

§

type Owned = <T as ToOwned>::Owned

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for HashSet<T>where T: ToOwned, T::Owned: Hash + Eq,

§

type Owned = HashSet<<T as ToOwned>::Owned, RandomState>

source§

fn to_owned(&self) -> Self::Owned

source§

impl ToOwned for str

§

type Owned = String

source§

fn to_owned(&self) -> Self::Owned

source§

impl<K, V> ToOwned for HashMap<K, V>where K: ToOwned, V: ToOwned, K::Owned: Hash + Eq,

§

type Owned = HashMap<<K as ToOwned>::Owned, <V as ToOwned>::Owned, RandomState>

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for LinkedList<T>where T: ToOwned,

§

type Owned = LinkedList<<T as ToOwned>::Owned, Global>

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for BTreeSet<T>where T: ToOwned, T::Owned: PartialOrd + Ord + Eq,

§

type Owned = BTreeSet<<T as ToOwned>::Owned, Global>

source§

fn to_owned(&self) -> Self::Owned

source§

impl ToOwned for &mut str

§

type Owned = String

source§

fn to_owned(&self) -> Self::Owned

source§

impl ToOwned for String

§

type Owned = String

source§

fn to_owned(&self) -> Self::Owned

source§

impl<K, V> ToOwned for BTreeMap<K, V>where K: ToOwned, V: ToOwned, K::Owned: PartialOrd + Ord + Eq,

§

type Owned = BTreeMap<<K as ToOwned>::Owned, <V as ToOwned>::Owned, Global>

source§

fn to_owned(&self) -> Self::Owned

source§

impl ToOwned for Path

§

type Owned = PathBuf

source§

fn to_owned(&self) -> Self::Owned

source§

impl<T> ToOwned for [T]where T: Clone,

§

type Owned = Vec<T, Global>

source§

fn to_owned(&self) -> Self::Owned

source§

impl ToOwned for &mut String

§

type Owned = String

source§

fn to_owned(&self) -> Self::Owned

Implementors§