Enum Oco

Source
pub enum Oco<'a, T>
where T: ToOwned + 'a + ?Sized,
{ 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 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(Rc<T>)

A reference counted pointer to a value.

§

Owned(<T as ToOwned>::Owned)

An owned value.

Implementations§

Source§

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

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(Rc::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(Rc::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(Rc::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: ToOwned + 'a + ?Sized, Rc<T>: for<'b> From<&'b T>,

Source

pub fn clone_inplace(&mut self) -> Oco<'a, T>

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, 'b> Add<&'b str> for Oco<'a, str>

Source§

type Output = Oco<'static, str>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'b str) -> <Oco<'a, str> as Add<&'b str>>::Output

Performs the + operation. Read more
Source§

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

Source§

type Output = Oco<'static, str>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Cow<'b, str>) -> <Oco<'a, str> as Add<Cow<'b, str>>>::Output

Performs the + operation. Read more
Source§

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

Source§

type Output = Oco<'static, str>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Oco<'b, str>) -> <Oco<'a, str> as Add<Oco<'b, str>>>::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> AsRef<T> for Oco<'_, T>
where T: ToOwned + ?Sized,

Source§

fn as_ref(&self) -> &T

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

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn clone(&self) -> Oco<'a, T>

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: Debug + ToOwned + ?Sized,

Source§

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

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

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

Source§

fn default() -> Oco<'_, T>

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

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

Source§

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: ToOwned + 'a + ?Sized, <T as ToOwned>::Owned: DeserializeOwned,

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Oco<'static, T>, <D as Deserializer<'a>>::Error>
where D: Deserializer<'a>,

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

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

Source§

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

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]) -> Oco<'a, [T]>

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) -> Oco<'a, T>

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>) -> Oco<'_, T>

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>) -> Oco<'a, T>

Converts to this type from the input type.
Source§

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

Source§

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

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>) -> Cow<'a, T>

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>) -> Oco<'a, [u8]>

Converts to this type from the input type.
Source§

impl From<Oco<'static, str>> for TextProp

Source§

fn from(s: Oco<'static, str>) -> TextProp

Converts to this type from the input type.
Source§

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

Source§

fn from(v: Rc<T>) -> Oco<'_, T>

Converts to this type from the input type.
Source§

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

Source§

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

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>) -> Oco<'_, [T]>

Converts to this type from the input type.
Source§

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

Source§

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

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 IntoAttribute for Oco<'static, str>

Source§

fn into_attribute(self) -> Attribute

Converts the object into an Attribute.
Source§

fn into_attribute_boxed(self: Box<Oco<'static, str>>) -> Attribute

Helper function for dealing with Box<dyn IntoAttribute>.
Source§

impl IntoStyle for Oco<'static, str>

Source§

fn into_style(self) -> Style

Converts the object into a Style.
Source§

fn into_style_boxed(self: Box<Oco<'static, str>>) -> Style

Helper function for dealing with Box<dyn IntoStyle>.
Source§

impl IntoView for Oco<'static, str>

Source§

fn into_view(self) -> View

Converts the value into View.
Source§

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

Source§

fn cmp(&self, other: &Oco<'_, T>) -> 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,

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

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

Source§

fn eq(&self, other: &&'b [T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn eq(&self, other: &[T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn eq(&self, other: &Cow<'b, [T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn eq(&self, other: &Oco<'_, [T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn eq(&self, other: &Oco<'_, [T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn eq(&self, other: &Oco<'a, [T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn eq(&self, other: &Vec<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

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

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

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

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: ToOwned + 'a + ?Sized, &'b T: for<'b> Serialize,

Source§

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

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

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

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>

§

impl<'a, T> !Sync for Oco<'a, T>

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Serializable for T

Source§

fn ser(&self) -> Result<String, SerializationError>

Serializes the object to a string.
Source§

fn de(json: &str) -> Result<T, SerializationError>

Deserializes the object from some bytes.
Source§

impl<T> ToHtmlElement for T
where T: AsRef<Element>,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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§

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>,

Source§

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>,

Source§

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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

impl<El> ElementDescriptorBounds for El
where El: Debug,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T