[−][src]Trait butcher::as_deref::AsDerefCow
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 implementToOwned
, which is required to build the initialCow<T>
,T
must implementDeref
, and itsTarget
must also implementToOwned
, which is required to build the outputCow<<T as Deref>::Target>
, and when the borrowed case is met,T
must be convertible into theOwned
type associated to theTarget
dereferenced type, which is required when the owned case is met.
Required methods
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...
T: Deref + ToOwned,
<T as Deref>::Target: ToOwned,
<<T as Deref>::Target as ToOwned>::Owned: From<<T as ToOwned>::Owned>,