Expand description

Generalization of std::borrow::Cow

deref_owned provides a wrapper Owned which holds an inner value and points to it by implementing Deref. Owned is similar to Cow, except that it’s always owning a value. This can be used to avoid runtime overhead in certain scenarios.

Moreover, a trait GenericCow<B: ?Sized + ToOwned>: Borrow<B> is provided, which allows conversion from certain types into an owned type <B as ToOwned>::Owned through an .into_owned() method. The trait can be seen as a generalization of Cow<'_, B> and is implemented for:

  • &'a B
  • Cow<'a, B>
  • Owned<<B as ToOwned>::Owned>

Example

use deref_owned::{GenericCow, Owned};
use std::borrow::{Borrow, Cow};

fn generic_fn(arg: impl GenericCow<str>) {
    let reference: &str = arg.borrow();
    assert_eq!(reference, "Hello");
    let owned: String = arg.into_owned();
    assert_eq!(owned, "Hello".to_string());
}

generic_fn(Owned("Hello".to_string()));
generic_fn(Cow::Owned("Hello".to_string()));
generic_fn(Cow::Borrowed("Hello"));
generic_fn("Hello");

Structs

Wrapper holding an owned value of type T and implementing Deref<Target = T>

Traits

Types which implement Borrow<B> and can be converted into <B as ToOwned>::Owned