Expand description

Generalization of std::borrow::Cow

deref_owned provides a wrapper Owned<B> which stores an inner value of type <B as ToOwned>::Owned and points to a value of type B by implementing Deref<Target = B> through Borrow::borrow.

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 is provided, which allows conversion from certain types into an owned type through an .into_owned() method. The trait GenericCow<Borrowed = B> is a generalization of Cow<'_, B> and is implemented for:

Example

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

fn generic_fn(arg: impl GenericCow<Borrowed = str>) {
    let reference: &str = &*arg; // or: 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 akin to Cow::Owned but known to be owned at compile-time

Traits

Generalized Cow