Enum Oco Copy item path 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)
.
A static reference to a value.
A reference counted pointer to a value.
Converts the value into an owned value.
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());
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());
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());
Returns a &str
slice of this Oco
.
§ Examples
let oco = Oco::<str>::Borrowed("Hello" );
let s: & str = oco.as_str();
assert_eq! (s, "Hello" );
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());
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" ));
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" ));
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" );
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());
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());
The resulting type after applying the +
operator.
The resulting type after applying the +
operator.
The resulting type after applying the +
operator.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value.
Read more 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());
Performs copy-assignment from
source
.
Read more Formats the value using the given formatter.
Read more Returns the “default value” for a type.
Read more The resulting type after dereferencing.
Dereferences the value.
Deserialize this value from the given Serde deserializer.
Read more Formats the value using the given formatter.
Read more Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Compares and returns the maximum of two values.
Read more Compares and returns the minimum of two values.
Read more Restrict a value to a certain interval.
Read more Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
This method returns an ordering between
self
and
other
values if one exists.
Read more Tests less than (for
self
and
other
) and is used by the
<
operator.
Read more Tests less than or equal to (for
self
and
other
) and is used by the
<=
operator.
Read more Tests greater than (for
self
and
other
) and is used by the
>
operator.
Read more Tests greater than or equal to (for
self
and
other
) and is used by
the
>=
operator.
Read more Serialize this value into the given Serde serializer.
Read more Immutably borrows from an owned value.
Read more Mutably borrows from an owned value.
Read more 🔬 This is a nightly-only experimental API. (clone_to_uninit
)
Performs copy-assignment from
self
to
dest
.
Read more Returns the argument unchanged.
Calls U::from(self)
.
That is, this conversion is whatever the implementation of
From <T> for U
chooses to do.
🔬 This is a nightly-only experimental API. (arbitrary_self_types
)
The target type on which the method may be called.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning.
Read more Uses borrowed data to replace owned data, usually by cloning.
Read more Converts the given value to a
String
.
Read more The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.