Skip to content

Commit c0e2dbc

Browse files
sffcManishearth
andauthored
Replace PluralElementsV1 with PluralElementsPackedULE (#5510)
--------- Co-authored-by: Manish Goregaokar <[email protected]>
1 parent b7d567f commit c0e2dbc

File tree

916 files changed

+101595
-98693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

916 files changed

+101595
-98693
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/experimental/src/dimension/provider/extended_currency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! Read more about data providers: [`icu_provider`]
1111
12-
use icu_plurals::provider::PluralElementsV1;
12+
use icu_plurals::provider::PluralElementsPackedCow;
1313
use icu_provider::prelude::*;
1414

1515
#[cfg(feature = "compiled_data")]
@@ -43,5 +43,5 @@ pub struct CurrencyExtendedDataV1<'data> {
4343
/// Regards to the [Unicode Report TR35](https://unicode.org/reports/tr35/tr35-numbers.html#Currencies),
4444
/// If no matching for specific count, the `other` count will be used.
4545
#[cfg_attr(feature = "serde", serde(borrow))]
46-
pub display_names: PluralElementsV1<'data>,
46+
pub display_names: PluralElementsPackedCow<'data, str>,
4747
}

components/experimental/src/relativetime/provider.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub struct PluralCategoryStr<'data>(pub PluralCategory, pub Cow<'data, str>);
103103
pub struct PluralPatterns<'data, B> {
104104
#[cfg_attr(feature = "serde", serde(borrow))]
105105
#[doc(hidden)] // databake only
106-
pub strings: icu_plurals::provider::PluralElementsV1<'data>,
106+
pub strings: icu_plurals::provider::PluralElementsPackedCow<'data, str>,
107107
#[cfg_attr(feature = "serde", serde(skip))]
108108
#[doc(hidden)] // databake only
109109
pub _phantom: PhantomData<B>,
@@ -126,16 +126,16 @@ impl<'data, B: PatternBackend<Store = str>> PluralPatterns<'data, B> {
126126
}
127127

128128
#[cfg(feature = "datagen")]
129-
impl<'data, B: PatternBackend<Store = str>> TryFrom<PluralElements<'data, str>>
129+
impl<'data, B: PatternBackend<Store = str>> TryFrom<PluralElements<&'data str>>
130130
for PluralPatterns<'static, B>
131131
where
132132
B::PlaceholderKeyCow<'data>: FromStr,
133133
<B::PlaceholderKeyCow<'data> as FromStr>::Err: Debug,
134134
{
135135
type Error = icu_pattern::PatternError;
136136

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

components/plurals/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ icu_benchmark_macros = { path = "../../tools/benchmark/macros" }
3636
icu_locale_core = { path = "../../components/locale_core" }
3737
serde = { workspace = true, features = ["derive"] }
3838
serde_json = { workspace = true }
39+
postcard = { workspace = true, features = ["alloc"] }
3940

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

components/plurals/src/lib.rs

Lines changed: 108 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -878,21 +878,33 @@ where
878878

879879
#[derive(Debug)]
880880
/// A bag of values for different plural cases.
881-
pub struct PluralElements<'a, T: ?Sized> {
882-
zero: Option<&'a T>,
883-
one: Option<&'a T>,
884-
two: Option<&'a T>,
885-
few: Option<&'a T>,
886-
many: Option<&'a T>,
887-
other: &'a T,
888-
explicit_zero: Option<&'a T>,
889-
explicit_one: Option<&'a T>,
881+
pub struct PluralElements<T>(PluralElementsInner<T>);
882+
883+
#[derive(Debug)]
884+
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
885+
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
886+
pub(crate) struct PluralElementsInner<T> {
887+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
888+
zero: Option<T>,
889+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
890+
one: Option<T>,
891+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
892+
two: Option<T>,
893+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
894+
few: Option<T>,
895+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
896+
many: Option<T>,
897+
other: T,
898+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
899+
explicit_zero: Option<T>,
900+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
901+
explicit_one: Option<T>,
890902
}
891903

892-
impl<'a, T: ?Sized + PartialEq> PluralElements<'a, T> {
904+
impl<T> PluralElements<T> {
893905
/// Creates a new [`PluralElements`] with the given default value.
894-
pub fn new(other: &'a T) -> Self {
895-
Self {
906+
pub fn new(other: T) -> Self {
907+
Self(PluralElementsInner {
896908
other,
897909
zero: None,
898910
one: None,
@@ -901,102 +913,119 @@ impl<'a, T: ?Sized + PartialEq> PluralElements<'a, T> {
901913
many: None,
902914
explicit_zero: None,
903915
explicit_one: None,
904-
}
916+
})
905917
}
906918

907-
/// Sets the value for [`PluralCategory::Zero`].
908-
pub fn with_zero_value(self, zero: Option<&'a T>) -> Self {
909-
Self {
910-
zero: zero.filter(|&t| t != self.other),
911-
..self
912-
}
919+
/// The value for [`PluralCategory::Zero`]
920+
pub fn zero(&self) -> &T {
921+
self.0.zero.as_ref().unwrap_or(&self.0.other)
913922
}
914923

915-
/// Sets the value for [`PluralCategory::One`].
916-
pub fn with_one_value(self, one: Option<&'a T>) -> Self {
917-
Self {
918-
one: one.filter(|&t| t != self.other),
919-
..self
920-
}
924+
/// The value for [`PluralCategory::One`]
925+
pub fn one(&self) -> &T {
926+
self.0.one.as_ref().unwrap_or(&self.0.other)
921927
}
922928

923-
/// Sets the value for [`PluralCategory::Two`].
924-
pub fn with_two_value(self, two: Option<&'a T>) -> Self {
925-
Self {
926-
two: two.filter(|&t| t != self.other),
927-
..self
928-
}
929+
/// The value for [`PluralCategory::Two`]
930+
pub fn two(&self) -> &T {
931+
self.0.two.as_ref().unwrap_or(&self.0.other)
929932
}
930933

931-
/// Sets the value for [`PluralCategory::Few`].
932-
pub fn with_few_value(self, few: Option<&'a T>) -> Self {
933-
Self {
934-
few: few.filter(|&t| t != self.other),
935-
..self
936-
}
934+
/// The value for [`PluralCategory::Few`]
935+
pub fn few(&self) -> &T {
936+
self.0.few.as_ref().unwrap_or(&self.0.other)
937937
}
938938

939-
/// Sets the value for [`PluralCategory::Many`].
940-
pub fn with_many_value(self, many: Option<&'a T>) -> Self {
941-
Self {
942-
many: many.filter(|&t| t != self.other),
943-
..self
944-
}
939+
/// The value for [`PluralCategory::Many`]
940+
pub fn many(&self) -> &T {
941+
self.0.many.as_ref().unwrap_or(&self.0.other)
945942
}
946943

947-
/// Sets the value for explicit 0.
948-
pub fn with_explicit_zero_value(self, explicit_zero: Option<&'a T>) -> Self {
949-
Self {
950-
explicit_zero,
951-
..self
952-
}
944+
/// The value for [`PluralCategory::Other`]
945+
pub fn other(&self) -> &T {
946+
&self.0.other
953947
}
954948

955-
/// Sets the value for explicit 1.
956-
pub fn with_explicit_one_value(self, explicit_one: Option<&'a T>) -> Self {
957-
Self {
958-
explicit_one,
959-
..self
960-
}
949+
/// The value used when the [`PluralOperands`] are exactly 0.
950+
pub fn explicit_zero(&self) -> Option<&T> {
951+
self.0.explicit_zero.as_ref()
961952
}
962953

963-
/// The value for [`PluralCategory::Zero`]
964-
pub fn zero(&self) -> &'a T {
965-
self.zero.unwrap_or(self.other)
954+
/// The value used when the [`PluralOperands`] are exactly 1.
955+
pub fn explicit_one(&self) -> Option<&T> {
956+
self.0.explicit_one.as_ref()
966957
}
967958

968-
/// The value for [`PluralCategory::One`]
969-
pub fn one(&self) -> &'a T {
970-
self.one.unwrap_or(self.other)
959+
/// Applies a function `f` to map all values to another type.
960+
pub fn map<B, F: Fn(T) -> B>(self, f: F) -> PluralElements<B> {
961+
let f = &f;
962+
PluralElements(PluralElementsInner {
963+
other: f(self.0.other),
964+
zero: self.0.zero.map(f),
965+
one: self.0.one.map(f),
966+
two: self.0.two.map(f),
967+
few: self.0.few.map(f),
968+
many: self.0.many.map(f),
969+
explicit_zero: self.0.explicit_zero.map(f),
970+
explicit_one: self.0.explicit_one.map(f),
971+
})
971972
}
973+
}
972974

973-
/// The value for [`PluralCategory::Two`]
974-
pub fn two(&self) -> &'a T {
975-
self.two.unwrap_or(self.other)
975+
impl<T: PartialEq> PluralElements<T> {
976+
/// Sets the value for [`PluralCategory::Zero`].
977+
pub fn with_zero_value(self, zero: Option<T>) -> Self {
978+
Self(PluralElementsInner {
979+
zero: zero.filter(|t| *t != self.0.other),
980+
..self.0
981+
})
976982
}
977983

978-
/// The value for [`PluralCategory::Few`]
979-
pub fn few(&self) -> &'a T {
980-
self.few.unwrap_or(self.other)
984+
/// Sets the value for [`PluralCategory::One`].
985+
pub fn with_one_value(self, one: Option<T>) -> Self {
986+
Self(PluralElementsInner {
987+
one: one.filter(|t| *t != self.0.other),
988+
..self.0
989+
})
981990
}
982991

983-
/// The value for [`PluralCategory::Many`]
984-
pub fn many(&self) -> &'a T {
985-
self.many.unwrap_or(self.other)
992+
/// Sets the value for [`PluralCategory::Two`].
993+
pub fn with_two_value(self, two: Option<T>) -> Self {
994+
Self(PluralElementsInner {
995+
two: two.filter(|t| *t != self.0.other),
996+
..self.0
997+
})
986998
}
987999

988-
/// The value for [`PluralCategory::Other`]
989-
pub fn other(&self) -> &'a T {
990-
self.other
1000+
/// Sets the value for [`PluralCategory::Few`].
1001+
pub fn with_few_value(self, few: Option<T>) -> Self {
1002+
Self(PluralElementsInner {
1003+
few: few.filter(|t| *t != self.0.other),
1004+
..self.0
1005+
})
9911006
}
9921007

993-
/// The value used when the [`PluralOperands`] are exactly 0.
994-
pub fn explicit_zero(&self) -> Option<&'a T> {
995-
self.explicit_zero
1008+
/// Sets the value for [`PluralCategory::Many`].
1009+
pub fn with_many_value(self, many: Option<T>) -> Self {
1010+
Self(PluralElementsInner {
1011+
many: many.filter(|t| *t != self.0.other),
1012+
..self.0
1013+
})
9961014
}
9971015

998-
/// The value used when the [`PluralOperands`] are exactly 1.
999-
pub fn explicit_one(&self) -> Option<&'a T> {
1000-
self.explicit_one
1016+
/// Sets the value for explicit 0.
1017+
pub fn with_explicit_zero_value(self, explicit_zero: Option<T>) -> Self {
1018+
Self(PluralElementsInner {
1019+
explicit_zero,
1020+
..self.0
1021+
})
1022+
}
1023+
1024+
/// Sets the value for explicit 1.
1025+
pub fn with_explicit_one_value(self, explicit_one: Option<T>) -> Self {
1026+
Self(PluralElementsInner {
1027+
explicit_one,
1028+
..self.0
1029+
})
10011030
}
10021031
}

0 commit comments

Comments
 (0)