Skip to main content

DataPayloadOr

Struct DataPayloadOr 

Source
pub struct DataPayloadOr<M: DynamicDataMarker, O>(/* private fields */);
Expand description

A container for data payloads with storage for something else.

The type parameter O is stored as part of the interior enum, leading to better stack size optimization. O can be as large as the DataPayload minus two words without impacting stack size.

§Examples

Create and use DataPayloadOr:

use icu_locale_core::langid;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider::DataPayloadOr;

let response: DataResponse<HelloWorldV1> = HelloWorldProvider
    .load(DataRequest {
        id: DataIdentifierBorrowed::for_locale(&langid!("de").into()),
        ..Default::default()
    })
    .expect("Loading should succeed");

let payload_some =
    DataPayloadOr::<HelloWorldV1, ()>::from_payload(response.payload);
let payload_none = DataPayloadOr::<HelloWorldV1, ()>::from_other(());

assert_eq!(
    payload_some.get(),
    Ok(&HelloWorld {
        message: "Hallo Welt".into()
    })
);
assert_eq!(payload_none.get(), Err(&()));

Stack size comparison:

use icu_provider::prelude::*;
use icu_provider::DataPayloadOr;

const W: usize = size_of::<usize>();

// Data struct is 3 words:
icu_provider::data_marker!(SampleV1, [usize; 3]);

// DataPayload adds a word for a total of 4 words:
assert_eq!(W * 4, size_of::<DataPayload<SampleV1>>());

// Option<DataPayload> balloons to 5 words:
assert_eq!(W * 5, size_of::<Option<DataPayload<SampleV1>>>());

// But, using DataPayloadOr is the same size as DataPayload:
assert_eq!(W * 4, size_of::<DataPayloadOr<SampleV1, ()>>());

// The largest optimized Other type is two words smaller than the DataPayload:
assert_eq!(W * 4, size_of::<DataPayloadOr<SampleV1, [usize; 1]>>());
assert_eq!(W * 4, size_of::<DataPayloadOr<SampleV1, [usize; 2]>>());
assert_eq!(W * 5, size_of::<DataPayloadOr<SampleV1, [usize; 3]>>());

Implementations§

Source§

impl<M, O> DataPayloadOr<M, O>

Source

pub fn from_payload(payload: DataPayload<M>) -> Self

Creates a DataPayloadOr from a DataPayload.

Source

pub fn from_other(other: O) -> Self

Creates a DataPayloadOr from the other type O.

Source

pub fn is_payload(&self) -> bool

Returns whether this object represents a DataPayload.

Source

pub fn get<'a>( &'a self, ) -> Result<&'a <M::DataStruct as Yokeable<'a>>::Output, &'a O>

Gets the value from this DataPayload as Ok or the other type as Err.

Source

pub fn into_inner(self) -> Result<DataPayload<M>, O>

Consumes this DataPayloadOr, returning either the wrapped DataPayload or the other type.

Source§

impl<M> DataPayloadOr<M, ()>

Source

pub fn none() -> Self

Convenience function to return the other type with value ()

Source

pub fn get_option<'a>( &'a self, ) -> Option<&'a <M::DataStruct as Yokeable<'a>>::Output>

Convenience function to return Some or None for other type ()

Trait Implementations§

Source§

impl<M, O> Clone for DataPayloadOr<M, O>
where M: DynamicDataMarker, for<'a> <M::DataStruct as Yokeable<'a>>::Output: Clone, O: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate 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<M, O> Debug for DataPayloadOr<M, O>
where M: DynamicDataMarker, for<'a> &'a <M::DataStruct as Yokeable<'a>>::Output: Debug, O: Debug,

Source§

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

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

impl<M, O> PartialEq for DataPayloadOr<M, O>
where M: DynamicDataMarker, for<'a> <M::DataStruct as Yokeable<'a>>::Output: PartialEq, O: Eq,

Source§

fn eq(&self, other: &Self) -> 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<M, O> Eq for DataPayloadOr<M, O>
where M: DynamicDataMarker, for<'a> <M::DataStruct as Yokeable<'a>>::Output: Eq, O: Eq,

Auto Trait Implementations§

§

impl<M, O> Freeze for DataPayloadOr<M, O>

§

impl<M, O> RefUnwindSafe for DataPayloadOr<M, O>

§

impl<M, O> Send for DataPayloadOr<M, O>
where O: Send, <M as DynamicDataMarker>::DataStruct: Sync + Send,

§

impl<M, O> Sync for DataPayloadOr<M, O>

§

impl<M, O> Unpin for DataPayloadOr<M, O>

§

impl<M, O> UnsafeUnpin for DataPayloadOr<M, O>

§

impl<M, O> UnwindSafe for DataPayloadOr<M, O>

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, dest: *mut u8)

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

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

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