Enum boo::Boo
[−]
[src]
pub enum Boo<'a, B: ?Sized + 'a> where
B: ToOwned, { Borrowed(&'a B), Owned(<B as ToOwned>::Owned), }
A clone-on-write smart pointer.
The type Boo
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. The type is designed to
work with general borrowed data via the Borrow
trait.
Boo
implements Deref
, which means that you can call
non-mutating methods directly on the data it encloses. If mutation
is desired, to_mut
will obtain a mutable reference to an owned
value, cloning if necessary.
Examples
use std::borrow::Boo; fn abs_all(input: &mut Boo<[i32]>) { for i in 0..input.len() { let v = input[i]; if v < 0 { // Clones into a vector if not already owned. input.to_mut()[i] = -v; } } } // No clone occurs because `input` doesn't need to be mutated. let slice = [0, 1, 2]; let mut input = Boo::from(&slice[..]); abs_all(&mut input); // Clone occurs because `input` needs to be mutated. let slice = [-1, 0, 1]; let mut input = Boo::from(&slice[..]); abs_all(&mut input); // No clone occurs because `input` is already owned. let mut input = Boo::from(vec![-1, 0, 1]); abs_all(&mut input);
Variants
Borrowed(&'a B)
Borrowed data.
Owned(<B as ToOwned>::Owned)
Owned data.
Methods
impl<'a, B: ?Sized> Boo<'a, B> where
B: ToOwned,
[src]
B: ToOwned,
fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned
[src]
Acquires a mutable reference to the owned form of the data.
Clones the data if it is not already owned.
Examples
use std::borrow::Boo; let mut Boo = Boo::Borrowed("foo"); Boo.to_mut().make_ascii_uppercase(); assert_eq!( Boo, Boo::Owned(String::from("FOO")) as Boo<str> );
fn into_owned(self) -> <B as ToOwned>::Owned
[src]
Extracts the owned data.
Clones the data if it is not already owned.
Examples
Calling into_owned
on a Boo::Borrowed
clones the underlying data
and becomes a Boo::Owned
:
use std::borrow::Boo; let s = "Hello world!"; let Boo = Boo::Borrowed(s); assert_eq!( Boo.into_owned(), Boo::Owned(String::from(s)) );
Calling into_owned
on a Boo::Owned
is a no-op:
use std::borrow::Boo; let s = "Hello world!"; let Boo: Boo<str> = Boo::Owned(String::from(s)); assert_eq!( Boo.into_owned(), Boo::Owned(String::from(s)) );
Trait Implementations
impl<'a, B: ?Sized> Borrow<B> for Boo<'a, B> where
B: ToOwned,
<B as ToOwned>::Owned: 'a,
[src]
B: ToOwned,
<B as ToOwned>::Owned: 'a,
impl<'a, B: ?Sized> Clone for Boo<'a, B> where
B: ToOwned,
[src]
B: ToOwned,
fn clone(&self) -> Boo<'a, B>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Boo<'a, B>)
[src]
Performs copy-assignment from source
. Read more
impl<'a, B: ?Sized> Deref for Boo<'a, B> where
B: ToOwned,
[src]
B: ToOwned,
type Target = B
The resulting type after dereferencing.
fn deref(&self) -> &B
[src]
Dereferences the value.
impl<'a, B: ?Sized> Eq for Boo<'a, B> where
B: Eq + ToOwned,
[src]
B: Eq + ToOwned,
impl<'a, B: ?Sized> Ord for Boo<'a, B> where
B: Ord + ToOwned,
[src]
B: Ord + ToOwned,
fn cmp(&self, other: &Boo<'a, B>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.0[src]
Compares and returns the minimum of two values. Read more
impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Boo<'b, C>> for Boo<'a, B> where
B: PartialEq<C> + ToOwned,
C: ToOwned,
[src]
B: PartialEq<C> + ToOwned,
C: ToOwned,
fn eq(&self, other: &Boo<'b, C>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<'a, B: ?Sized> PartialOrd for Boo<'a, B> where
B: PartialOrd + ToOwned,
[src]
B: PartialOrd + ToOwned,
fn partial_cmp(&self, other: &Boo<'a, B>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<'a, B: ?Sized> Debug for Boo<'a, B> where
B: Debug + ToOwned,
<B as ToOwned>::Owned: Debug,
[src]
B: Debug + ToOwned,
<B as ToOwned>::Owned: Debug,
impl<'a, B: ?Sized> Display for Boo<'a, B> where
B: Display + ToOwned,
<B as ToOwned>::Owned: Display,
[src]
B: Display + ToOwned,
<B as ToOwned>::Owned: Display,
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<'a, B: ?Sized> Default for Boo<'a, B> where
B: ToOwned,
<B as ToOwned>::Owned: Default,
[src]
B: ToOwned,
<B as ToOwned>::Owned: Default,
fn default() -> Boo<'a, B>
[src]
Creates an owned Boo<'a, B> with the default value for the contained owned value.
impl<'a, B: ?Sized> Hash for Boo<'a, B> where
B: Hash + ToOwned,
[src]
B: Hash + ToOwned,
fn hash<H: Hasher>(&self, state: &mut H)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more