Enum abi_stable::std_types::RCow
source · #[repr(C)]pub enum RCow<B, O> {
Borrowed(B),
Owned(O),
}
Expand description
Ffi-safe equivalent of std::borrow::Cow
.
This has type aliases for the three most common usecases:
-
RCowStr
: contains anRStr<'_>
or anRString
. -
RCowSlice
: contains anRSlice<'_, T>
or anRVec<T>
. -
RCowVal
: contains a&T
or aT
.
§Example
§Using a RCowStr<'a>
.
This implements a solution to the well known fizzbuzz problem.
use abi_stable::std_types::{RCow, RCowStr};
fn fizzbuzz(n: u32) -> RCowStr<'static> {
match (n % 3, n % 5) {
(0, 0) => RCow::from("FizzBuzz"),
(0, _) => RCow::from("Fizz"),
(_, 0) => RCow::from("Buzz"),
(_, _) => RCow::from(n.to_string()),
}
}
for n in 1..=100 {
println!("{}", fizzbuzz(n));
}
Note: this example allocates when the number is neither a multiple of 5 or 3.
Variants§
Implementations§
source§impl<B> RCow<B, B::ROwned>where
B: IntoOwned,
impl<B> RCow<B, B::ROwned>where
B: IntoOwned,
sourcepub fn to_mut(&mut self) -> &mut B::ROwned
pub fn to_mut(&mut self) -> &mut B::ROwned
Get a mutable reference to the owned form of RCow, converting to the owned form if it is currently the borrowed form.
§Examples
use abi_stable::std_types::{RCow, RCowStr};
let mut cow: RCowStr<'_> = RCow::from("Hello");
assert_eq!(&*cow, "Hello");
assert!(cow.is_borrowed());
cow.to_mut().push_str(", world!");
assert!(cow.is_owned());
assert_eq!(cow, RCow::from("Hello, world!"));
sourcepub fn into_owned(self) -> B::ROwned
pub fn into_owned(self) -> B::ROwned
Unwraps into the owned owner form of RCow, converting to the owned form if it is currently the borrowed form.
§Examples
use abi_stable::std_types::{RCow, RCowStr};
let mut cow: RCowStr<'_> = RCow::from("Hello");
assert_eq!(&*cow, "Hello");
let mut buff = cow.into_owned();
buff.push_str(", world!");
assert_eq!(&*buff, "Hello, world!");
sourcepub fn borrowed(&self) -> &<B as Deref>::Target
pub fn borrowed(&self) -> &<B as Deref>::Target
Gets the contents of the RCow casted to the borrowed variant.
§Examples
use abi_stable::std_types::{RCow, RCowSlice, RSlice};
{
let cow: RCowSlice<'_, u8> = RCow::from(&[0, 1, 2, 3][..]);
assert_eq!(cow.borrowed(), RSlice::from_slice(&[0, 1, 2, 3]));
}
{
let cow: RCowSlice<'_, u8> = RCow::from(vec![0, 1, 2, 3]);
assert_eq!(cow.borrowed(), RSlice::from_slice(&[0, 1, 2, 3]));
}
source§impl<B, O> RCow<B, O>
impl<B, O> RCow<B, O>
sourcepub const fn is_borrowed(&self) -> bool
pub const fn is_borrowed(&self) -> bool
Whether this is a borrowing RCow.
§Examples
use abi_stable::std_types::{RCow, RCowSlice};
{
let cow: RCowSlice<'_, u8> = RCow::from(&[0, 1, 2, 3][..]);
assert!(cow.is_borrowed());
}
{
let cow: RCowSlice<'_, u8> = RCow::from(vec![0, 1, 2, 3]);
assert!(!cow.is_borrowed());
}
source§impl<'a> RCow<RStr<'a>, RString>
impl<'a> RCow<RStr<'a>, RString>
sourcepub const fn from_str(this: &'a str) -> Self
pub const fn from_str(this: &'a str) -> Self
For converting a &'a [T]
to an RCowSlice<'a, T>
,
most useful when converting from &'a [T;N]
because it coerces the array to a slice.
§Example
use abi_stable::std_types::{RCow, RCowStr};
const C: RCowStr<'_> = RCow::from_str("hello");
assert_eq!(C, "hello");
source§impl<'a, T> RCow<RSlice<'a, T>, RVec<T>>
impl<'a, T> RCow<RSlice<'a, T>, RVec<T>>
sourcepub const fn from_slice(this: &'a [T]) -> Self
pub const fn from_slice(this: &'a [T]) -> Self
For converting a &'a [T]
to an RCowSlice<'a, T>
,
most useful when converting from &'a [T;N]
because it coerces the array to a slice.
§Example
use abi_stable::std_types::{RCow, RCowSlice};
const C: RCowSlice<'_, u8> = RCow::from_slice(&[3, 5, 8]);
assert_eq!(C, [3, 5, 8]);
Trait Implementations§
source§impl<'a, T> From<Cow<'a, T>> for RCow<T::RefC, T::ROwned>where
T: ?Sized + RCowCompatibleRef<'a>,
impl<'a, T> From<Cow<'a, T>> for RCow<T::RefC, T::ROwned>where
T: ?Sized + RCowCompatibleRef<'a>,
source§impl<B, O> GetStaticEquivalent_ for RCow<B, O>where
B: __StableAbi,
O: __StableAbi,
impl<B, O> GetStaticEquivalent_ for RCow<B, O>where
B: __StableAbi,
O: __StableAbi,
source§type StaticEquivalent = _static_RCow<<B as GetStaticEquivalent_>::StaticEquivalent, <O as GetStaticEquivalent_>::StaticEquivalent>
type StaticEquivalent = _static_RCow<<B as GetStaticEquivalent_>::StaticEquivalent, <O as GetStaticEquivalent_>::StaticEquivalent>
'static
equivalent of Self
source§impl<B> Ord for RCow<B, B::ROwned>
impl<B> Ord for RCow<B, B::ROwned>
source§impl<U: PartialEq<T>, T, const N: usize> PartialEq<RCow<RSlice<'_, T>, RVec<T>>> for [U; N]where
T: Clone,
impl<U: PartialEq<T>, T, const N: usize> PartialEq<RCow<RSlice<'_, T>, RVec<T>>> for [U; N]where
T: Clone,
source§impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for &[U]
impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for &[U]
source§impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for [U]
impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for [U]
source§impl<U, T, const N: usize> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for [U; N]
impl<U, T, const N: usize> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for [U; N]
source§impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for Cow<'_, [U]>
impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for Cow<'_, [U]>
source§impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for Vec<U>
impl<U, T> PartialOrd<RCow<RSlice<'_, T>, RVec<T>>> for Vec<U>
source§impl<T, U> PartialOrd<RCow<RSlice<'_, U>, RVec<U>>> for RSlice<'_, T>
impl<T, U> PartialOrd<RCow<RSlice<'_, U>, RVec<U>>> for RSlice<'_, T>
source§impl<T, U> PartialOrd<RCow<RSlice<'_, U>, RVec<U>>> for RSliceMut<'_, T>
impl<T, U> PartialOrd<RCow<RSlice<'_, U>, RVec<U>>> for RSliceMut<'_, T>
source§impl<T, U> PartialOrd<RCow<RSlice<'_, U>, RVec<U>>> for RVec<T>
impl<T, U> PartialOrd<RCow<RSlice<'_, U>, RVec<U>>> for RVec<T>
source§impl PartialOrd<RCow<RStr<'_>, RString>> for &str
impl PartialOrd<RCow<RStr<'_>, RString>> for &str
source§impl PartialOrd<RCow<RStr<'_>, RString>> for Cow<'_, str>
impl PartialOrd<RCow<RStr<'_>, RString>> for Cow<'_, str>
source§impl PartialOrd<RCow<RStr<'_>, RString>> for RStr<'_>
impl PartialOrd<RCow<RStr<'_>, RString>> for RStr<'_>
source§impl PartialOrd<RCow<RStr<'_>, RString>> for RString
impl PartialOrd<RCow<RStr<'_>, RString>> for RString
source§impl PartialOrd<RCow<RStr<'_>, RString>> for String
impl PartialOrd<RCow<RStr<'_>, RString>> for String
source§impl PartialOrd<RCow<RStr<'_>, RString>> for str
impl PartialOrd<RCow<RStr<'_>, RString>> for str
source§impl<B, V> PartialOrd<RCow<V, <V as IntoOwned>::ROwned>> for RCow<B, B::ROwned>
impl<B, V> PartialOrd<RCow<V, <V as IntoOwned>::ROwned>> for RCow<B, B::ROwned>
source§impl<B, O> StableAbi for RCow<B, O>where
B: __StableAbi,
O: __StableAbi,
impl<B, O> StableAbi for RCow<B, O>where
B: __StableAbi,
O: __StableAbi,
source§type IsNonZeroType = False
type IsNonZeroType = False
source§const LAYOUT: &'static TypeLayout = _
const LAYOUT: &'static TypeLayout = _
source§const ABI_CONSTS: AbiConsts = _
const ABI_CONSTS: AbiConsts = _
const
-equivalents of the associated types.impl<B> Copy for RCow<B, B::ROwned>
impl<B> Eq for RCow<B, B::ROwned>
Auto Trait Implementations§
impl<B, O> Freeze for RCow<B, O>
impl<B, O> RefUnwindSafe for RCow<B, O>where
B: RefUnwindSafe,
O: RefUnwindSafe,
impl<B, O> Send for RCow<B, O>
impl<B, O> Sync for RCow<B, O>
impl<B, O> Unpin for RCow<B, O>
impl<B, O> UnwindSafe for RCow<B, O>where
B: UnwindSafe,
O: UnwindSafe,
Blanket Implementations§
source§impl<T> AlignerFor<1> for T
impl<T> AlignerFor<1> for T
source§impl<T> AlignerFor<1024> for T
impl<T> AlignerFor<1024> for T
source§type Aligner = AlignTo1024<T>
type Aligner = AlignTo1024<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<128> for T
impl<T> AlignerFor<128> for T
source§type Aligner = AlignTo128<T>
type Aligner = AlignTo128<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<16> for T
impl<T> AlignerFor<16> for T
source§impl<T> AlignerFor<16384> for T
impl<T> AlignerFor<16384> for T
source§type Aligner = AlignTo16384<T>
type Aligner = AlignTo16384<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<2> for T
impl<T> AlignerFor<2> for T
source§impl<T> AlignerFor<2048> for T
impl<T> AlignerFor<2048> for T
source§type Aligner = AlignTo2048<T>
type Aligner = AlignTo2048<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<256> for T
impl<T> AlignerFor<256> for T
source§type Aligner = AlignTo256<T>
type Aligner = AlignTo256<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<32> for T
impl<T> AlignerFor<32> for T
source§impl<T> AlignerFor<32768> for T
impl<T> AlignerFor<32768> for T
source§type Aligner = AlignTo32768<T>
type Aligner = AlignTo32768<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<4> for T
impl<T> AlignerFor<4> for T
source§impl<T> AlignerFor<4096> for T
impl<T> AlignerFor<4096> for T
source§type Aligner = AlignTo4096<T>
type Aligner = AlignTo4096<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<512> for T
impl<T> AlignerFor<512> for T
source§type Aligner = AlignTo512<T>
type Aligner = AlignTo512<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<64> for T
impl<T> AlignerFor<64> for T
source§impl<T> AlignerFor<8> for T
impl<T> AlignerFor<8> for T
source§impl<T> AlignerFor<8192> for T
impl<T> AlignerFor<8192> for T
source§type Aligner = AlignTo8192<T>
type Aligner = AlignTo8192<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
source§fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
source§fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
source§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
source§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset
. Read moresource§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset
. Read moresource§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset
. Read moresource§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset
. Read moresource§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
source§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read moresource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read moresource§impl<T> StringExt for T
impl<T> StringExt for T
source§fn previous_char_boundary(&self, index: usize) -> usize
fn previous_char_boundary(&self, index: usize) -> usize
source§fn next_char_boundary(&self, index: usize) -> usize
fn next_char_boundary(&self, index: usize) -> usize
source§fn left_char_boundary(&self, index: usize) -> usize
fn left_char_boundary(&self, index: usize) -> usize
source§fn right_char_boundary(&self, index: usize) -> usize
fn right_char_boundary(&self, index: usize) -> usize
source§fn split_while<'a, P, T>(&'a self, mapper: P) -> SplitWhile<'a, P, T>
fn split_while<'a, P, T>(&'a self, mapper: P) -> SplitWhile<'a, P, T>
mapper
. Read moresource§fn rsplit_while<'a, P, T>(&'a self, mapper: P) -> RSplitWhile<'a, P, T>
fn rsplit_while<'a, P, T>(&'a self, mapper: P) -> RSplitWhile<'a, P, T>
split_while
that iterates
from the right(the order of substrings is reversed). Read moresource§fn get_nth_char_index(&self, nth: usize) -> Option<usize>
fn get_nth_char_index(&self, nth: usize) -> Option<usize>
nth
character Read moresource§fn nth_char(&self, nth: usize) -> Option<char>
fn nth_char(&self, nth: usize) -> Option<char>
nth
character in the str. Read moresource§fn first_chars(&self, n: usize) -> &str
fn first_chars(&self, n: usize) -> &str
n
chars. Read moresource§fn calc_len_utf16(&self) -> usize
fn calc_len_utf16(&self) -> usize
source§fn get_char_at(&self, at_byte: usize) -> Option<char>
fn get_char_at(&self, at_byte: usize) -> Option<char>
at_byte
index inside of the string,
returning None
if the index is outside the string. Read moresource§fn char_indices_to(&self, to: usize) -> CharIndices<'_>
fn char_indices_to(&self, to: usize) -> CharIndices<'_>
to
byte. Read moresource§fn char_indices_from(&self, from: usize) -> CharIndicesFrom<'_>
fn char_indices_from(&self, from: usize) -> CharIndicesFrom<'_>
from
byte. Read moresource§fn left_pad(&self, how_much: usize) -> String
fn left_pad(&self, how_much: usize) -> String
how_much
additional spaces. Read moresource§fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a>
fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a>
how_much
additional
spaces in its Display
impl. Read more