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

Replace PluralElementsV1 with PluralElementsPackedULE #5510

Merged
merged 39 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1f151e0
Add TinyVarVar
sffc Sep 8, 2024
79582ad
Fix typo bug on main
sffc Sep 8, 2024
0f7223b
Add PluralElementsPackedULE and use it in relativetime
sffc Sep 8, 2024
1ccd8e0
Make PluralElementsPackedULE not depend on TinyVarVarULE
sffc Sep 9, 2024
0be2b08
Revert "Add TinyVarVar"
sffc Sep 9, 2024
dae6881
Clippy: create more intermediate types
sffc Sep 9, 2024
121339f
More refactoring
sffc Sep 9, 2024
e2d3fdd
features
sffc Sep 9, 2024
0dfe79c
clippy
sffc Sep 9, 2024
38c778b
cargo make testdata
sffc Sep 9, 2024
9f21225
cargo make bakeddata experimental
sffc Sep 9, 2024
6712c93
Fix baked safety comment
sffc Sep 9, 2024
e8faf2f
cargo make bakeddata experimental
sffc Sep 9, 2024
922b1a7
Clean up PluralCategoryAndMetadata
sffc Sep 9, 2024
0570498
More safety comments
sffc Sep 10, 2024
5ec4aac
fmt
sffc Sep 10, 2024
964562d
Try to write deserialize_with fn
sffc Sep 10, 2024
3a99f60
feature gate
Manishearth Sep 10, 2024
7e6cb9d
superfluous bound
Manishearth Sep 10, 2024
6e429a1
lol this works?????
Manishearth Sep 10, 2024
7043da7
link to rustc bug
Manishearth Sep 10, 2024
fdd273e
deserialize_with in relativetime
sffc Sep 10, 2024
fa70e60
Review feedback
sffc Sep 10, 2024
9b30c63
rm PluralElementsV1; replace in currency formatter
sffc Sep 10, 2024
38f6676
cargo make testdata
sffc Sep 10, 2024
ba45af4
cargo make bakeddata experimental
sffc Sep 10, 2024
effaef0
get_specials_tuples
sffc Sep 10, 2024
687cf68
add PluralElementsPackedCowStr
sffc Sep 10, 2024
5a3101f
Revert all changes in the experimental crate
sffc Sep 10, 2024
c33063c
Small changes to experimental crate call sites
sffc Sep 10, 2024
594863b
cargo make bakeddata experimental
sffc Sep 10, 2024
1b055db
doc, fmt, missing_apis
sffc Sep 10, 2024
7714c3b
Don't expose serde impl on `PluralElements`
sffc Sep 10, 2024
e858f66
rm PluralElementsField
sffc Sep 10, 2024
382c846
Move struct definitions closer together
sffc Sep 10, 2024
f8c10e5
clippy
sffc Sep 10, 2024
9670ab8
Make PluralElementsPackedCow generic
sffc Sep 10, 2024
3b307df
cargo make bakeddata experimental
sffc Sep 10, 2024
55c6d25
Make the fn private now that PluralElementsPackedCow is generic
sffc Sep 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ impl<'l> Writeable for LongFormattedCurrency<'l> {
W: core::fmt::Write + ?Sized,
{
let operands = self.value.into();
let display_name = self.extended.display_names.get(operands, self.plural_rules);
let display_name = self
.extended
.display_names
.get(operands, self.plural_rules)
.1;
let pattern = self.patterns.patterns.get(operands, self.plural_rules);
let formatted_value = self.fixed_decimal_formatter.format(self.value);
let interpolated = pattern.interpolate((formatted_value, display_name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//!
//! Read more about data providers: [`icu_provider`]

use icu_plurals::provider::PluralElementsV1;
use alloc::borrow::Cow;

use icu_plurals::provider::PluralElementsPackedULE;
use icu_provider::prelude::*;

#[cfg(feature = "compiled_data")]
Expand Down Expand Up @@ -42,6 +44,13 @@ pub struct CurrencyExtendedDataV1<'data> {
/// # NOTE
/// Regards to the [Unicode Report TR35](https://unicode.org/reports/tr35/tr35-numbers.html#Currencies),
/// If no matching for specific count, the `other` count will be used.
#[cfg_attr(feature = "serde", serde(borrow))]
pub display_names: PluralElementsV1<'data>,
#[cfg_attr(
feature = "serde",
serde(borrow),
serde(
// Explicit disambiguation due to https://github.com/rust-lang/rust/issues/130180
deserialize_with = "icu_plurals::provider::deserialize_plural_elements_packed_cow::<_, str>"
)
)]
pub display_names: Cow<'data, PluralElementsPackedULE<str>>,
}
115 changes: 56 additions & 59 deletions components/experimental/src/relativetime/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use core::marker::PhantomData;
use core::{fmt::Debug, str::FromStr};
use icu_pattern::{Pattern, PatternBackend, SinglePlaceholder};
#[cfg(feature = "datagen")]
use icu_plurals::PluralElements;
use icu_plurals::{
provider::{FourBitMetadata, PluralElementsPackedULE},
PluralElements,
};
use icu_plurals::{PluralCategory, PluralOperands, PluralRules};
use icu_provider::prelude::*;
use zerovec::ZeroMap;
Expand Down Expand Up @@ -101,9 +104,16 @@ pub struct PluralCategoryStr<'data>(pub PluralCategory, pub Cow<'data, str>);
#[cfg_attr(feature = "datagen", databake(path = icu_experimental::relativetime::provider))]
#[yoke(prove_covariance_manually)]
pub struct PluralPatterns<'data, B> {
#[cfg_attr(feature = "serde", serde(borrow))]
#[cfg_attr(
feature = "serde",
serde(borrow),
serde(
// Explicit disambiguation due to https://github.com/rust-lang/rust/issues/130180
deserialize_with = "icu_plurals::provider::deserialize_plural_elements_packed_cow::<_, str>"
)
)]
#[doc(hidden)] // databake only
pub strings: icu_plurals::provider::PluralElementsV1<'data>,
pub strings: Cow<'data, icu_plurals::provider::PluralElementsPackedULE<str>>,
#[cfg_attr(feature = "serde", serde(skip))]
#[doc(hidden)] // databake only
pub _phantom: PhantomData<B>,
Expand All @@ -121,76 +131,63 @@ impl<'data, B> Clone for PluralPatterns<'data, B> {
impl<'data, B: PatternBackend<Store = str>> PluralPatterns<'data, B> {
/// Returns the pattern for the given [`PluralCategory`].
pub fn get(&'data self, op: PluralOperands, rules: &PluralRules) -> &'data Pattern<B, str> {
Pattern::from_ref_store_unchecked(self.strings.get(op, rules))
Pattern::from_ref_store_unchecked(self.strings.get(op, rules).1)
}
}

#[cfg(feature = "datagen")]
impl<'data, B: PatternBackend<Store = str>> TryFrom<PluralElements<'data, str>>
impl<'data, B: PatternBackend<Store = str>> TryFrom<PluralElements<&'data str>>
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
for PluralPatterns<'static, B>
where
B::PlaceholderKeyCow<'data>: FromStr,
<B::PlaceholderKeyCow<'data> as FromStr>::Err: Debug,
{
type Error = icu_pattern::PatternError;

fn try_from(elements: PluralElements<str>) -> Result<Self, Self::Error> {
let make_pattern = |s: &str|
fn try_from(elements: PluralElements<&'data str>) -> Result<Self, Self::Error> {
let make_pattern = |s: &&str|
// TODO: Make pattern support apostrophes
Pattern::<B, String>::from_str(&s.replace('\'', "''")).map(|p| p.take_store());
Pattern::<B, String>::from_str(&s.replace('\'', "''")).map(|p| (FourBitMetadata::zero(), p.take_store()));

let plural_elements = PluralElements::new(make_pattern(elements.other())?)
.with_zero_value(
Some(elements.zero())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?,
)
.with_one_value(
Some(elements.one())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?,
)
.with_two_value(
Some(elements.two())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?,
)
.with_few_value(
Some(elements.few())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?,
)
.with_many_value(
Some(elements.many())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?,
)
.with_explicit_zero_value(elements.explicit_zero().map(make_pattern).transpose()?)
.with_explicit_one_value(elements.explicit_one().map(make_pattern).transpose()?);

let packed_pattern_box =
zerovec::ule::encode_varule_to_box::<_, PluralElementsPackedULE<str>>(&plural_elements);

Ok(Self {
strings: PluralElements::new(make_pattern(elements.other())?.as_str())
.with_zero_value(
Some(elements.zero())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?
.as_deref(),
)
.with_one_value(
Some(elements.one())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?
.as_deref(),
)
.with_two_value(
Some(elements.two())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?
.as_deref(),
)
.with_few_value(
Some(elements.few())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?
.as_deref(),
)
.with_many_value(
Some(elements.many())
.filter(|&e| e != elements.other())
.map(make_pattern)
.transpose()?
.as_deref(),
)
.with_explicit_zero_value(
elements
.explicit_zero()
.map(make_pattern)
.transpose()?
.as_deref(),
)
.with_explicit_one_value(
elements
.explicit_one()
.map(make_pattern)
.transpose()?
.as_deref(),
)
.into(),
strings: Cow::Owned(packed_pattern_box),
_phantom: PhantomData,
})
}
Expand Down
1 change: 1 addition & 0 deletions components/plurals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ icu_benchmark_macros = { path = "../../tools/benchmark/macros" }
icu_locale_core = { path = "../../components/locale_core" }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
postcard = { workspace = true, features = ["alloc"] }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = { workspace = true }
Expand Down
Loading
Loading