Skip to content

Commit 30d488e

Browse files
Use Pattern more directly (#5531)
* Removed the `Store` generic from `Pattern`. It now always uses the unsized `PatternBackend::Store`. Instead of using `Pattern<B, String>` for the owned version, it's now `Box<Pattern<B>>` * Implemented `ToOwned` for `Pattern` so that we can use `Cow<Pattern<B>>`. The owned type is `Box<Pattern<B>>` * Replaced the `FromStr` implementation by `try_from_str`, which returns `Box<Self>` * Together these changes completely remove the need of downstream users to fuss around with the store.
1 parent 0866999 commit 30d488e

File tree

886 files changed

+30026
-17432
lines changed

Some content is hidden

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

886 files changed

+30026
-17432
lines changed

components/experimental/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ icu_properties = { workspace = true }
3838
databake = { workspace = true, optional = true, features = ["derive"] }
3939
either = { workspace = true }
4040
fixed_decimal = { workspace = true }
41-
icu_pattern = { workspace = true , features = ["alloc", "yoke", "zerofrom"]}
41+
icu_pattern = { workspace = true , features = ["alloc", "yoke", "zerovec"]}
4242
litemap = { workspace = true }
4343
tinystr = { workspace = true, features = ["alloc", "zerovec"] }
4444
potential_utf = { workspace = true, features = ["zerovec"] }

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use icu_provider::prelude::*;
1414
use tinystr::UnvalidatedTinyAsciiStr;
1515
use zerovec::{VarZeroVec, ZeroMap};
1616

17+
#[cfg(feature = "serde")]
18+
use icu_pattern::DoublePlaceholder;
1719
use icu_pattern::DoublePlaceholderPattern;
1820

1921
#[cfg(feature = "compiled_data")]
@@ -50,15 +52,27 @@ pub struct CurrencyEssentialsV1<'data> {
5052
/// Represents the standard pattern.
5153
/// NOTE: place holder 0 is the place of the currency value.
5254
/// place holder 1 is the place of the currency sign `¤`.
53-
#[cfg_attr(feature = "serde", serde(borrow))]
54-
pub standard_pattern: Option<DoublePlaceholderPattern<Cow<'data, str>>>,
55+
#[cfg_attr(
56+
feature = "serde",
57+
serde(
58+
borrow,
59+
deserialize_with = "icu_pattern::deserialize_option_borrowed_cow::<DoublePlaceholder, _>"
60+
)
61+
)]
62+
pub standard_pattern: Option<Cow<'data, DoublePlaceholderPattern>>,
5563

5664
// TODO(#4677): Implement the pattern to accept the signed negative and signed positive patterns.
5765
/// Represents the standard alpha_next_to_number pattern.
5866
/// NOTE: place holder 0 is the place of the currency value.
5967
/// place holder 1 is the place of the currency sign `¤`.
60-
#[cfg_attr(feature = "serde", serde(borrow))]
61-
pub standard_alpha_next_to_number_pattern: Option<DoublePlaceholderPattern<Cow<'data, str>>>,
68+
#[cfg_attr(
69+
feature = "serde",
70+
serde(
71+
borrow,
72+
deserialize_with = "icu_pattern::deserialize_option_borrowed_cow::<DoublePlaceholder, _>"
73+
)
74+
)]
75+
pub standard_alpha_next_to_number_pattern: Option<Cow<'data, DoublePlaceholderPattern>>,
6276

6377
/// Contains all the place holders.
6478
#[cfg_attr(feature = "serde", serde(borrow))]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//!
1010
//! Read more about data providers: [`icu_provider`]
1111
12-
use crate::relativetime::provider::PluralPatterns;
13-
use icu_pattern::DoublePlaceholder;
12+
use icu_pattern::DoublePlaceholderPattern;
13+
use icu_plurals::provider::PluralElementsPackedCow;
1414
use icu_provider::prelude::*;
1515

1616
/// Currency Extended V1 data struct.
@@ -23,5 +23,5 @@ use icu_provider::prelude::*;
2323
pub struct CurrencyPatternsDataV1<'data> {
2424
/// Contains the unit patterns for a currency based on plural rules.
2525
#[cfg_attr(feature = "serde", serde(borrow))]
26-
pub patterns: PluralPatterns<'data, DoublePlaceholder>,
26+
pub patterns: PluralElementsPackedCow<'data, DoublePlaceholderPattern>,
2727
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use alloc::borrow::Cow;
1313
use icu_pattern::{DoublePlaceholderPattern, SinglePlaceholderPattern};
1414
use icu_provider::prelude::*;
1515

16+
#[cfg(feature = "serde")]
17+
use icu_pattern::{DoublePlaceholder, SinglePlaceholder};
18+
1619
#[cfg(feature = "compiled_data")]
1720
/// Baked data
1821
///
@@ -34,15 +37,27 @@ pub use crate::provider::Baked;
3437
/// <https://www.unicode.org/reports/tr35/tr35-numbers.html#approximate-number-formatting>
3538
/// <https://www.unicode.org/reports/tr35/tr35-numbers.html#explicit-plus-signs>
3639
pub struct PercentEssentialsV1<'data> {
37-
#[cfg_attr(feature = "serde", serde(borrow))]
40+
#[cfg_attr(
41+
feature = "serde",
42+
serde(
43+
borrow,
44+
deserialize_with = "icu_pattern::deserialize_borrowed_cow::<DoublePlaceholder, _>"
45+
)
46+
)]
3847
/// Represents the standard pattern for signed percents.
3948
/// NOTE: place holder 0 is the place of the percent value.
4049
/// place holder 1 is the place of the plus, minus, or approximate signs.
41-
pub signed_pattern: DoublePlaceholderPattern<Cow<'data, str>>,
50+
pub signed_pattern: Cow<'data, DoublePlaceholderPattern>,
4251

43-
#[cfg_attr(feature = "serde", serde(borrow))]
52+
#[cfg_attr(
53+
feature = "serde",
54+
serde(
55+
borrow,
56+
deserialize_with = "icu_pattern::deserialize_borrowed_cow::<SinglePlaceholder, _>"
57+
)
58+
)]
4459
/// Represents the standard pattern for unsigned percents.
45-
pub unsigned_pattern: SinglePlaceholderPattern<Cow<'data, str>>,
60+
pub unsigned_pattern: Cow<'data, SinglePlaceholderPattern>,
4661

4762
#[cfg_attr(feature = "serde", serde(borrow))]
4863
/// The localize approximate sign.

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//!
1010
//! Read more about data providers: [`icu_provider`]
1111
12-
use crate::relativetime::provider::PluralPatterns;
13-
use icu_pattern::SinglePlaceholder;
12+
use icu_pattern::SinglePlaceholderPattern;
13+
use icu_plurals::provider::PluralElementsPackedCow;
1414
use icu_provider::prelude::*;
1515

1616
#[icu_provider::data_struct(marker(
@@ -26,7 +26,7 @@ pub struct UnitsDisplayNameV1<'data> {
2626
// TODO: use `MeasureUnit` for the units key instead of strings.
2727
/// Contains the long width patterns for the units.
2828
#[cfg_attr(feature = "serde", serde(borrow))]
29-
pub patterns: PluralPatterns<'data, SinglePlaceholder>,
29+
pub patterns: PluralElementsPackedCow<'data, SinglePlaceholderPattern>,
3030
}
3131

3232
impl<'data> UnitsDisplayNameV1<'data> {
@@ -37,17 +37,14 @@ impl<'data> UnitsDisplayNameV1<'data> {
3737
/// The bytes must represent a valid [`icu_plurals::provider::PluralElementsPackedULE`]
3838
pub const unsafe fn from_byte_slice_unchecked(bytes: &'data [u8]) -> Self {
3939
Self {
40-
patterns: PluralPatterns {
41-
strings: icu_plurals::provider::PluralElementsPackedCow {
42-
elements: alloc::borrow::Cow::Borrowed(
43-
// Safety: this function's safety invariant guarantees that the bytes
44-
// represent a valid `PluralElementsPackedULE`
45-
icu_plurals::provider::PluralElementsPackedULE::from_byte_slice_unchecked(
46-
bytes,
47-
),
40+
patterns: icu_plurals::provider::PluralElementsPackedCow {
41+
elements: alloc::borrow::Cow::Borrowed(
42+
// Safety: this function's safety invariant guarantees that the bytes
43+
// represent a valid `PluralElementsPackedULE`
44+
icu_plurals::provider::PluralElementsPackedULE::from_byte_slice_unchecked(
45+
bytes,
4846
),
49-
},
50-
_phantom: core::marker::PhantomData,
47+
),
5148
},
5249
}
5350
}
@@ -58,7 +55,7 @@ impl databake::Bake for UnitsDisplayNameV1<'_> {
5855
fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream {
5956
use zerovec::ule::VarULE;
6057
ctx.insert("icu_experimental::dimension::provider::units");
61-
let bytes = self.patterns.strings.elements.as_byte_slice().bake(ctx);
58+
let bytes = self.patterns.elements.as_byte_slice().bake(ctx);
6259
// Safety: The bytes are returned by `PluralElementsPackedULE::as_byte_slice`.
6360
databake::quote! { unsafe {
6461
icu_experimental::dimension::provider::units::UnitsDisplayNameV1::from_byte_slice_unchecked(#bytes)

components/experimental/src/personnames/specifications/derive_missing_initials.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ pub fn derive_missing_initials(
1818
initial_pattern_str: &str,
1919
initial_sequence_pattern_str: &str,
2020
) -> String {
21-
let initial_pattern: SinglePlaceholderPattern<_> = initial_pattern_str.parse().unwrap();
22-
let initial_sequence_pattern: DoublePlaceholderPattern<_> =
23-
initial_sequence_pattern_str.parse().unwrap();
21+
let initial_pattern =
22+
SinglePlaceholderPattern::try_from_str(initial_pattern_str, Default::default()).unwrap();
23+
let initial_sequence_pattern =
24+
DoublePlaceholderPattern::try_from_str(initial_sequence_pattern_str, Default::default())
25+
.unwrap();
2426

2527
if person_name.has_name_field(requested_field) {
2628
return String::from(person_name.get(requested_field));

components/experimental/src/personnames/specifications/pattern_regex_selector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl FromStr for NameField {
209209
pub fn to_person_name_pattern(value: &str) -> Result<PersonNamePattern, PersonNamesFormatterError> {
210210
let mut name_fields_map: Vec<(NameField, Cow<str>)> = Vec::new();
211211

212-
let parsed_pattern = MultiNamedPlaceholderPattern::from_str(value)?;
212+
let parsed_pattern = MultiNamedPlaceholderPattern::try_from_str(value, Default::default())?;
213213

214214
let mut current_name_field = None;
215215
let mut current_literal = None;

components/experimental/src/relativetime/provider.rs

Lines changed: 5 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
//!
1010
//! Read more about data providers: [`icu_provider`]
1111
12-
use alloc::borrow::Cow;
13-
use core::marker::PhantomData;
1412
#[cfg(feature = "datagen")]
15-
use core::{fmt::Debug, str::FromStr};
16-
use icu_pattern::{Pattern, PatternBackend, SinglePlaceholder};
17-
#[cfg(feature = "datagen")]
18-
use icu_plurals::PluralElements;
19-
use icu_plurals::{PluralCategory, PluralOperands, PluralRules};
13+
use core::fmt::Debug;
14+
use icu_pattern::SinglePlaceholderPattern;
15+
use icu_plurals::provider::PluralElementsPackedCow;
2016
use icu_provider::prelude::*;
2117
use zerovec::ZeroMap;
2218

@@ -71,127 +67,10 @@ pub struct RelativeTimePatternDataV1<'data> {
7167
pub relatives: ZeroMap<'data, i8, str>,
7268
/// How to display times in the past.
7369
#[cfg_attr(feature = "serde", serde(borrow))]
74-
pub past: PluralPatterns<'data, SinglePlaceholder>,
70+
pub past: PluralElementsPackedCow<'data, SinglePlaceholderPattern>,
7571
/// How to display times in the future.
7672
#[cfg_attr(feature = "serde", serde(borrow))]
77-
pub future: PluralPatterns<'data, SinglePlaceholder>,
78-
}
79-
80-
#[derive(Debug)]
81-
#[zerovec::make_varule(PluralCategoryStrULE)]
82-
#[zerovec::skip_derive(Ord)]
83-
#[zerovec::derive(Debug)]
84-
#[cfg_attr(
85-
feature = "serde",
86-
derive(serde::Deserialize),
87-
zerovec::derive(Deserialize)
88-
)]
89-
#[cfg_attr(
90-
feature = "datagen",
91-
derive(serde::Serialize),
92-
zerovec::derive(Serialize)
93-
)]
94-
/// A tuple of [`PluralCategory`] and [`str`].
95-
pub struct PluralCategoryStr<'data>(pub PluralCategory, pub Cow<'data, str>);
96-
97-
/// Display specification for relative times, split over potential plural patterns.
98-
#[derive(Debug, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
99-
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
100-
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
101-
#[cfg_attr(feature = "datagen", databake(path = icu_experimental::relativetime::provider))]
102-
#[yoke(prove_covariance_manually)]
103-
pub struct PluralPatterns<'data, B> {
104-
#[cfg_attr(feature = "serde", serde(borrow))]
105-
#[doc(hidden)] // databake only
106-
pub strings: icu_plurals::provider::PluralElementsPackedCow<'data, str>,
107-
#[cfg_attr(feature = "serde", serde(skip))]
108-
#[doc(hidden)] // databake only
109-
pub _phantom: PhantomData<B>,
110-
}
111-
112-
impl<'data, B> Clone for PluralPatterns<'data, B> {
113-
fn clone(&self) -> Self {
114-
Self {
115-
strings: self.strings.clone(),
116-
_phantom: PhantomData,
117-
}
118-
}
119-
}
120-
121-
impl<'data, B: PatternBackend<Store = str>> PluralPatterns<'data, B> {
122-
/// Returns the pattern for the given [`PluralCategory`].
123-
pub fn get(&'data self, op: PluralOperands, rules: &PluralRules) -> &'data Pattern<B, str> {
124-
Pattern::from_ref_store_unchecked(self.strings.get(op, rules))
125-
}
126-
}
127-
128-
#[cfg(feature = "datagen")]
129-
impl<'data, B: PatternBackend<Store = str>> TryFrom<PluralElements<&'data str>>
130-
for PluralPatterns<'static, B>
131-
where
132-
B::PlaceholderKeyCow<'data>: FromStr,
133-
<B::PlaceholderKeyCow<'data> as FromStr>::Err: Debug,
134-
{
135-
type Error = icu_pattern::PatternError;
136-
137-
fn try_from(elements: PluralElements<&'data str>) -> Result<Self, Self::Error> {
138-
let make_pattern = |s: &&str| Pattern::<B, String>::from_str(s).map(|p| p.take_store());
139-
140-
Ok(Self {
141-
strings: PluralElements::new(make_pattern(elements.other())?.as_str())
142-
.with_zero_value(
143-
Some(elements.zero())
144-
.filter(|&e| e != elements.other())
145-
.map(make_pattern)
146-
.transpose()?
147-
.as_deref(),
148-
)
149-
.with_one_value(
150-
Some(elements.one())
151-
.filter(|&e| e != elements.other())
152-
.map(make_pattern)
153-
.transpose()?
154-
.as_deref(),
155-
)
156-
.with_two_value(
157-
Some(elements.two())
158-
.filter(|&e| e != elements.other())
159-
.map(make_pattern)
160-
.transpose()?
161-
.as_deref(),
162-
)
163-
.with_few_value(
164-
Some(elements.few())
165-
.filter(|&e| e != elements.other())
166-
.map(make_pattern)
167-
.transpose()?
168-
.as_deref(),
169-
)
170-
.with_many_value(
171-
Some(elements.many())
172-
.filter(|&e| e != elements.other())
173-
.map(make_pattern)
174-
.transpose()?
175-
.as_deref(),
176-
)
177-
.with_explicit_zero_value(
178-
elements
179-
.explicit_zero()
180-
.map(make_pattern)
181-
.transpose()?
182-
.as_deref(),
183-
)
184-
.with_explicit_one_value(
185-
elements
186-
.explicit_one()
187-
.map(make_pattern)
188-
.transpose()?
189-
.as_deref(),
190-
)
191-
.into(),
192-
_phantom: PhantomData,
193-
})
194-
}
73+
pub future: PluralElementsPackedCow<'data, SinglePlaceholderPattern>,
19574
}
19675

19776
pub(crate) struct ErasedRelativeTimeFormatV1Marker;

0 commit comments

Comments
 (0)