Skip to content

Commit

Permalink
Rename AnyPayloadProvider to FixedProvider, make it generic (unicode-…
Browse files Browse the repository at this point in the history
…org#5502)

Part 1 of unicode-org#3947

not yet planning on doing part 2

<!--
Thank you for your pull request to ICU4X!

Reminder: try to use [Conventional
Comments](https://conventionalcomments.org/) to make comments clearer.

Please see
https://github.com/unicode-org/icu4x/blob/main/CONTRIBUTING.md for
general
information on contributing to ICU4X.
-->
  • Loading branch information
Manishearth authored Sep 10, 2024
1 parent e266337 commit b7d567f
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 170 deletions.
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
# Changelog

## icu4x 2.0

- Components
- General
- Compiled data updated to CLDR 45 and ICU 75 (unicode-org#4782)
- `icu_calendar`
- `icu_collections`
- `icu_normalizer`
- `icu_datetime`
- `icu_experimental`
- `icu_locid`
- `icu_locid_transform`
- `icu_plurals`
- `icu_properties`
- `icu_segmenter`
- `icu_timezone`
- Data model and providers
- `icu_datagen`
- `icu_provider`
- `icu_provider_blob`
- `icu_provider_adapters`
- Replace `AnyPayloadProvider` with `FixedProvider<M>`
- FFI:
- All languages
- JS
- C++
- `icu_harfbuzz`
- Utilities
- `calendrical_calculations`:
- `databake`
- `fixed_decimal`
- `icu_pattern`
- `litemap`
- `yoke`
- `zerofrom`
- `zerotrie`
- `zerovec`
- `writeable`


## icu4x 1.5.x


Expand Down
4 changes: 2 additions & 2 deletions components/decimal/benches/fixed_decimal_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use fixed_decimal::FixedDecimal;
use icu_decimal::provider::DecimalSymbolsV1Marker;
use icu_decimal::FixedDecimalFormatter;
use icu_provider_adapters::any_payload::AnyPayloadProvider;
use icu_provider_adapters::fixed::FixedProvider;

fn triangular_nums(range: f64) -> Vec<isize> {
// Use Lcg64Xsh32, a small, fast PRNG.
Expand All @@ -26,7 +26,7 @@ fn triangular_nums(range: f64) -> Vec<isize> {

fn overview_bench(c: &mut Criterion) {
let nums = triangular_nums(1e9);
let provider = AnyPayloadProvider::new_default::<DecimalSymbolsV1Marker>();
let provider = FixedProvider::<DecimalSymbolsV1Marker>::new_default();
c.bench_function("icu_decimal/overview", |b| {
b.iter(|| {
// This benchmark demonstrates the performance of the format function on 1000 numbers
Expand Down
4 changes: 2 additions & 2 deletions components/decimal/src/grouper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn test_grouper() {
use crate::FixedDecimalFormatter;
use fixed_decimal::FixedDecimal;
use icu_provider::prelude::*;
use icu_provider_adapters::any_payload::AnyPayloadProvider;
use icu_provider_adapters::fixed::FixedProvider;
use writeable::assert_writeable_eq;

let western_sizes = GroupingSizesV1 {
Expand Down Expand Up @@ -154,7 +154,7 @@ fn test_grouper() {
for cas in &cases {
for i in 0..4 {
let dec = FixedDecimal::from(1).multiplied_pow10((i as i16) + 3);
let provider = AnyPayloadProvider::from_owned::<DecimalSymbolsV1Marker>(
let provider = FixedProvider::<DecimalSymbolsV1Marker>::from_owned(
crate::provider::DecimalSymbolsV1 {
grouping_sizes: cas.sizes,
..Default::default()
Expand Down
29 changes: 10 additions & 19 deletions ffi/capi/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ pub mod ffi {
}
}

use icu_decimal::provider::{
AffixesV1, DecimalSymbolsV1, DecimalSymbolsV1Marker, GroupingSizesV1,
};
use icu_decimal::provider::{AffixesV1, DecimalSymbolsV1, GroupingSizesV1};
let mut new_digits = ['\0'; 10];
for (old, new) in digits
.iter()
Expand Down Expand Up @@ -113,22 +111,15 @@ pub mod ffi {
.map(Into::into)
.unwrap_or(options.grouping_strategy);
Ok(Box::new(FixedDecimalFormatter(
icu_decimal::FixedDecimalFormatter::try_new_with_any_provider(
&icu_provider_adapters::any_payload::AnyPayloadProvider::from_any_payload::<
icu_decimal::provider::DecimalSymbolsV1Marker,
>(
icu_provider::DataPayload::<DecimalSymbolsV1Marker>::from_owned(
DecimalSymbolsV1 {
plus_sign_affixes,
minus_sign_affixes,
decimal_separator: str_to_cow(decimal_separator),
grouping_separator: str_to_cow(grouping_separator),
grouping_sizes,
digits,
},
)
.wrap_into_any_payload(),
),
icu_decimal::FixedDecimalFormatter::try_new_unstable(
&icu_provider_adapters::fixed::FixedProvider::from_owned(DecimalSymbolsV1 {
plus_sign_affixes,
minus_sign_affixes,
decimal_separator: str_to_cow(decimal_separator),
grouping_separator: str_to_cow(grouping_separator),
grouping_sizes,
digits,
}),
&Default::default(),
options,
)?,
Expand Down
131 changes: 0 additions & 131 deletions provider/adapters/src/any_payload.rs

This file was deleted.

123 changes: 123 additions & 0 deletions provider/adapters/src/fixed.rs
Original file line number Diff line number Diff line change
@@ -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 core::fmt;
use icu_provider::any::MaybeSendSync;
use icu_provider::prelude::*;
use yoke::trait_hack::YokeTraitHack;
use yoke::Yokeable;

/// A data provider that returns clones of a fixed type-erased payload.
///
/// [`FixedProvider`] 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::fixed::FixedProvider;
/// use std::borrow::Cow;
/// use writeable::assert_writeable_eq;
///
/// let provider =
/// FixedProvider::<HelloWorldV1Marker>::from_static(&HelloWorldV1 {
/// message: Cow::Borrowed("custom hello world"),
/// });
///
/// // Check that it works:
/// let formatter = HelloWorldFormatter::try_new_with_any_provider(
/// &provider,
/// &Default::default(),
/// )
/// .expect("marker matches");
/// assert_writeable_eq!(formatter.format(), "custom hello world");
///
/// # struct DummyMarker;
/// # impl DynamicDataMarker for DummyMarker {
/// # type DataStruct = <HelloWorldV1Marker as DynamicDataMarker>::DataStruct;
/// # }
/// # impl DataMarker for DummyMarker {
/// # const INFO: DataMarkerInfo = DataMarkerInfo::from_path(icu_provider::marker::data_marker_path!("dummy@1"));
/// # }
/// // Requests for invalid markers get MissingDataMarker
/// assert!(matches!(
/// provider.load_any(DummyMarker::INFO, Default::default()),
/// Err(DataError {
/// kind: DataErrorKind::MarkerNotFound,
/// ..
/// })
/// ))
/// ```
#[allow(clippy::exhaustive_structs)] // this type is stable
pub struct FixedProvider<M: DataMarker> {
data: DataPayload<M>,
}

impl<M: DataMarker> FixedProvider<M> {
/// Creates a `FixedProvider` with an owned (allocated) payload of the given data.
pub fn from_owned(data: M::DataStruct) -> Self {
Self::from_payload(DataPayload::from_owned(data))
}

/// Creates a `FixedProvider` with a statically borrowed payload of the given data.
pub fn from_static(data: &'static M::DataStruct) -> Self {
FixedProvider {
data: DataPayload::from_static_ref(data),
}
}

/// Creates a `FixedProvider` from an existing [`DataPayload`].
pub fn from_payload(data: DataPayload<M>) -> Self {
FixedProvider { data }
}

/// Creates a `FixedProvider` with the default (allocated) version of the data struct.
pub fn new_default() -> Self
where
M::DataStruct: Default,
{
Self::from_owned(M::DataStruct::default())
}
}

impl<M: DataMarker> AnyProvider for FixedProvider<M>
where
for<'a> YokeTraitHack<<M::DataStruct as Yokeable<'a>>::Output>: Clone,
M::DataStruct: MaybeSendSync,
{
fn load_any(&self, marker: DataMarkerInfo, _: DataRequest) -> Result<AnyResponse, DataError> {
marker.match_marker(M::INFO)?;
Ok(AnyResponse {
metadata: DataResponseMetadata::default(),
payload: self.data.clone().wrap_into_any_payload(),
})
}
}

impl<M> DataProvider<M> for FixedProvider<M>
where
M: DataMarker,
for<'a> YokeTraitHack<<M::DataStruct as Yokeable<'a>>::Output>: Clone,
{
fn load(&self, _: DataRequest) -> Result<DataResponse<M>, DataError> {
Ok(DataResponse {
metadata: Default::default(),
payload: self.data.clone(),
})
}
}

impl<M> fmt::Debug for FixedProvider<M>
where
M: DynamicDataMarker,
M: DataMarker,
for<'a> &'a <M::DataStruct as Yokeable<'a>>::Output: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.data.fmt(f)
}
}
2 changes: 1 addition & 1 deletion provider/adapters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

extern crate alloc;

pub mod any_payload;
pub mod either;
pub mod empty;
pub mod fallback;
pub mod filter;
pub mod fixed;
pub mod fork;
mod helpers;
Loading

0 comments on commit b7d567f

Please sign in to comment.