Enum oco_ref::Oco

source ·
pub enum Oco<'a, T: ?Sized + ToOwned + 'a> {
    Borrowed(&'a T),
    Counted(Arc<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 Clone implementation is amortized O(1). 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 upgrades it to Oco::Counted, which requires an O(n) clone of the data, but all subsequent clones will be O(1).

Variants§

§

Borrowed(&'a T)

A static reference to a value.

§

Counted(Arc<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>

source

pub fn into_owned(self) -> <T as ToOwned>::Owned

Converts the value into an owned value.

source

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(Arc::from("Hello")).is_borrowed());
assert!(!Oco::<str>::Owned("Hello".to_string()).is_borrowed());
source

pub const fn is_counted(&self) -> bool

Checks if the value is Oco::Counted.

§Examples
assert!(Oco::<str>::Counted(Arc::from("Hello")).is_counted());
assert!(!Oco::<str>::Borrowed("Hello").is_counted());
assert!(!Oco::<str>::Owned("Hello".to_string()).is_counted());
source

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(Arc::from("Hello")).is_owned());
source§

impl Oco<'_, str>

source

pub fn as_str(&self) -> &str

Returns a &str slice of this Oco.

§Examples
let oco = Oco::<str>::Borrowed("Hello");
let s: &str = oco.as_str();
assert_eq!(s, "Hello");
source§

impl Oco<'_, CStr>

source

pub fn as_c_str(&self) -> &CStr

Returns a &CStr slice of this Oco.

§Examples
use std::ffi::CStr;

let oco =
    Oco::<CStr>::Borrowed(CStr::from_bytes_with_nul(b"Hello\0").unwrap());
let s: &CStr = oco.as_c_str();
assert_eq!(s, CStr::from_bytes_with_nul(b"Hello\0").unwrap());
source§

impl Oco<'_, OsStr>

source

pub fn as_os_str(&self) -> &OsStr

Returns a &OsStr slice of this Oco.

§Examples
use std::ffi::OsStr;

let oco = Oco::<OsStr>::Borrowed(OsStr::new("Hello"));
let s: &OsStr = oco.as_os_str();
assert_eq!(s, OsStr::new("Hello"));
source§

impl Oco<'_, Path>

source

pub fn as_path(&self) -> &Path

Returns a &Path slice of this Oco.

§Examples
use std::path::Path;

let oco = Oco::<Path>::Borrowed(Path::new("Hello"));
let s: &Path = oco.as_path();
assert_eq!(s, Path::new("Hello"));
source§

impl<T> Oco<'_, [T]>
where [T]: ToOwned,

source

pub fn as_slice(&self) -> &[T]

Returns a &[T] slice of this Oco.

§Examples
let oco = Oco::<[u8]>::Borrowed(b"Hello");
let s: &[u8] = oco.as_slice();
assert_eq!(s, b"Hello");
source§

impl<'a, T> Oco<'a, T>
where T: ?Sized + ToOwned + 'a, for<'b> Arc<T>: From<&'b T>,

source

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());
source

pub fn upgrade_inplace(&mut self)

Converts the value into its cheaply-clonable form in place. In other words, if it is currently Oco::Owned, converts into Oco::Counted in an O(n) operation, so that all future clones are O(1).

§Examples
let mut oco = Oco::<str>::Owned("Hello".to_string());
oco.upgrade_inplace();
assert!(oco.is_counted());

Trait Implementations§

source§

impl<'a, 'b> Add<&'b str> for Oco<'a, str>

§

type Output = Oco<'static, str>

The resulting type after applying the + operator.
source§

fn add(self, rhs: &'b str) -> Self::Output

Performs the + operation. Read more
source§

impl<'a, 'b> Add<Cow<'b, str>> for Oco<'a, str>

§

type Output = Oco<'static, str>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Cow<'b, str>) -> Self::Output

Performs the + operation. Read more
source§

impl<'a, 'b> Add<Oco<'b, str>> for Oco<'a, str>

§

type Output = Oco<'static, str>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Oco<'b, str>) -> Self::Output

Performs the + operation. Read more
source§

impl AsRef<Path> for Oco<'_, OsStr>

source§

fn as_ref(&self) -> &Path

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

impl AsRef<Path> for Oco<'_, str>

source§

fn as_ref(&self) -> &Path

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

impl<T: ?Sized + ToOwned> AsRef<T> for Oco<'_, T>

source§

fn as_ref(&self) -> &T

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

impl<T: ?Sized + ToOwned> Borrow<T> for Oco<'_, T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<'a, T> Clone for Oco<'a, T>
where T: ?Sized + ToOwned + 'a, for<'b> Arc<T>: From<&'b T>,

source§

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)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Oco<'_, T>
where T: ToOwned + ?Sized + Debug,

source§

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

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

impl<T> Default for Oco<'_, T>
where T: ToOwned + ?Sized, T::Owned: Default,

source§

fn default() -> Self

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

impl<T: ?Sized + ToOwned> Deref for Oco<'_, T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<'a, T> Deserialize<'a> for Oco<'static, T>
where T: ?Sized + ToOwned + 'a, T::Owned: DeserializeOwned,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'a>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> Display for Oco<'_, T>
where T: ToOwned + ?Sized + Display,

source§

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

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

impl<'a, T, const N: usize> From<&'a [T; N]> for Oco<'a, [T]>
where [T]: ToOwned,

source§

fn from(v: &'a [T; N]) -> Self

Converts to this type from the input type.
source§

impl<'a, T> From<&'a T> for Oco<'a, T>
where T: ToOwned + ?Sized,

source§

fn from(v: &'a T) -> Self

Converts to this type from the input type.
source§

impl<T> From<Arc<T>> for Oco<'_, T>
where T: ToOwned + ?Sized,

source§

fn from(v: Arc<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<Box<T>> for Oco<'_, T>
where T: ToOwned + ?Sized,

source§

fn from(v: Box<T>) -> Self

Converts to this type from the input type.
source§

impl<'a, T> From<Cow<'a, T>> for Oco<'a, T>
where T: ToOwned + ?Sized,

source§

fn from(v: Cow<'a, T>) -> Self

Converts to this type from the input type.
source§

impl From<Oco<'_, str>> for String

source§

fn from(v: Oco<'_, str>) -> Self

Converts to this type from the input type.
source§

impl<'a, T> From<Oco<'a, T>> for Cow<'a, T>
where T: ToOwned + ?Sized,

source§

fn from(value: Oco<'a, T>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<Oco<'a, str>> for Oco<'a, [u8]>

source§

fn from(v: Oco<'a, str>) -> Self

Converts to this type from the input type.
source§

impl From<String> for Oco<'_, str>

source§

fn from(v: String) -> Self

Converts to this type from the input type.
source§

impl<T> From<Vec<T>> for Oco<'_, [T]>
where [T]: ToOwned<Owned = Vec<T>>,

source§

fn from(v: Vec<T>) -> Self

Converts to this type from the input type.
source§

impl<'a> FromIterator<Oco<'a, str>> for String

source§

fn from_iter<T: IntoIterator<Item = Oco<'a, str>>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<T> Hash for Oco<'_, T>
where T: ToOwned + ?Sized + Hash,

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<T> Ord for Oco<'_, T>
where T: ToOwned + ?Sized + Ord,

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) -> Self
where Self: Sized,

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

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

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

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

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

impl<'a, 'b, T: PartialEq> PartialEq<&'b [T]> for Oco<'a, [T]>
where [T]: ToOwned,

source§

fn eq(&self, other: &&'b [T]) -> 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<'a, 'b> PartialEq<&'b str> for Oco<'a, str>

source§

fn eq(&self, other: &&'b str) -> 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<T: PartialEq> PartialEq<[T]> for Oco<'_, [T]>
where [T]: ToOwned,

source§

fn eq(&self, other: &[T]) -> 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<'a, 'b, T: PartialEq> PartialEq<Cow<'b, [T]>> for Oco<'a, [T]>
where [T]: ToOwned,

source§

fn eq(&self, other: &Cow<'b, [T]>) -> 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<'a, 'b> PartialEq<Cow<'b, str>> for Oco<'a, str>

source§

fn eq(&self, other: &Cow<'b, str>) -> 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<T: PartialEq> PartialEq<Oco<'_, [T]>> for [T]
where [T]: ToOwned,

source§

fn eq(&self, other: &Oco<'_, [T]>) -> 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<T: PartialEq> PartialEq<Oco<'_, [T]>> for Vec<T>
where [T]: ToOwned,

source§

fn eq(&self, other: &Oco<'_, [T]>) -> 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 PartialEq<Oco<'_, str>> for String

source§

fn eq(&self, other: &Oco<'_, str>) -> 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 PartialEq<Oco<'_, str>> for str

source§

fn eq(&self, other: &Oco<'_, str>) -> 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<'a, 'b, T: PartialEq> PartialEq<Oco<'a, [T]>> for &'b [T]
where [T]: ToOwned,

source§

fn eq(&self, other: &Oco<'a, [T]>) -> 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<'a, 'b, T: PartialEq> PartialEq<Oco<'a, [T]>> for Cow<'b, [T]>
where [T]: ToOwned,

source§

fn eq(&self, other: &Oco<'a, [T]>) -> 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<'a, 'b> PartialEq<Oco<'a, str>> for &'b str

source§

fn eq(&self, other: &Oco<'a, str>) -> 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<'a, 'b> PartialEq<Oco<'a, str>> for Cow<'b, str>

source§

fn eq(&self, other: &Oco<'a, str>) -> 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<'a, 'b, A, B> PartialEq<Oco<'b, B>> for Oco<'a, A>
where A: PartialEq<B> + ToOwned + ?Sized, B: ToOwned + ?Sized,

source§

fn eq(&self, other: &Oco<'b, B>) -> 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 PartialEq<String> for Oco<'_, str>

source§

fn eq(&self, other: &String) -> 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<T: PartialEq> PartialEq<Vec<T>> for Oco<'_, [T]>
where [T]: ToOwned,

source§

fn eq(&self, other: &Vec<T>) -> 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 PartialEq<str> for Oco<'_, str>

source§

fn eq(&self, other: &str) -> 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<'a, 'b, A, B> PartialOrd<Oco<'b, B>> for Oco<'a, A>
where A: PartialOrd<B> + ToOwned + ?Sized, B: ToOwned + ?Sized,

source§

fn partial_cmp(&self, other: &Oco<'b, B>) -> 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<'a, T> Serialize for Oco<'a, T>
where T: ?Sized + ToOwned + 'a, for<'b> &'b T: Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: ?Sized + ToOwned + Eq> Eq for Oco<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for Oco<'a, T>
where <T as ToOwned>::Owned: Freeze, T: ?Sized,

§

impl<'a, T> RefUnwindSafe for Oco<'a, T>

§

impl<'a, T> Send for Oco<'a, T>
where <T as ToOwned>::Owned: Send, T: Sync + Send + ?Sized,

§

impl<'a, T> Sync for Oco<'a, T>
where <T as ToOwned>::Owned: Sync, T: Sync + Send + ?Sized,

§

impl<'a, T> Unpin for Oco<'a, T>
where <T as ToOwned>::Owned: Unpin, T: ?Sized,

§

impl<'a, T> UnwindSafe for Oco<'a, T>
where <T as ToOwned>::Owned: UnwindSafe, T: RefUnwindSafe + ?Sized,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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 T
where 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 T
where 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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,