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 data.

Owned data.

Methods

impl<'a, B: ?Sized> Boo<'a, B> where
    B: ToOwned
[src]

[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>
);

[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]

[src]

Immutably borrows from an owned value. Read more

impl<'a, B: ?Sized> Clone for Boo<'a, B> where
    B: ToOwned
[src]

[src]

Returns a copy of the value. Read more

[src]

Performs copy-assignment from source. Read more

impl<'a, B: ?Sized> Deref for Boo<'a, B> where
    B: ToOwned
[src]

The resulting type after dereferencing.

[src]

Dereferences the value.

impl<'a, B: ?Sized> Eq for Boo<'a, B> where
    B: Eq + ToOwned
[src]

impl<'a, B: ?Sized> Ord for Boo<'a, B> where
    B: Ord + ToOwned
[src]

[src]

This method returns an Ordering between self and other. Read more

1.22.0
[src]

Compares and returns the maximum of two values. Read more

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]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl<'a, B: ?Sized> PartialOrd for Boo<'a, B> where
    B: PartialOrd + ToOwned
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

1.0.0
[src]

This method tests less than (for self and other) and is used by the < operator. Read more

1.0.0
[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

1.0.0
[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

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]

[src]

Formats the value using the given formatter.

impl<'a, B: ?Sized> Display for Boo<'a, B> where
    B: Display + ToOwned,
    <B as ToOwned>::Owned: Display
[src]

[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]

[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]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<'a, T: ?Sized + ToOwned> AsRef<T> for Boo<'a, T>
[src]

[src]

Performs the conversion.