pub struct AnyPayload { /* private fields */ }
Expand description

A type-erased data payload.

The only useful method on this type is AnyPayload::downcast(), which transforms this into a normal DataPayload which you can subsequently access or mutate.

As with DataPayload, cloning is designed to be cheap.

Implementations

Transforms a type-erased AnyPayload into a concrete DataPayload<M>.

Because it is expected that the call site knows the identity of the AnyPayload (e.g., from the data request), this function returns a DataError if the generic type does not match the type stored in the AnyPayload.

Creates an AnyPayload from a static reference to a data struct.

Examples
use icu_provider::prelude::*;
use icu_provider::hello_world::*;
use std::borrow::Cow;

const HELLO_DATA: HelloWorldV1<'static> = HelloWorldV1 {
    message: Cow::Borrowed("Custom Hello World")
};

let any_payload = AnyPayload::from_static_ref(&HELLO_DATA);

let payload: DataPayload<HelloWorldV1Marker> = any_payload.downcast()
    .expect("TypeId matches");
assert_eq!("Custom Hello World", payload.get().message);

Creates an AnyPayload from a DataPayload stored in an Rc.

Examples
use icu_provider::prelude::*;
use icu_provider::hello_world::*;
use std::borrow::Cow;
use std::rc::Rc;

let payload: DataPayload<HelloWorldV1Marker> =
    DataPayload::from_owned(HelloWorldV1 {
        message: Cow::Borrowed("Custom Hello World")
    });
let rc_payload = Rc::from(payload);

let any_payload = AnyPayload::from_rc_payload(rc_payload);

let payload: DataPayload<HelloWorldV1Marker> = any_payload.downcast()
    .expect("TypeId matches");
assert_eq!("Custom Hello World", payload.get().message);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>

This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more

This method must cast self between Self<'static> and Self<'a>. Read more

This method can be used to cast away Self<'a>’s lifetime. Read more

This method must cast self between &'a mut Self<'static> and &'a mut Self<'a>, and pass it to f. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.