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§
Implementations§
source§impl<'a, B: ?Sized, O> LaxCow<'a, B, O>
impl<'a, B: ?Sized, O> LaxCow<'a, B, O>
sourcepub const fn is_borrowed(&self) -> bool
pub const fn is_borrowed(&self) -> bool
sourcepub fn to_mut(&mut self) -> &mut Owhere
B: ToOwned<Owned = O>,
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));
sourcepub fn try_as_owned_mut(&mut self) -> Result<&mut O, &mut &'a B>
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"));
sourcepub fn into_owned(self) -> Owhere
B: ToOwned<Owned = O>,
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());
sourcepub fn try_into_owned(self) -> Result<O, &'a B>
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, O> Ord for LaxCow<'_, B, O>where
O: Borrow<B>,
B: Ord + ?Sized,
impl<B, O> Ord for LaxCow<'_, B, O>where O: Borrow<B>, B: Ord + ?Sized,
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>,
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§impl<B, O> PartialOrd<LaxCow<'_, B, O>> for LaxCow<'_, B, O>where
B: PartialOrd + ?Sized,
O: Borrow<B>,
impl<B, O> PartialOrd<LaxCow<'_, B, O>> for LaxCow<'_, B, O>where B: PartialOrd + ?Sized, O: Borrow<B>,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more