pub enum Oco<'a, T>{
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> Oco<'a, T>
impl<'a, T> 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) -> Oco<'a, T>
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, T> Clone for Oco<'a, T>
impl<'a, T> Clone for Oco<'a, T>
source§fn clone(&self) -> Oco<'a, T>
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)
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<Oco<'static, T>, <D as Deserializer<'a>>::Error>where
D: Deserializer<'a>,
fn deserialize<D>(
deserializer: D
) -> Result<Oco<'static, T>, <D as Deserializer<'a>>::Error>where
D: Deserializer<'a>,
source§impl IntoAttribute for Oco<'static, str>
impl IntoAttribute for Oco<'static, str>
source§impl<T> Ord for Oco<'_, T>
impl<T> Ord for Oco<'_, T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a, 'b, T> PartialEq<&'b [T]> for Oco<'a, [T]>
impl<'a, 'b, T> 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<Cow<'b, [T]>> for Oco<'a, [T]>
impl<'a, 'b, T> 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<Oco<'_, [T]>> for [T]
impl<T> PartialEq<Oco<'_, [T]>> for [T]
source§impl PartialEq<Oco<'_, str>> for str
impl PartialEq<Oco<'_, str>> for str
source§impl<'a, 'b, T> PartialEq<Oco<'a, [T]>> for &'b [T]
impl<'a, 'b, T> PartialEq<Oco<'a, [T]>> for &'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, 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<Vec<T>> for Oco<'_, [T]>
impl<T> 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 moresource§impl<'a, T> Serialize for Oco<'a, T>
impl<'a, T> Serialize for Oco<'a, T>
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Eq for Oco<'_, T>
Auto Trait Implementations§
impl<'a, T> Freeze for Oco<'a, T>
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>
impl<'a, T> UnwindSafe for Oco<'a, T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Casing<T> for T
impl<T> Casing<T> for T
source§fn to_case(&self, case: Case) -> String
fn to_case(&self, case: Case) -> String
self
and create a new
String
with the same pattern and delimeter as case
. It will split on boundaries
defined at Boundary::defaults()
. Read moresource§fn with_boundaries(&self, bs: &[Boundary]) -> StateConverter<'_, T>
fn with_boundaries(&self, bs: &[Boundary]) -> StateConverter<'_, T>
StateConverter
struct initialized with the boundaries
provided. Read moresource§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<CustErr, T, Request> FromReq<Cbor, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<Cbor, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<GetUrl, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<GetUrl, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<PostUrl, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<PostUrl, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moresource§impl<T> Serializable for Twhere
T: DeserializeOwned + Serialize,
impl<T> Serializable for Twhere
T: DeserializeOwned + Serialize,
source§impl<T> ToHtmlElement for T
impl<T> ToHtmlElement for T
source§fn to_leptos_element(self) -> HtmlElement<AnyElement>
fn to_leptos_element(self) -> HtmlElement<AnyElement>
HtmlElement
.