[][src]Trait butcher::as_deref::AsDerefCow

pub trait AsDerefCow<'cow, T> where
    T: Deref,
    <T as Deref>::Target: ToOwned
{ fn as_deref(self) -> Cow<'cow, <T as Deref>::Target>; }

Allows to dereference the content of a Cow.

The best use case for this is to transform a Cow<String> into a Cow<str>. This also works for the boxed unsized type, as long as they implement the traits as required below.

Example

The following example shows the flattening of Cow<String> and Cow<PathBuf>:

use std::{
    borrow::Cow,
    path::{Path, PathBuf},
};

use butcher::as_deref::AsDerefCow;

// The type is annotated so that it is easier to understand what's happening
let a: Cow<String> = Cow::Owned(String::from("Grace Hopper"));
let a_as_deref: Cow<str> = a.as_deref();
assert_eq!(a_as_deref, "Grace Hopper");

let b: Cow<PathBuf> = Cow::Owned(PathBuf::from("/path/to/foo"));
let b_as_deref: Cow<Path> = b.as_deref();
assert_eq!(b_as_deref, Path::new("/path/to/foo"));

Traits requirements

In order to call as_deref on a Cow<T>, the following requirements must be satisified:

  • T must implement ToOwned, which is required to build the initial Cow<T>,
  • T must implement Deref, and its Target must also implement ToOwned, which is required to build the output Cow<<T as Deref>::Target>, and when the borrowed case is met,
  • T must be convertible into the Owned type associated to the Target dereferenced type, which is required when the owned case is met.

Required methods

fn as_deref(self) -> Cow<'cow, <T as Deref>::Target>

Loading content...

Implementations on Foreign Types

impl<'cow, T> AsDerefCow<'cow, T> for Cow<'cow, T> where
    T: Deref + ToOwned,
    <T as Deref>::Target: ToOwned,
    <<T as Deref>::Target as ToOwned>::Owned: From<<T as ToOwned>::Owned>, 
[src]

Loading content...

Implementors

Loading content...