Enum laxcow::LaxCow

source ·
pub enum LaxCow<'a, B: ?Sized, O = B> {
    Borrowed(&'a B),
    Owned(O),
}
Expand description

Clone-on-write smart pointer with relaxed trait constraints relative to Cow.

LaxCow does not constrain owned type in its type definition, but in methods that specifically require this. Thus, Owned type is generic and may need to be explicitly defined when instantiating. Also, LaxCow isn’t strictly clone-on-write as not all instances of it support writing i.e. mutable access.

Examples

Simple usage

use laxcow::LaxCow;

let lc = LaxCow::Borrowed("foobar");

let lc2 = lc.clone();
assert_eq!(lc2, LaxCow::<_, String>::Borrowed("foobar"));

let owned = lc.into_owned();
assert_eq!(owned, "foobar".to_owned());

Usage not possible with Cow

Storing a borrowed struct which doesn’t implement Clone. This is possible because LaxCow::Owned variant is not restricted by the LaxCow::Borrowed variant via ToOwned trait.

use laxcow::LaxCow;

struct Foo;

let laxcow = LaxCow::<_, ()>::Borrowed(&Foo);

Cow definition by wrapping LaxCow

use laxcow::LaxCow;

struct Cow<'a, T: ?Sized + ToOwned>(LaxCow::<'a, T, T::Owned>);

Variants§

§

Borrowed(&'a B)

§

Owned(O)

Implementations§

source§

impl<'a, B: ?Sized, O> LaxCow<'a, B, O>

source

pub const fn is_borrowed(&self) -> bool

Returns true if LaxCow contains borrowed item, false otherwise.

Examples
use laxcow::LaxCow;

// We don't care what owned type is as it is not used.
let borrowed = LaxCow::<i32, ()>::Borrowed(&42);

assert!(borrowed.is_borrowed());
source

pub const fn is_owned(&self) -> bool

Returns true if LaxCow contains owned item, false otherwise.

Examples
use laxcow::LaxCow;

// We don't care what borrowed type is as it is not used.
let owned = LaxCow::<(), i32>::Owned(42);

assert!(owned.is_owned());
source

pub fn to_mut(&mut self) -> &mut Owhere B: ToOwned<Owned = O>,

Returns mutable reference to the owned item.

If the item is currently borrowed, it will converted into owned via ToOwned trait before returning mutable reference to it.

Examples
use laxcow::LaxCow;

let mut borrowed = LaxCow::Borrowed(&42);
let mut owned = LaxCow::<i32, _>::Owned(42);

*borrowed.to_mut() += 10;
*owned.to_mut() += 10;

assert_eq!(borrowed, LaxCow::Owned(52));
assert_eq!(owned, LaxCow::Owned(52));
source

pub fn try_as_owned_mut(&mut self) -> Result<&mut O, &mut &'a B>

Attempts to get a mutable reference to the owned variant of the instance.

No conversion is attempted but mutable reference to the borrowed item is returned as an error.

Examples
use laxcow::LaxCow;

let mut borrowed = LaxCow::<_, String>::Borrowed("borrow");
let mut owned = LaxCow::<str, _>::Owned("own".to_owned());

assert_eq!(borrowed.try_as_owned_mut(), Err(&mut "borrow"));
assert_eq!(owned.try_as_owned_mut(), Ok(&mut "own".to_owned()));

As the borrowed item is not mutable referenced, it cannot be modified. However, the reference itself can be modified:

use laxcow::LaxCow;

let another = "another borrow";
let mut borrowed = LaxCow::<_, String>::Borrowed("borrow");

*borrowed.try_as_owned_mut().unwrap_err() = another;

assert_eq!(borrowed, LaxCow::<_, String>::Borrowed("another borrow"));
source

pub fn into_owned(self) -> Owhere B: ToOwned<Owned = O>,

Consumes LaxCow and returns owned item as is, or borrowed item via ToOwned trait conversion.

Examples
use laxcow::LaxCow;

let borrowed: LaxCow<_, String> = LaxCow::Borrowed("foobar");
let owned: LaxCow<str, _> = LaxCow::Owned("foobar".to_owned());

assert_eq!(borrowed.into_owned(), "foobar".to_owned());
assert_eq!(owned.into_owned(), "foobar".to_owned());
source

pub fn try_into_owned(self) -> Result<O, &'a B>

Attempts to transform the instance into its owned variant.

No conversion is attempted but the borrowed item is returned as an error.

Examples
use laxcow::LaxCow;

let mut borrowed = LaxCow::<_, String>::Borrowed("borrow");
let mut owned = LaxCow::<str, _>::Owned("own".to_owned());

assert_eq!(borrowed.try_into_owned(), Err("borrow"));
assert_eq!(owned.try_into_owned(), Ok("own".to_owned()));

Trait Implementations§

source§

impl<B: ?Sized, O> AsRef<B> for LaxCow<'_, B, O>where O: AsRef<B>,

source§

fn as_ref(&self) -> &B

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<B: ?Sized, O> Clone for LaxCow<'_, B, O>where O: Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<B, O> Debug for LaxCow<'_, B, O>where B: Debug + ?Sized, O: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<B: ?Sized, O> Default for LaxCow<'_, B, O>where O: Default,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<B: ?Sized, O> Deref for LaxCow<'_, B, O>where O: Borrow<B>,

§

type Target = B

The resulting type after dereferencing.
source§

fn deref(&self) -> &B

Dereferences the value.
source§

impl<B, O> Display for LaxCow<'_, B, O>where B: Display + ?Sized, O: Display,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, B, O> From<Cow<'a, B>> for LaxCow<'a, B, O>where B: ToOwned<Owned = O> + ?Sized,

source§

fn from(cow: Cow<'a, B>) -> Self

Converts to this type from the input type.
source§

impl<'a, B, O> From<LaxCow<'a, B, O>> for Cow<'a, B>where B: ToOwned<Owned = O> + ?Sized,

source§

fn from(laxcow: LaxCow<'a, B, O>) -> Self

Converts to this type from the input type.
source§

impl<B, O> Hash for LaxCow<'_, B, O>where O: Borrow<B>, B: Hash + ?Sized,

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<B, O> Ord for LaxCow<'_, B, O>where O: Borrow<B>, B: Ord + ?Sized,

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<'a, 'b, B, O, B2: ?Sized, O2> PartialEq<LaxCow<'a, B2, O2>> for LaxCow<'b, B, O>where B: PartialEq<B2> + ?Sized, O: Borrow<B>, O2: Borrow<B2>,

source§

fn eq(&self, other: &LaxCow<'a, B2, O2>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<B, O> PartialOrd<LaxCow<'_, B, O>> for LaxCow<'_, B, O>where B: PartialOrd + ?Sized, O: Borrow<B>,

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<B, O> Eq for LaxCow<'_, B, O>where B: Eq + ?Sized, O: Borrow<B>,

Auto Trait Implementations§

§

impl<'a, B: ?Sized, O> RefUnwindSafe for LaxCow<'a, B, O>where B: RefUnwindSafe, O: RefUnwindSafe,

§

impl<'a, B: ?Sized, O> Send for LaxCow<'a, B, O>where B: Sync, O: Send,

§

impl<'a, B: ?Sized, O> Sync for LaxCow<'a, B, O>where B: Sync, O: Sync,

§

impl<'a, B: ?Sized, O> Unpin for LaxCow<'a, B, O>where O: Unpin,

§

impl<'a, B: ?Sized, O> UnwindSafe for LaxCow<'a, B, O>where B: RefUnwindSafe, O: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.