diff options
Diffstat (limited to 'vendor/icu_provider/src/any.rs')
-rw-r--r-- | vendor/icu_provider/src/any.rs | 77 |
1 files changed, 50 insertions, 27 deletions
diff --git a/vendor/icu_provider/src/any.rs b/vendor/icu_provider/src/any.rs index 989438c6b..c3af61fed 100644 --- a/vendor/icu_provider/src/any.rs +++ b/vendor/icu_provider/src/any.rs @@ -65,6 +65,7 @@ pub struct AnyPayload { /// The [`DataMarker`] marker type for [`AnyPayload`]. #[allow(clippy::exhaustive_structs)] // marker type +#[derive(Debug)] pub struct AnyMarker; impl DataMarker for AnyMarker { @@ -73,7 +74,7 @@ impl DataMarker for AnyMarker { impl<M> crate::dynutil::UpcastDataPayload<M> for AnyMarker where - M: DataMarker + 'static, + M: DataMarker, M::Yokeable: MaybeSendSync, { #[inline] @@ -90,7 +91,7 @@ impl AnyPayload { /// the type stored in the `AnyPayload`. pub fn downcast<M>(self) -> Result<DataPayload<M>, DataError> where - M: DataMarker + 'static, + M: DataMarker, // For the StructRef case: M::Yokeable: ZeroFrom<'static, M::Yokeable>, // For the PayloadRc case: @@ -118,7 +119,7 @@ impl AnyPayload { /// Clones and then transforms a type-erased `AnyPayload` into a concrete `DataPayload<M>`. pub fn downcast_cloned<M>(&self) -> Result<DataPayload<M>, DataError> where - M: DataMarker + 'static, + M: DataMarker, // For the StructRef case: M::Yokeable: ZeroFrom<'static, M::Yokeable>, // For the PayloadRc case: @@ -162,7 +163,7 @@ impl AnyPayload { impl<M> DataPayload<M> where - M: DataMarker + 'static, + M: DataMarker, M::Yokeable: MaybeSendSync, { /// Moves this DataPayload to the heap (requiring an allocation) and returns it as an @@ -200,7 +201,7 @@ impl DataPayload<AnyMarker> { #[inline] pub fn downcast<M>(self) -> Result<DataPayload<M>, DataError> where - M: DataMarker + 'static, + M: DataMarker, for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, M::Yokeable: ZeroFrom<'static, M::Yokeable>, M::Yokeable: MaybeSendSync, @@ -213,6 +214,7 @@ impl DataPayload<AnyMarker> { /// /// Convertible to and from `DataResponse<AnyMarker>`. #[allow(clippy::exhaustive_structs)] // this type is stable (the metadata is allowed to grow) +#[derive(Debug)] pub struct AnyResponse { /// Metadata about the returned object. pub metadata: DataResponseMetadata, @@ -247,7 +249,7 @@ impl AnyResponse { #[inline] pub fn downcast<M>(self) -> Result<DataResponse<M>, DataError> where - M: DataMarker + 'static, + M: DataMarker, for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, M::Yokeable: ZeroFrom<'static, M::Yokeable>, M::Yokeable: MaybeSendSync, @@ -261,7 +263,7 @@ impl AnyResponse { /// Clones and then transforms a type-erased `AnyResponse` into a concrete `DataResponse<M>`. pub fn downcast_cloned<M>(&self) -> Result<DataResponse<M>, DataError> where - M: DataMarker + 'static, + M: DataMarker, M::Yokeable: ZeroFrom<'static, M::Yokeable>, M::Yokeable: MaybeSendSync, for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, @@ -279,7 +281,7 @@ impl AnyResponse { impl<M> DataResponse<M> where - M: DataMarker + 'static, + M: DataMarker, M::Yokeable: MaybeSendSync, { /// Moves the inner DataPayload to the heap (requiring an allocation) and returns it as an @@ -301,24 +303,43 @@ where /// use icu_provider::prelude::*; /// use std::borrow::Cow; /// -/// let any_response = HelloWorldProvider -/// .as_any_provider() -/// .load_any( -/// HelloWorldV1Marker::KEY, -/// DataRequest { -/// locale: &icu_locid::locale!("de").into(), -/// metadata: Default::default(), -/// }, -/// ) -/// .expect("Load should succeed"); +/// let any_provider = HelloWorldProvider.as_any_provider(); /// -/// // Downcast to something useful -/// let response: DataResponse<HelloWorldV1Marker> = -/// any_response.downcast().expect("Types match"); +/// let req = DataRequest { +/// locale: &icu_locid::locale!("de").into(), +/// metadata: Default::default(), +/// }; /// -/// let payload = response.take_payload().expect("Data should be present"); +/// // Downcasting manually +/// assert_eq!( +/// any_provider +/// .load_any(HelloWorldV1Marker::KEY, req) +/// .expect("load should succeed") +/// .downcast::<HelloWorldV1Marker>() +/// .expect("types should match") +/// .take_payload() +/// .unwrap() +/// .get(), +/// &HelloWorldV1 { +/// message: Cow::Borrowed("Hallo Welt"), +/// }, +/// ); /// -/// assert_eq!(payload.get().message, "Hallo Welt"); +/// // Downcasting automatically +/// let downcasting_provider: &dyn DataProvider<HelloWorldV1Marker> = +/// &any_provider.as_downcasting(); +/// +/// assert_eq!( +/// downcasting_provider +/// .load(req) +/// .expect("load should succeed") +/// .take_payload() +/// .unwrap() +/// .get(), +/// &HelloWorldV1 { +/// message: Cow::Borrowed("Hallo Welt"), +/// }, +/// ); /// ``` pub trait AnyProvider { /// Loads an [`AnyPayload`] according to the key and request. @@ -333,6 +354,7 @@ impl<T: AnyProvider + ?Sized> AnyProvider for alloc::boxed::Box<T> { /// A wrapper over `DynamicDataProvider<AnyMarker>` that implements `AnyProvider` #[allow(clippy::exhaustive_structs)] // newtype +#[derive(Debug)] pub struct DynamicDataProviderAnyMarkerWrap<'a, P: ?Sized>(pub &'a P); /// Blanket-implemented trait adding the [`Self::as_any_provider()`] function. @@ -363,6 +385,7 @@ where /// A wrapper over `AnyProvider` that implements `DynamicDataProvider<M>` via downcasting #[allow(clippy::exhaustive_structs)] // newtype +#[derive(Debug)] pub struct DowncastingAnyProvider<'a, P: ?Sized>(pub &'a P); /// Blanket-implemented trait adding the [`Self::as_downcasting()`] function. @@ -384,7 +407,7 @@ where impl<M, P> DataProvider<M> for DowncastingAnyProvider<'_, P> where P: AnyProvider + ?Sized, - M: KeyedDataMarker + 'static, + M: KeyedDataMarker, for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, M::Yokeable: ZeroFrom<'static, M::Yokeable>, M::Yokeable: MaybeSendSync, @@ -401,7 +424,7 @@ where impl<M, P> DynamicDataProvider<M> for DowncastingAnyProvider<'_, P> where P: AnyProvider + ?Sized, - M: DataMarker + 'static, + M: DataMarker, for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, M::Yokeable: ZeroFrom<'static, M::Yokeable>, M::Yokeable: MaybeSendSync, @@ -434,7 +457,7 @@ mod test { let any_payload = payload.wrap_into_any_payload(); assert_eq!( "AnyPayload { inner: PayloadRc(Any { .. }), type_name: \"icu_provider::hello_world::HelloWorldV1Marker\" }", - format!("{:?}", any_payload) + format!("{any_payload:?}") ); struct WrongMarker; @@ -446,7 +469,7 @@ mod test { let err = any_payload.downcast::<WrongMarker>().unwrap_err(); assert_eq!( "ICU4X data error: Mismatched types: tried to downcast with icu_provider::any::test::test_debug::WrongMarker, but actual type is different: icu_provider::hello_world::HelloWorldV1Marker", - format!("{}", err) + format!("{err}") ); } |