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 usizes without impacting stack size.
§Examples
Create and use DataPayloadOr:
use icu_locale_core::data_locale;
use icu_provider::DataPayloadOr;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
let response: DataResponse<HelloWorldV1> = HelloWorldProvider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&data_locale!("de")),
..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 usizes:
icu_provider::data_marker!(SampleV1, [usize; 3]);
// DataPayload adds a usize for a total of 4 usizes:
assert_eq!(W * 4, size_of::<DataPayload<SampleV1>>());
// Option<DataPayload> balloons to 5 usizes:
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 usizes 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.
Sourcepub fn map_other<O2>(self, f: impl FnOnce(O) -> O2) -> DataPayloadOr<M, O2>
pub fn map_other<O2>(self, f: impl FnOnce(O) -> O2) -> DataPayloadOr<M, O2>
Maps the Other type to a new Other type.
Sourcepub fn cast<M2>(self) -> DataPayloadOr<M2, O>where
M2: DynamicDataMarker<DataStruct = M::DataStruct>,
pub fn cast<M2>(self) -> DataPayloadOr<M2, O>where
M2: DynamicDataMarker<DataStruct = M::DataStruct>,
Maps the Marker type to a compatible Marker type.
See DataResponse::cast.
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 ()