[][src]Trait butcher::flatten::FlattenCow

pub trait FlattenCow<'a, T: ToOwned + ?Sized + 'a> {
    fn flatten(self) -> Cow<'a, T>;
}

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(_))).

Required methods

fn flatten(self) -> Cow<'a, T>

Loading content...

Implementations on Foreign Types

impl<'a, 'b: 'a, T: ToOwned + ?Sized + 'a> FlattenCow<'a, T> for Cow<'b, Cow<'a, T>>[src]

fn flatten(self) -> Cow<'a, T>[src]

Flattens the Cow.

Loading content...

Implementors

Loading content...