Skip to content

Commit 0a6edfd

Browse files
committed
Add Expander argument to LocaleCanonicalizer
1 parent 5887e27 commit 0a6edfd

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `icu_locale`
1717
- New crate
1818
- Allow `LocaleDirectionality` to wrap a `LocaleExpander` with user-controlled storage (https://github.com/unicode-org/icu4x/pull/5704)
19+
- Allow `LocaleCanonicalizer` to wrap a `LocaleExpander` with user-controlled storage (https://github.com/unicode-org/icu4x/pull/5704)
1920
- `icu_locale_core`
2021
- New crate, renamed from `icu_locid`
2122
- Removed `Ord` and `PartialOrd` impl from `extensions::unicode::Unicode` (https://github.com/unicode-org/icu4x/pull/5617)

components/locale/src/canonicalizer.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ use tinystr::TinyAsciiStr;
3737
///
3838
/// [UTS #35: Annex C, LocaleId Canonicalization]: http://unicode.org/reports/tr35/#LocaleId_Canonicalization
3939
#[derive(Debug)]
40-
pub struct LocaleCanonicalizer {
40+
pub struct LocaleCanonicalizer<Expander = LocaleExpander> {
4141
/// Data to support canonicalization.
4242
aliases: DataPayload<AliasesV2Marker>,
4343
/// Likely subtags implementation for delegation.
44-
expander: LocaleExpander,
44+
expander: Expander,
4545
}
4646

4747
fn uts35_rule_matches<'a, I>(
@@ -197,13 +197,13 @@ fn uts35_check_language_rules(
197197
}
198198

199199
#[cfg(feature = "compiled_data")]
200-
impl Default for LocaleCanonicalizer {
200+
impl Default for LocaleCanonicalizer<LocaleExpander> {
201201
fn default() -> Self {
202202
Self::new()
203203
}
204204
}
205205

206-
impl LocaleCanonicalizer {
206+
impl LocaleCanonicalizer<LocaleExpander> {
207207
/// A constructor which creates a [`LocaleCanonicalizer`] from compiled data.
208208
///
209209
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
@@ -235,14 +235,16 @@ impl LocaleCanonicalizer {
235235
let expander = LocaleExpander::try_new_unstable(provider)?;
236236
Self::try_new_with_expander_unstable(provider, expander)
237237
}
238+
}
238239

240+
impl<Expander: AsRef<LocaleExpander>> LocaleCanonicalizer<Expander> {
239241
/// Creates a [`LocaleCanonicalizer`] with a custom [`LocaleExpander`] and compiled data.
240242
///
241243
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
242244
///
243245
/// [📚 Help choosing a constructor](icu_provider::constructors)
244246
#[cfg(feature = "compiled_data")]
245-
pub const fn new_with_expander(expander: LocaleExpander) -> Self {
247+
pub const fn new_with_expander(expander: Expander) -> Self {
246248
Self {
247249
aliases: DataPayload::from_static_ref(
248250
crate::provider::Baked::SINGLETON_ALIASES_V2_MARKER,
@@ -254,7 +256,7 @@ impl LocaleCanonicalizer {
254256
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new_with_expander)]
255257
pub fn try_new_with_expander_unstable<P>(
256258
provider: &P,
257-
expander: LocaleExpander,
259+
expander: Expander,
258260
) -> Result<Self, DataError>
259261
where
260262
P: DataProvider<AliasesV2Marker> + ?Sized,
@@ -264,7 +266,7 @@ impl LocaleCanonicalizer {
264266
Ok(Self { aliases, expander })
265267
}
266268

267-
icu_provider::gen_any_buffer_data_constructors!((options: LocaleExpander) -> error: DataError,
269+
icu_provider::gen_any_buffer_data_constructors!((options: Expander) -> error: DataError,
268270
functions: [
269271
new_with_expander: skip,
270272
try_new_with_expander_with_any_provider,
@@ -395,7 +397,10 @@ impl LocaleCanonicalizer {
395397
};
396398

397399
locale.id.region = Some(
398-
match (self.expander.maximize(&mut maximized), maximized.region) {
400+
match (
401+
self.expander.as_ref().maximize(&mut maximized),
402+
maximized.region,
403+
) {
399404
(TransformResult::Modified, Some(candidate))
400405
if regions.iter().any(|x| x == candidate) =>
401406
{

0 commit comments

Comments
 (0)