abi_stable::std_types

Type Alias RCowVal

Source
pub type RCowVal<'a, T> = RCow<&'a T, T>;
Expand description

Ffi-safe equivalent of Cow<'a, T>, either a &T or T.

§Example

use abi_stable::std_types::{RCow, RCowVal};

fn foo(x: u8) -> RCowVal<'static, u8> {
    if x % 2 == 0 {
       RCow::Borrowed(&1)
    } else {
       RCow::Owned(x * 2)
    }
}

assert_eq!(*foo(3), 6);
assert_eq!(*foo(4), 1);
assert_eq!(*foo(5), 10);
assert_eq!(*foo(6), 1);
assert_eq!(*foo(7), 14);

Aliased Type§

enum RCowVal<'a, T> {
    Borrowed(&'a T),
    Owned(T),
}

Variants§

§

Borrowed(&'a T)

§

Owned(T)

Implementations

Source§

impl<B> RCow<B, B::ROwned>
where B: IntoOwned,

Source

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!"));
Source

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!");
Source

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>

Source

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

pub const fn is_owned(&self) -> bool

Whether this is an owning RCow.

§Examples
use abi_stable::std_types::{RCow, RCowSlice};

let cow: RCowSlice<'_, u8> = RCow::from(&[0, 1, 2, 3][..]);
assert!(!cow.is_owned());

let cow: RCowSlice<'_, u8> = RCow::from(vec![0, 1, 2, 3]);
assert!(cow.is_owned());

Trait Implementations§

Source§

impl<T: Clone> AsRef<T> for RCowVal<'_, T>

Source§

fn as_ref(&self) -> &T

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

impl<T: Clone> Borrow<T> for RCowVal<'_, T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'de, 'a, T> Deserialize<'de> for RCowVal<'a, T>
where T: Clone + Deserialize<'de>,

Source§

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

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

impl<'a, T: Clone> IntoReprRust for RCowVal<'a, T>

Source§

type ReprRust = Cow<'a, T>

The #[repr(Rust)] equivalent.
Source§

fn into_rust(self) -> Self::ReprRust

Performs the conversion
Source§

impl<B> Clone for RCow<B, B::ROwned>
where B: IntoOwned, B::ROwned: Clone,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<B> Debug for RCow<B, B::ROwned>
where B: IntoOwned, B::Target: Debug,

Source§

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

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

impl<B> Deref for RCow<B, B::ROwned>
where B: IntoOwned,

Source§

type Target = <B as Deref>::Target

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<B> Display for RCow<B, B::ROwned>
where B: IntoOwned, B::Target: Display,

Source§

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

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

impl<'a, T> From<Cow<'a, T>> for RCow<T::RefC, T::ROwned>
where T: ?Sized + RCowCompatibleRef<'a>,

Source§

fn from(this: Cow<'a, T>) -> RCow<T::RefC, T::ROwned>

Converts to this type from the input type.
Source§

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>

The 'static equivalent of Self
Source§

impl<B> Hash for RCow<B, B::ROwned>
where B: IntoOwned, B::Target: Hash,

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<B> Ord for RCow<B, B::ROwned>
where B: IntoOwned, B::Target: 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,

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

impl<B, V> PartialEq<RCow<V, <V as IntoOwned>::ROwned>> for RCow<B, B::ROwned>
where B: IntoOwned, V: IntoOwned, B::Target: PartialEq<V::Target>,

Source§

fn eq(&self, other: &RCow<V, V::ROwned>) -> 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<B, V> PartialOrd<RCow<V, <V as IntoOwned>::ROwned>> for RCow<B, B::ROwned>
where B: IntoOwned, V: IntoOwned, B::Target: PartialOrd<V::Target>,

Source§

fn partial_cmp(&self, other: &RCow<V, V::ROwned>) -> 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<B> Serialize for RCow<B, B::ROwned>
where B: IntoOwned, B::Target: 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<B, O> StableAbi for RCow<B, O>
where B: __StableAbi, O: __StableAbi,

Source§

const LAYOUT: &'static TypeLayout = _

The layout of the type provided by implementors.
Source§

type IsNonZeroType = False

Whether this type has a single invalid bit-pattern. Read more
Source§

const ABI_CONSTS: AbiConsts = _

const-equivalents of the associated types.
Source§

impl<B> Copy for RCow<B, B::ROwned>
where B: IntoOwned, B::ROwned: Copy,

Source§

impl<B> Eq for RCow<B, B::ROwned>
where B: IntoOwned, B::Target: Eq,