diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:32 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:32 +0000 |
commit | 4547b622d8d29df964fa2914213088b148c498fc (patch) | |
tree | 9fc6b25f3c3add6b745be9a2400a6e96140046e9 /vendor/icu_provider_adapters/src/any_payload.rs | |
parent | Releasing progress-linux version 1.66.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-4547b622d8d29df964fa2914213088b148c498fc.tar.xz rustc-4547b622d8d29df964fa2914213088b148c498fc.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | vendor/icu_provider_adapters/src/any_payload.rs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/vendor/icu_provider_adapters/src/any_payload.rs b/vendor/icu_provider_adapters/src/any_payload.rs new file mode 100644 index 000000000..bbb9220c3 --- /dev/null +++ b/vendor/icu_provider_adapters/src/any_payload.rs @@ -0,0 +1,123 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! Data provider always serving the same struct. + +use icu_provider::prelude::*; +use yoke::trait_hack::YokeTraitHack; +use yoke::Yokeable; +use zerofrom::ZeroFrom; + +/// A data provider that returns clones of a fixed type-erased payload. +/// +/// [`AnyPayloadProvider`] implements [`AnyProvider`], so it can be used in +/// `*_with_any_provider` constructors across ICU4X. +/// +/// # Examples +/// +/// ``` +/// use icu_provider::hello_world::*; +/// use icu_provider::prelude::*; +/// use icu_provider_adapters::any_payload::AnyPayloadProvider; +/// use std::borrow::Cow; +/// use writeable::assert_writeable_eq; +/// +/// let provider = +/// AnyPayloadProvider::from_static::<HelloWorldV1Marker>(&HelloWorldV1 { +/// message: Cow::Borrowed("custom hello world"), +/// }); +/// +/// // Check that it works: +/// let formatter = HelloWorldFormatter::try_new_with_any_provider( +/// &provider, +/// &icu_locid::Locale::UND.into(), +/// ) +/// .expect("key matches"); +/// assert_writeable_eq!(formatter.format(), "custom hello world"); +/// +/// // Requests for invalid keys get MissingDataKey +/// assert!(matches!( +/// provider.load_any(icu_provider::data_key!("foo@1"), Default::default()), +/// Err(DataError { +/// kind: DataErrorKind::MissingDataKey, +/// .. +/// }) +/// )) +/// ``` +#[allow(clippy::exhaustive_structs)] // this type is stable +pub struct AnyPayloadProvider { + /// The [`DataKey`] for which to provide data. All others will receive a + /// [`DataErrorKind::MissingDataKey`]. + key: DataKey, + /// The [`AnyPayload`] to return on matching requests. + data: AnyPayload, +} + +impl AnyPayloadProvider { + /// Creates an `AnyPayloadProvider` with an owned (allocated) payload of the given data. + pub fn from_owned<M: KeyedDataMarker + 'static>(data: M::Yokeable) -> Self + where + M::Yokeable: icu_provider::MaybeSendSync, + { + Self::from_payload::<M>(DataPayload::from_owned(data)) + } + + /// Creates an `AnyPayloadProvider` with a statically borrowed payload of the given data. + pub fn from_static<M: KeyedDataMarker>(data: &'static M::Yokeable) -> Self { + AnyPayloadProvider { + key: M::KEY, + data: AnyPayload::from_static_ref(data), + } + } + + /// Creates an `AnyPayloadProvider` from an existing [`DataPayload`]. + pub fn from_payload<M: KeyedDataMarker + 'static>(payload: DataPayload<M>) -> Self + where + M::Yokeable: icu_provider::MaybeSendSync, + { + AnyPayloadProvider { + key: M::KEY, + data: payload.wrap_into_any_payload(), + } + } + + /// Creates an `AnyPayloadProvider` from an existing [`AnyPayload`]. + pub fn from_any_payload<M: KeyedDataMarker + 'static>(payload: AnyPayload) -> Self { + AnyPayloadProvider { + key: M::KEY, + data: payload, + } + } + + /// Creates an `AnyPayloadProvider` with the default (allocated) version of the data struct. + pub fn new_default<M: KeyedDataMarker + 'static>() -> Self + where + M::Yokeable: Default, + M::Yokeable: icu_provider::MaybeSendSync, + { + Self::from_owned::<M>(M::Yokeable::default()) + } +} + +impl AnyProvider for AnyPayloadProvider { + fn load_any(&self, key: DataKey, _: DataRequest) -> Result<AnyResponse, DataError> { + key.match_key(self.key)?; + Ok(AnyResponse { + metadata: DataResponseMetadata::default(), + payload: Some(self.data.clone()), + }) + } +} + +impl<M> DataProvider<M> for AnyPayloadProvider +where + M: KeyedDataMarker + 'static, + for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, + M::Yokeable: ZeroFrom<'static, M::Yokeable>, + M::Yokeable: icu_provider::MaybeSendSync, +{ + fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> { + self.as_downcasting().load(req) + } +} |