From 2873b0e5056a8709fd625c8b647ff1de6c56de5b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 17 Oct 2024 22:06:00 -0700 Subject: [PATCH] Add AsRef to LocaleDirectionality (#5704) Fixes https://github.com/unicode-org/icu4x/issues/5297 --- CHANGELOG.md | 1 + components/locale/src/directionality.rs | 28 ++++++++++++++----------- components/locale/src/expander.rs | 6 ++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6e48325763..ba74b11b061 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `icu_experimental` - `icu_locale` - New crate + - Allow `LocaleDirectionality` to wrap a `LocaleExpander` with user-controlled storage (https://github.com/unicode-org/icu4x/pull/5704) - `icu_locale_core` - New crate, renamed from `icu_locid` - Removed `Ord` and `PartialOrd` impl from `extensions::unicode::Unicode` (https://github.com/unicode-org/icu4x/pull/5617) diff --git a/components/locale/src/directionality.rs b/components/locale/src/directionality.rs index bbafb71d9f9..95068be4d73 100644 --- a/components/locale/src/directionality.rs +++ b/components/locale/src/directionality.rs @@ -22,6 +22,8 @@ pub enum Direction { /// Provides methods to determine the direction of a locale. /// +/// The `Expander` generic parameter wraps a [`LocaleExpander`]. +/// /// # Examples /// /// ``` @@ -32,19 +34,19 @@ pub enum Direction { /// assert_eq!(ld.get(&langid!("en")), Some(Direction::LeftToRight)); /// ``` #[derive(Debug)] -pub struct LocaleDirectionality { +pub struct LocaleDirectionality { script_direction: DataPayload, - expander: LocaleExpander, + expander: Expander, } #[cfg(feature = "compiled_data")] -impl Default for LocaleDirectionality { +impl Default for LocaleDirectionality { fn default() -> Self { Self::new() } } -impl LocaleDirectionality { +impl LocaleDirectionality { /// Creates a [`LocaleDirectionality`] from compiled data. /// /// This includes limited likely subtags data, see [`LocaleExpander::new()`]. @@ -52,12 +54,11 @@ impl LocaleDirectionality { pub const fn new() -> Self { Self::new_with_expander(LocaleExpander::new()) } - // Note: This is a custom impl because the bounds on `try_new_unstable` don't suffice #[doc = icu_provider::gen_any_buffer_unstable_docs!(ANY, Self::new)] pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), - ) -> Result { + ) -> Result { let expander = LocaleExpander::try_new_with_any_provider(provider)?; Self::try_new_with_expander_unstable(&provider.as_downcasting(), expander) } @@ -67,11 +68,10 @@ impl LocaleDirectionality { #[cfg(feature = "serde")] pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), - ) -> Result { + ) -> Result { let expander = LocaleExpander::try_new_with_buffer_provider(provider)?; Self::try_new_with_expander_unstable(&provider.as_deserializing(), expander) } - #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new)] pub fn try_new_unstable

(provider: &P) -> Result where @@ -83,7 +83,9 @@ impl LocaleDirectionality { let expander = LocaleExpander::try_new_unstable(provider)?; Self::try_new_with_expander_unstable(provider, expander) } +} +impl> LocaleDirectionality { /// Creates a [`LocaleDirectionality`] with a custom [`LocaleExpander`] and compiled data. /// /// This allows using [`LocaleExpander::new_extended()`] with data for all locales. @@ -108,7 +110,7 @@ impl LocaleDirectionality { /// ); /// ``` #[cfg(feature = "compiled_data")] - pub const fn new_with_expander(expander: LocaleExpander) -> Self { + pub const fn new_with_expander(expander: Expander) -> Self { LocaleDirectionality { script_direction: DataPayload::from_static_ref( crate::provider::Baked::SINGLETON_SCRIPT_DIRECTION_V1_MARKER, @@ -120,8 +122,8 @@ impl LocaleDirectionality { #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new_with_expander)] pub fn try_new_with_expander_unstable

( provider: &P, - expander: LocaleExpander, - ) -> Result + expander: Expander, + ) -> Result where P: DataProvider + ?Sized, { @@ -177,7 +179,7 @@ impl LocaleDirectionality { /// ); /// ``` pub fn get(&self, langid: &LanguageIdentifier) -> Option { - let script = self.expander.get_likely_script(langid)?; + let script = self.expander.as_ref().get_likely_script(langid)?; if self.script_in_ltr(script) { Some(Direction::LeftToRight) @@ -197,6 +199,7 @@ impl LocaleDirectionality { /// See [`LocaleDirectionality::get`] for more information. pub fn is_right_to_left(&self, langid: &LanguageIdentifier) -> bool { self.expander + .as_ref() .get_likely_script(langid) .map(|s| self.script_in_rtl(s)) .unwrap_or(false) @@ -211,6 +214,7 @@ impl LocaleDirectionality { /// See [`LocaleDirectionality::get`] for more information. pub fn is_left_to_right(&self, langid: &LanguageIdentifier) -> bool { self.expander + .as_ref() .get_likely_script(langid) .map(|s| self.script_in_ltr(s)) .unwrap_or(false) diff --git a/components/locale/src/expander.rs b/components/locale/src/expander.rs index 31e82f09a8f..a0f053194ad 100644 --- a/components/locale/src/expander.rs +++ b/components/locale/src/expander.rs @@ -582,6 +582,12 @@ impl LocaleExpander { } } +impl AsRef for LocaleExpander { + fn as_ref(&self) -> &LocaleExpander { + self + } +} + #[cfg(feature = "serde")] #[cfg(test)] mod tests {