[−][src]Trait butcher::flatten::FlattenCow
Allows to flatten a Cow
.
This trait is automatically implemented for each Cow<Cow<T>>
, and provide
the flatten
method. This method allows to deal easily with return type
created by #[derive(Butcher)]
when it is called on objects with Cow
in
one of their fields.
See for example the following struct:
use butcher::Butcher; use std::borrow::Cow; #[derive(Butcher, Clone)] struct Foo<'a> { bar: Cow<'a, str>, }
Here, the fields bar
of ButcheredFoo
would have type
Cow<'cow<Cow<'a str>>
. This trait allows us to easily get back a
Cow<'cow str>
:
use butcher::flatten::FlattenCow; let input_foo = Cow::Owned(Foo { bar: Cow::Borrowed("hello, world!"), }); // Here, bar has type Cow<'cow, Cow<'a, str>> let ButcheredFoo { bar } = Foo::butcher(input_foo); // The following piece of code transforms it into Cow<'cow, str> let bar = bar.flatten();
The outputed Cow
will be and Owned
variant only if the input Cow
is
Owned
on its two levels (eg: it matches Cow::Owned(Cow::Owned(_))
).