pub enum Oco<'a, T: ?Sized + ToOwned + 'a> {
Borrowed(&'a T),
Counted(Rc<T>),
Owned(<T as ToOwned>::Owned),
}
Expand description
“Owned Clones Once”: a smart pointer that can be either a reference, an owned value, or a reference-counted pointer. This is useful for storing immutable values, such as strings, in a way that is cheap to clone and pass around.
The cost of the Clone
implementation depends on the branch. Cloning the Oco::Borrowed
variant simply copies the references (O(1)
). Cloning the Oco::Counted
variant increments a reference count (O(1)
). Cloning the Oco::Owned
variant requires an O(n)
clone of the data.
For an amortized O(1)
clone, you can use Oco::clone_inplace()
. Using this method,
Oco::Borrowed
and Oco::Counted
are still O(1)
. Oco::Owned
does a single O(n)
clone, but converts the object to the Oco::Counted
branch, which means future clones will
be O(1)
.
In general, you’ll either want to call clone_inplace()
once, before sharing the Oco
with
other parts of your application (so that all future clones are O(1)
), or simply use this as
if it is a Cow
with an additional branch for reference-counted values.
Variants§
Borrowed(&'a T)
A static reference to a value.
Counted(Rc<T>)
A reference counted pointer to a value.
Owned(<T as ToOwned>::Owned)
An owned value.
Implementations§
source§impl<'a, T: ?Sized + ToOwned> Oco<'a, T>
impl<'a, T: ?Sized + ToOwned> Oco<'a, T>
sourcepub fn into_owned(self) -> <T as ToOwned>::Owned
pub fn into_owned(self) -> <T as ToOwned>::Owned
Converts the value into an owned value.
sourcepub const fn is_borrowed(&self) -> bool
pub const fn is_borrowed(&self) -> bool
Checks if the value is Oco::Borrowed
.
§Examples
assert!(Oco::<str>::Borrowed("Hello").is_borrowed());
assert!(!Oco::<str>::Counted(Rc::from("Hello")).is_borrowed());
assert!(!Oco::<str>::Owned("Hello".to_string()).is_borrowed());
sourcepub const fn is_counted(&self) -> bool
pub const fn is_counted(&self) -> bool
Checks if the value is Oco::Counted
.
§Examples
assert!(Oco::<str>::Counted(Rc::from("Hello")).is_counted());
assert!(!Oco::<str>::Borrowed("Hello").is_counted());
assert!(!Oco::<str>::Owned("Hello".to_string()).is_counted());
sourcepub const fn is_owned(&self) -> bool
pub const fn is_owned(&self) -> bool
Checks if the value is Oco::Owned
.
§Examples
assert!(Oco::<str>::Owned("Hello".to_string()).is_owned());
assert!(!Oco::<str>::Borrowed("Hello").is_owned());
assert!(!Oco::<str>::Counted(Rc::from("Hello")).is_owned());
source§impl<'a, T> Oco<'a, T>
impl<'a, T> Oco<'a, T>
sourcepub fn clone_inplace(&mut self) -> Self
pub fn clone_inplace(&mut self) -> Self
Clones the value with inplace conversion into Oco::Counted
if it
was previously Oco::Owned
.
§Examples
let mut oco1 = Oco::<str>::Owned("Hello".to_string());
let oco2 = oco1.clone_inplace();
assert_eq!(oco1, oco2);
assert!(oco1.is_counted());
assert!(oco2.is_counted());
Trait Implementations§
source§impl<'a, T> Clone for Oco<'a, T>
impl<'a, T> Clone for Oco<'a, T>
source§fn clone(&self) -> Self
fn clone(&self) -> Self
Returns a new Oco
with the same value as this one.
If the value is Oco::Owned
, this will convert it into
Oco::Counted
, so that the next clone will be O(1).
§Examples
String
:
let oco = Oco::<str>::Owned("Hello".to_string());
let oco2 = oco.clone();
assert_eq!(oco, oco2);
assert!(oco2.is_counted());
Vec
:
let oco = Oco::<[u8]>::Owned(b"Hello".to_vec());
let oco2 = oco.clone();
assert_eq!(oco, oco2);
assert!(oco2.is_counted());
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a, T> Deserialize<'a> for Oco<'static, T>
impl<'a, T> Deserialize<'a> for Oco<'static, T>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'a>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'a>,
source§impl<T> Ord for Oco<'_, T>
impl<T> Ord for Oco<'_, T>
source§impl<'a, 'b, T: PartialEq> PartialEq<&'b [T]> for Oco<'a, [T]>
impl<'a, 'b, T: PartialEq> PartialEq<&'b [T]> for Oco<'a, [T]>
source§impl<'a, 'b> PartialEq<&'b str> for Oco<'a, str>
impl<'a, 'b> PartialEq<&'b str> for Oco<'a, str>
source§impl<'a, 'b, T: PartialEq> PartialEq<Cow<'b, [T]>> for Oco<'a, [T]>
impl<'a, 'b, T: PartialEq> PartialEq<Cow<'b, [T]>> for Oco<'a, [T]>
source§impl<'a, 'b> PartialEq<Cow<'b, str>> for Oco<'a, str>
impl<'a, 'b> PartialEq<Cow<'b, str>> for Oco<'a, str>
source§impl<T: PartialEq> PartialEq<Oco<'_, [T]>> for [T]
impl<T: PartialEq> PartialEq<Oco<'_, [T]>> for [T]
source§impl<T: PartialEq> PartialEq<Oco<'_, [T]>> for Vec<T>
impl<T: PartialEq> PartialEq<Oco<'_, [T]>> for Vec<T>
source§impl PartialEq<Oco<'_, str>> for String
impl PartialEq<Oco<'_, str>> for String
source§impl PartialEq<Oco<'_, str>> for str
impl PartialEq<Oco<'_, str>> for str
source§impl<'a, 'b, T: PartialEq> PartialEq<Oco<'a, [T]>> for &'b [T]
impl<'a, 'b, T: PartialEq> PartialEq<Oco<'a, [T]>> for &'b [T]
source§impl<'a, 'b, T: PartialEq> PartialEq<Oco<'a, [T]>> for Cow<'b, [T]>
impl<'a, 'b, T: PartialEq> PartialEq<Oco<'a, [T]>> for Cow<'b, [T]>
source§impl<'a, 'b> PartialEq<Oco<'a, str>> for &'b str
impl<'a, 'b> PartialEq<Oco<'a, str>> for &'b str
source§impl<'a, 'b> PartialEq<Oco<'a, str>> for Cow<'b, str>
impl<'a, 'b> PartialEq<Oco<'a, str>> for Cow<'b, str>
source§impl<'a, 'b, A, B> PartialEq<Oco<'b, B>> for Oco<'a, A>
impl<'a, 'b, A, B> PartialEq<Oco<'b, B>> for Oco<'a, A>
source§impl PartialEq<String> for Oco<'_, str>
impl PartialEq<String> for Oco<'_, str>
source§impl<T: PartialEq> PartialEq<Vec<T>> for Oco<'_, [T]>
impl<T: PartialEq> PartialEq<Vec<T>> for Oco<'_, [T]>
source§impl<'a, 'b, A, B> PartialOrd<Oco<'b, B>> for Oco<'a, A>
impl<'a, 'b, A, B> PartialOrd<Oco<'b, B>> for Oco<'a, A>
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