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>where
M: DynamicDataMarker,
impl<M, O> DataPayloadOr<M, O>where
M: DynamicDataMarker,
Sourcepub fn from_payload(payload: DataPayload<M>) -> Self
pub fn from_payload(payload: DataPayload<M>) -> Self
Creates a DataPayloadOr from a DataPayload.
Sourcepub fn from_other(other: O) -> Self
pub fn from_other(other: O) -> Self
Creates a DataPayloadOr from the other type O.
Sourcepub fn is_payload(&self) -> bool
pub fn is_payload(&self) -> bool
Returns whether this object represents a DataPayload.
Sourcepub fn get<'a>(
&'a self,
) -> Result<&'a <M::DataStruct as Yokeable<'a>>::Output, &'a O>
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.
Sourcepub fn into_inner(self) -> Result<DataPayload<M>, O>
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, ()>where
M: DynamicDataMarker,
impl<M> DataPayloadOr<M, ()>where
M: DynamicDataMarker,
Sourcepub fn get_option<'a>(
&'a self,
) -> Option<&'a <M::DataStruct as Yokeable<'a>>::Output>
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>
impl<M, O> Clone for DataPayloadOr<M, O>
Source§impl<M, O> Debug for DataPayloadOr<M, O>
impl<M, O> Debug for DataPayloadOr<M, O>
Source§impl<M, O> PartialEq for DataPayloadOr<M, O>
impl<M, O> PartialEq for DataPayloadOr<M, O>
impl<M, O> Eq for DataPayloadOr<M, O>
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>
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> 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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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