Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DataProvider without AnyProvider in tutorial example #3948

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions docs/tutorials/data_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -269,31 +270,35 @@ use tinystr::tinystr;

pub struct CustomDecimalSymbolsProvider<P>(P);

impl<P> AnyProvider for CustomDecimalSymbolsProvider<P>
impl<P, M> DataProvider<M> for CustomDecimalSymbolsProvider<P>
where
P: AnyProvider
P: DataProvider<M>,
M: KeyedDataMarker,
{
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError> {
let mut any_res = self.0.load_any(key, req)?;
if key == DecimalSymbolsV1Marker::KEY && req.locale.region() == Some(region!("CH")) {
let mut res: DataResponse<DecimalSymbolsV1Marker> = 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("🐮");
});
#[inline]
fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
let mut res = self.0.load(req)?;
if let Some(mut generic_payload) = res.payload.as_mut() {
// Cast from `DataPayload<M>` to `DataPayload<DecimalSymbolsV1Marker>`
let mut any_payload = generic_payload as &mut dyn Any;
if let Some(mut decimal_payload) = any_payload.downcast_mut::<DataPayload<DecimalSymbolsV1Marker>>() {
if req.locale.region() == Some(region!("CH")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put the CH check at the top level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep the region nearer the payload override for readability. I don't have reason to believe that there's a performance difference between checking the TypeId of the payload versus the region; maybe there's a small difference but this code needs to exist either way.

decimal_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();
}
Ok(any_res)
Ok(res)
}
}

let provider = CustomDecimalSymbolsProvider(
AnyPayloadProvider::new_default::<DecimalSymbolsV1Marker>()
);

let formatter = FixedDecimalFormatter::try_new_with_any_provider(
let formatter = FixedDecimalFormatter::try_new_unstable(
&provider,
&locale!("und").into(),
Default::default(),
Expand All @@ -302,7 +307,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(),
Expand Down