pub struct Cow<'a, T>{ /* private fields */ }Expand description
A clone-on-write smart pointer.
The type Cow is a smart pointer providing clone-on-write functionality: it
can enclose and provide immutable access to borrowed data, and clone the
data lazily when mutation or ownership is required.
Cow implements Deref, which means that you can call non-mutating
methods directly on the data it encloses.
Implementations§
Source§impl<'a, T> Cow<'a, T>
impl<'a, T> Cow<'a, T>
Sourcepub fn owned(o: T::Owned) -> Self
pub fn owned(o: T::Owned) -> Self
Construct from owned data.
§Examples
use dairy::Cow;
let cow: Cow<str> = Cow::owned(String::from("moo"));Sourcepub fn is_borrowed(&self) -> bool
pub fn is_borrowed(&self) -> bool
Returns true if the data is borrowed.
§Examples
use dairy::Cow;
let cow: Cow<str> = Cow::borrowed("moo");
assert!(cow.is_borrowed());
let cow: Cow<str> = Cow::owned(String::from("...moo?"));
assert!(!cow.is_borrowed());Sourcepub fn is_owned(&self) -> bool
pub fn is_owned(&self) -> bool
Returns true if the data is owned.
§Examples
use dairy::Cow;
let cow: Cow<str> = Cow::owned(String::from("moo"));
assert!(cow.is_owned());
let cow: Cow<str> = Cow::borrowed("...moo?");
assert!(!cow.is_owned());Sourcepub fn into_owned(self) -> T::Owned
pub fn into_owned(self) -> T::Owned
Converts into owned data.
Clones the data if it is not already owned.
§Examples
Calling .into_owned() on a borrowed Cow clones the underlying data.
use dairy::Cow;
let cow = Cow::borrowed("Moo!");
assert_eq!(cow.into_owned(), String::from("Moo!"));Calling .into_owned() on an owned Cow is a noop.
use dairy::Cow;
let cow: Cow<str> = Cow::owned(String::from("Moo!"));
assert_eq!(cow.into_owned(), String::from("Moo!"));Sourcepub fn into_boxed(self) -> Box<T>where
T: ToBoxed,
pub fn into_boxed(self) -> Box<T>where
T: ToBoxed,
Converts into boxed data.
Clones the data if it is not already owned.
§Examples
Calling .into_boxed() on a borrowed Cow clones the underlying data.
use dairy::Cow;
let cow = Cow::borrowed("Moo!");
assert_eq!(cow.into_boxed(), String::from("Moo!").into_boxed_str());Calling .into_boxed() on an owned Cow is a noop.
use dairy::Cow;
let cow: Cow<str> = Cow::owned(String::from("Moo!"));
assert_eq!(cow.into_boxed(), String::from("Moo!").into_boxed_str());Sourcepub fn apply<F>(&mut self, f: F)
pub fn apply<F>(&mut self, f: F)
Applies the given function to the owned data.
Clones the data if it is not already owned. This is useful because the
compact implementation is not able to provide a .to_mut() method like
the standard library. This function allows you to modify the Cow
without moving it.
§Examples
use dairy::Cow;
let mut cow = Cow::borrowed("Moo!");
cow.apply(|s| s.make_ascii_uppercase());
assert_eq!(cow, "MOO!");Trait Implementations§
Source§impl<'a, 'b> Extend<&'b OsStr> for Cow<'a, OsStr>
impl<'a, 'b> Extend<&'b OsStr> for Cow<'a, OsStr>
Source§fn extend<I: IntoIterator<Item = &'b OsStr>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b OsStr>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<&'b OsString> for Cow<'a, OsStr>
impl<'a, 'b> Extend<&'b OsString> for Cow<'a, OsStr>
Source§fn extend<I: IntoIterator<Item = &'b OsString>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b OsString>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<&'b Path> for Cow<'a, Path>
impl<'a, 'b> Extend<&'b Path> for Cow<'a, Path>
Source§fn extend<I: IntoIterator<Item = &'b Path>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b Path>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<&'b PathBuf> for Cow<'a, Path>
impl<'a, 'b> Extend<&'b PathBuf> for Cow<'a, Path>
Source§fn extend<I: IntoIterator<Item = &'b PathBuf>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b PathBuf>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<&'b String> for Cow<'a, str>
impl<'a, 'b> Extend<&'b String> for Cow<'a, str>
Source§fn extend<I: IntoIterator<Item = &'b String>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b String>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b, T: 'a + 'b + Copy> Extend<&'b T> for Cow<'a, [T]>
impl<'a, 'b, T: 'a + 'b + Copy> Extend<&'b T> for Cow<'a, [T]>
Source§fn extend<I: IntoIterator<Item = &'b T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<&'b char> for Cow<'a, str>
impl<'a, 'b> Extend<&'b char> for Cow<'a, str>
Source§fn extend<I: IntoIterator<Item = &'b char>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b char>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<&'b str> for Cow<'a, str>
impl<'a, 'b> Extend<&'b str> for Cow<'a, str>
Source§fn extend<I: IntoIterator<Item = &'b str>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'b str>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<Box<OsStr>> for Cow<'_, OsStr>
impl Extend<Box<OsStr>> for Cow<'_, OsStr>
Source§fn extend<I: IntoIterator<Item = Box<OsStr>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Box<OsStr>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<Box<Path>> for Cow<'_, Path>
impl Extend<Box<Path>> for Cow<'_, Path>
Source§fn extend<I: IntoIterator<Item = Box<Path>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Box<Path>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<Box<str>> for Cow<'_, str>
impl Extend<Box<str>> for Cow<'_, str>
Source§fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<Cow<'b, OsStr>> for Cow<'a, OsStr>
impl<'a, 'b> Extend<Cow<'b, OsStr>> for Cow<'a, OsStr>
Source§fn extend<I: IntoIterator<Item = Cow<'b, OsStr>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Cow<'b, OsStr>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<Cow<'b, Path>> for Cow<'a, Path>
impl<'a, 'b> Extend<Cow<'b, Path>> for Cow<'a, Path>
Source§fn extend<I: IntoIterator<Item = Cow<'b, Path>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Cow<'b, Path>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b> Extend<Cow<'b, str>> for Cow<'a, str>
impl<'a, 'b> Extend<Cow<'b, str>> for Cow<'a, str>
Source§fn extend<I: IntoIterator<Item = Cow<'b, str>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Cow<'b, str>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<OsString> for Cow<'_, OsStr>
impl Extend<OsString> for Cow<'_, OsStr>
Source§fn extend<I: IntoIterator<Item = OsString>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = OsString>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<PathBuf> for Cow<'_, Path>
impl Extend<PathBuf> for Cow<'_, Path>
Source§fn extend<I: IntoIterator<Item = PathBuf>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = PathBuf>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<String> for Cow<'_, str>
impl Extend<String> for Cow<'_, str>
Source§fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, 'b, T: 'a + Clone> Extend<T> for Cow<'a, [T]>
impl<'a, 'b, T: 'a + Clone> Extend<T> for Cow<'a, [T]>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<char> for Cow<'_, str>
impl Extend<char> for Cow<'_, str>
Source§fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)