Struct DataPayload
struct DataPayload<M: DynamicDataMarker>(_)
A container for data payloads returned from a data provider.
DataPayload is built on top of the yoke framework, which allows for cheap, zero-copy
operations on data via the use of self-references.
The type of the data stored in DataPayload is determined by the DynamicDataMarker type parameter.
Accessing the data
To get a reference to the data inside DataPayload, use [DataPayload::get()]. If you need
to store the data for later use, you need to store the DataPayload itself, since get only
returns a reference with an ephemeral lifetime.
Mutating the data
To modify the data stored in a DataPayload, use [DataPayload::with_mut()].
Transforming the data to a different type
To transform a DataPayload to a different type backed by the same data store (cart), use
[DataPayload::map_project()] or one of its sister methods.
Cargo feature: sync
By default, the payload uses non-concurrent reference counting internally, and hence is neither
Sync nor Send; if these traits are required, the sync Cargo feature can be enabled.
Examples
Basic usage, using the HelloWorldV1 marker:
use *;
use *;
use Cow;
let payload = from_owned;
assert_eq!;
Implementations
impl DataPayload<BufferMarker>
fn from_yoked_buffer(yoke: Yoke<&'static [u8], Option<Cart>>) -> SelfConverts a yoked byte buffer into a
DataPayload<BufferMarker>.fn from_static_buffer(buffer: &'static [u8]) -> SelfConverts a static byte buffer into a
DataPayload<BufferMarker>.
impl<M> DataPayload<M>
fn from_owned(data: <M as >::DataStruct) -> SelfConvert a fully owned (
'static) data struct into a DataPayload.This constructor creates
'staticpayloads.Examples
use *; use *; use Cow; let local_struct = HelloWorld ; let payload = from_owned; assert_eq!;const fn from_static_ref(data: &'static <M as >::DataStruct) -> SelfConstruct a
DataPayloadfrom a static reference.This is mainly used by databake.
fn with_mut<'a, F>(self: &'a mut Self, f: F) where F: 'static + for<'b> FnOnce(&'b mut <<M as >::DataStruct as Yokeable<'a>>::Output), <M as >::DataStruct: ZeroFrom<'static, <M as >::DataStruct>Mutate the data contained in this DataPayload.
For safety, all mutation operations must take place within a helper function that cannot borrow data from the surrounding context.
Examples
Basic usage:
use HelloWorldV1; use *; let mut payload = from_static_str; payload.with_mut; assert_eq!;To transfer data from the context into the data struct, use the
movekeyword:use HelloWorldV1; use *; let mut payload = from_static_str; let suffix = " World"; payload.with_mut; assert_eq!;fn get<'a>(self: &'a Self) -> &'a <<M as >::DataStruct as Yokeable<'a>>::OutputBorrows the underlying data.
This function should be used like
Derefwould normally be used. For more information on why DataPayload cannot implementDeref, see theyokecrate.Examples
use HelloWorldV1; use *; let payload = from_static_str; assert_eq!;fn get_static(self: &Self) -> Option<&'static <<M as >::DataStruct as Yokeable<'static>>::Output>Borrows the underlying data statically if possible.
This will succeed if
DataPayloadis constructed withDataPayload::from_static_ref, which is used by baked providers.fn map_project<M2, F>(self: Self, f: F) -> DataPayload<M2> where M2: DynamicDataMarker, F: for<'a> FnOnce(<<M as >::DataStruct as Yokeable<'a>>::Output, PhantomData<&'a ()>) -> <<M2 as >::DataStruct as Yokeable<'a>>::Output, <M as >::DataStruct: ZeroFrom<'static, <M as >::DataStruct>Maps
DataPayload<M>toDataPayload<M2>by projecting it withYoke::map_project.This is accomplished by a function that takes
M's data type and returnsM2's data type. The function takes a second argument which should be ignored. For more details, see [Yoke::map_project()].The standard [
DataPayload::map_project()] function movesselfand cannot capture any data from its context. Use one of the sister methods if you need these capabilities:- [
DataPayload::map_project_cloned()] if you don't have ownership ofself - [
DataPayload::try_map_project()] to bubble up an error - [
DataPayload::try_map_project_cloned()] to do both of the above
Examples
Map from
HelloWorldto aCow<str>containing just the message:use *; use *; use Cow; // A custom marker type is required when using `map_project`. The DataStruct should be the // target type, and the Cart should correspond to the type being transformed. ; let p1: = from_owned; assert_eq!; let p2: = p1.map_project; // Note: at this point, p1 has been moved. assert_eq!;- [
fn map_project_cloned<'this, M2, F>(self: &'this Self, f: F) -> DataPayload<M2> where M2: DynamicDataMarker, F: for<'a> FnOnce(&'this <<M as >::DataStruct as Yokeable<'a>>::Output, PhantomData<&'a ()>) -> <<M2 as >::DataStruct as Yokeable<'a>>::OutputVersion of [
DataPayload::map_project()] that borrowsselfinstead of movingself.Examples
Same example as above, but this time, do not move out of
p1:// Same imports and definitions as above # use *; # use *; # use Cow; # ; # let p1: = from_owned; assert_eq!; let p2: = p1.map_project_cloned; // Note: p1 is still valid. assert_eq!;fn try_map_project<M2, F, E>(self: Self, f: F) -> Result<DataPayload<M2>, E> where M2: DynamicDataMarker, F: for<'a> FnOnce(<<M as >::DataStruct as Yokeable<'a>>::Output, PhantomData<&'a ()>) -> Result<<<M2 as >::DataStruct as Yokeable<'a>>::Output, E>, <M as >::DataStruct: ZeroFrom<'static, <M as >::DataStruct>Version of [
DataPayload::map_project()] that bubbles up an error fromf.Examples
Same example as above, but bubble up an error:
// Same imports and definitions as above # use *; # use *; # use Cow; # ; # let p1: = from_owned; assert_eq!; let string_to_append = "Extra"; let p2: = p1.try_map_project?; assert_eq!; # Ok::fn try_map_project_cloned<'this, M2, F, E>(self: &'this Self, f: F) -> Result<DataPayload<M2>, E> where M2: DynamicDataMarker, F: for<'a> FnOnce(&'this <<M as >::DataStruct as Yokeable<'a>>::Output, PhantomData<&'a ()>) -> Result<<<M2 as >::DataStruct as Yokeable<'a>>::Output, E>Version of [
DataPayload::map_project_cloned()] that bubbles up an error fromf.Examples
Same example as above, but bubble up an error:
// Same imports and definitions as above # use *; # use *; # use Cow; # ; # let p1: = from_owned; assert_eq!; let string_to_append = "Extra"; let p2: = p1 .try_map_project_cloned?; // Note: p1 is still valid, but the values no longer equal. assert_ne!; assert_eq!; # Ok::fn cast<M2>(self: Self) -> DataPayload<M2> where M2: DynamicDataMarker<DataStruct = <M as >::DataStruct>Convert between two
DynamicDataMarkertypes that are compatible with each other with compile-time type checking.This happens if they both have the same
DynamicDataMarker::DataStructtype.Can be used to erase the marker of a data payload in cases where multiple markers correspond to the same data struct.
For runtime dynamic casting, use [
DataPayload::dynamic_cast_mut()].Examples
use icu_provider::hello_world::*; use icu_provider::prelude::*; struct CustomHelloWorldV1; impl DynamicDataMarker for CustomHelloWorldV1 { type DataStruct = HelloWorld<'static>; } let hello_world: DataPayload<HelloWorldV1> = todo!(); let custom: DataPayload<CustomHelloWorldV1> = hello_world.cast();fn cast_ref<M2>(self: &Self) -> &DataPayload<M2> where M2: DynamicDataMarker<DataStruct = <M as >::DataStruct>Convert between two
DynamicDataMarkertypes that are compatible with each other with compile-time type checking.This happens if they both have the same
DynamicDataMarker::DataStructtype.Can be used to erase the marker of a data payload in cases where multiple markers correspond to the same data struct.
fn dynamic_cast<M2>(self: Self) -> Result<DataPayload<M2>, DataError> where M2: DynamicDataMarkerConvert a
DataPayloadto one of the same type with runtime type checking.Primarily useful to convert from a generic to a concrete marker type.
If the
M2type argument does not match the true marker type, aDataErroris returned.For compile-time static casting, use [
DataPayload::cast()].Examples
Short-circuit a data request request based on the marker, returning a result from a different data provider:
use TypeId; use locale; use *; use *; use EmptyDataProvider; use Cow; let provider = MyForkingProvider ; let formatter = try_new_unstable .unwrap; // This succeeds because the data was loaded from HelloWorldProvider // rather than the empty fallback provider. assert_eq!;fn dynamic_cast_mut<M2>(self: &mut Self) -> Result<&mut DataPayload<M2>, DataError> where M2: DynamicDataMarkerConvert a mutable reference of a
DataPayloadto another mutable reference of the same type with runtime type checking.Primarily useful to convert from a generic to a concrete marker type.
If the
M2type argument does not match the true marker type, aDataErroris returned.For compile-time static casting, use [
DataPayload::cast()].Examples
Change the results of a particular request based on marker:
use locale; use *; use *; let provider = MyWrapper ; let formatter = try_new_unstable .unwrap; assert_eq!;
impl<M> Clone for DataPayload<M>
fn clone(self: &Self) -> Self
impl<M> Debug for DataPayload<M>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<M> Default for DataPayload<M>
fn default() -> Self
impl<M> Eq for DataPayload<M>
impl<M> Freeze for DataPayload<M>
impl<M> PartialEq for DataPayload<M>
fn eq(self: &Self, other: &Self) -> bool
impl<M> RefUnwindSafe for DataPayload<M>
impl<M> Send for DataPayload<M>
impl<M> Sync for DataPayload<M>
impl<M> Unpin for DataPayload<M>
impl<M> UnsafeUnpin for DataPayload<M>
impl<M> UnwindSafe for DataPayload<M>
impl<T> Any for DataPayload<M>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for DataPayload<M>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for DataPayload<M>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for DataPayload<M>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> ErasedDestructor for DataPayload<M>
impl<T> From for DataPayload<M>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for DataPayload<M>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for DataPayload<M>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for DataPayload<M>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for DataPayload<M>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>