From b68c0f85ab3b26ec3e43ddf468e039d03f0e78da Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Sat, 26 Aug 2023 17:45:43 -0700 Subject: [PATCH 1/4] Use DataProvider without AnyProvider in tutorial example --- docs/tutorials/data_provider.md | 42 ++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/docs/tutorials/data_provider.md b/docs/tutorials/data_provider.md index 07a59c4f858..063f0d481b4 100644 --- a/docs/tutorials/data_provider.md +++ b/docs/tutorials/data_provider.md @@ -269,23 +269,33 @@ use tinystr::tinystr; pub struct CustomDecimalSymbolsProvider

(P); -impl

AnyProvider for CustomDecimalSymbolsProvider

+fn transform(any_res: AnyResponse) -> Result { + let mut dec_res: DataResponse = any_res.downcast()?; + if let Some(payload) = &mut dec_res.payload.as_mut() { + payload.with_mut(|data| { + // Change the grouping separator for all Swiss locales to '🐮' + data.grouping_separator = Cow::Borrowed("🐮"); + }); + } + Ok(dec_res.wrap_into_any_response()) +} + +impl DataProvider for CustomDecimalSymbolsProvider

where - P: AnyProvider + P: DataProvider, + M: KeyedDataMarker, + M::Yokeable: icu_provider::MaybeSendSync + zerofrom::ZeroFrom<'static, M::Yokeable>, + for<'a> yoke::trait_hack::YokeTraitHack<>::Output>: Clone, { - fn load_any(&self, key: DataKey, req: DataRequest) -> Result { - let mut any_res = self.0.load_any(key, req)?; - if key == DecimalSymbolsV1Marker::KEY && req.locale.region() == Some(region!("CH")) { - let mut res: DataResponse = any_res.downcast()?; - if let Some(payload) = &mut res.payload.as_mut() { - payload.with_mut(|data| { - // Change the grouping separator for all Swiss locales to '🐮' - data.grouping_separator = Cow::Borrowed("🐮"); - }); - } - any_res = res.wrap_into_any_response(); + #[inline] + fn load(&self, req: DataRequest) -> Result, DataError> { + let mut res = self.0.load(req)?; + if M::KEY == DecimalSymbolsV1Marker::KEY && req.locale.region() == Some(region!("CH")) { + let any_res = res.wrap_into_any_response(); + transform(any_res)?.downcast() + } else { + Ok(res) } - Ok(any_res) } } @@ -293,7 +303,7 @@ let provider = CustomDecimalSymbolsProvider( AnyPayloadProvider::new_default::() ); -let formatter = FixedDecimalFormatter::try_new_with_any_provider( +let formatter = FixedDecimalFormatter::try_new_unstable( &provider, &locale!("und").into(), Default::default(), @@ -302,7 +312,7 @@ let formatter = FixedDecimalFormatter::try_new_with_any_provider( assert_eq!(formatter.format_to_string(&100007i64.into()), "100,007"); -let formatter = FixedDecimalFormatter::try_new_with_any_provider( +let formatter = FixedDecimalFormatter::try_new_unstable( &provider, &locale!("und-CH").into(), Default::default(), From f39465170b66f9511556e350e5b20cffe9f5251b Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Sun, 27 Aug 2023 11:23:54 +0200 Subject: [PATCH 2/4] inline --- docs/tutorials/data_provider.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/docs/tutorials/data_provider.md b/docs/tutorials/data_provider.md index 063f0d481b4..7957275c8c3 100644 --- a/docs/tutorials/data_provider.md +++ b/docs/tutorials/data_provider.md @@ -269,17 +269,6 @@ use tinystr::tinystr; pub struct CustomDecimalSymbolsProvider

(P); -fn transform(any_res: AnyResponse) -> Result { - let mut dec_res: DataResponse = any_res.downcast()?; - if let Some(payload) = &mut dec_res.payload.as_mut() { - payload.with_mut(|data| { - // Change the grouping separator for all Swiss locales to '🐮' - data.grouping_separator = Cow::Borrowed("🐮"); - }); - } - Ok(dec_res.wrap_into_any_response()) -} - impl DataProvider for CustomDecimalSymbolsProvider

where P: DataProvider, @@ -291,8 +280,16 @@ where fn load(&self, req: DataRequest) -> Result, DataError> { let mut res = self.0.load(req)?; if M::KEY == DecimalSymbolsV1Marker::KEY && req.locale.region() == Some(region!("CH")) { - let any_res = res.wrap_into_any_response(); - transform(any_res)?.downcast() + // Cast from `DataPayload` to `DataPayload` + let mut dec_res = res.wrap_into_any_response().downcast::()?; + if let Some(payload) = &mut dec_res.payload.as_mut() { + payload.with_mut(|data| { + // Change the grouping separator for all Swiss locales to '🐮' + data.grouping_separator = Cow::Borrowed("🐮"); + }); + } + // Cast back + dec_res.wrap_into_any_response().downcast() } else { Ok(res) } From 8478ddc3bcd86dccf62ea437d608cb706c261403 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Mon, 28 Aug 2023 17:11:05 -0700 Subject: [PATCH 3/4] Cast the payload directly through dyn Any --- docs/tutorials/data_provider.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/tutorials/data_provider.md b/docs/tutorials/data_provider.md index 7957275c8c3..660d041610e 100644 --- a/docs/tutorials/data_provider.md +++ b/docs/tutorials/data_provider.md @@ -258,6 +258,7 @@ ICU4X's explicit data pipeline allows for specific data entries to be overwritte The following example illustrates how to overwrite the decimal separators for a region. ```rust +use core::any::Any; use icu::decimal::FixedDecimalFormatter; use icu::decimal::provider::DecimalSymbolsV1Marker; use icu_provider::prelude::*; @@ -279,20 +280,19 @@ where #[inline] fn load(&self, req: DataRequest) -> Result, DataError> { let mut res = self.0.load(req)?; - if M::KEY == DecimalSymbolsV1Marker::KEY && req.locale.region() == Some(region!("CH")) { + if let Some(mut generic_payload) = res.payload.as_mut() { // Cast from `DataPayload` to `DataPayload` - let mut dec_res = res.wrap_into_any_response().downcast::()?; - if let Some(payload) = &mut dec_res.payload.as_mut() { - payload.with_mut(|data| { - // Change the grouping separator for all Swiss locales to '🐮' - data.grouping_separator = Cow::Borrowed("🐮"); - }); + let mut any_payload = generic_payload as &mut dyn Any; + if let Some(mut decimal_payload) = any_payload.downcast_mut::>() { + if req.locale.region() == Some(region!("CH")) { + decimal_payload.with_mut(|data| { + // Change the grouping separator for all Swiss locales to '🐮' + data.grouping_separator = Cow::Borrowed("🐮"); + }); + } } - // Cast back - dec_res.wrap_into_any_response().downcast() - } else { - Ok(res) } + Ok(res) } } From a81f0ca675d268c41962656799e07b820b6c4931 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Mon, 28 Aug 2023 17:12:27 -0700 Subject: [PATCH 4/4] Don't need all the bounds any more --- docs/tutorials/data_provider.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/tutorials/data_provider.md b/docs/tutorials/data_provider.md index 660d041610e..42c49a1c5cf 100644 --- a/docs/tutorials/data_provider.md +++ b/docs/tutorials/data_provider.md @@ -274,8 +274,6 @@ impl DataProvider for CustomDecimalSymbolsProvider

where P: DataProvider, M: KeyedDataMarker, - M::Yokeable: icu_provider::MaybeSendSync + zerofrom::ZeroFrom<'static, M::Yokeable>, - for<'a> yoke::trait_hack::YokeTraitHack<>::Output>: Clone, { #[inline] fn load(&self, req: DataRequest) -> Result, DataError> {