From 60eca993ed70fa2582cc3c37af9f65927349469d Mon Sep 17 00:00:00 2001 From: Robert Bastian <4706271+robertbastian@users.noreply.github.com> Date: Tue, 9 Jul 2024 08:24:23 +0100 Subject: [PATCH] Remove implicit `LocaleFallbackProvider` constructors (#5183) Now that `LocaleFallbacker` is an explicit argument to `ExportDriver`, it might make sense to always make it explicit on the `LocaleFallbackProvider` as well. The `_unstable`, `_with_buffer_provider`, and `_with_any_provider` constructors are a bit odd for this crate anyway, no other adapter has the constructor trinity. Removing them also has the added benefit of removing the `serde` feature from the crate. --- components/icu/README.md | 6 +- components/icu/src/lib.rs | 6 +- ffi/capi/bindings/c/ICU4XDataProvider.h | 3 - ffi/capi/bindings/cpp/ICU4XDataProvider.d.hpp | 2 - ffi/capi/bindings/cpp/ICU4XDataProvider.hpp | 8 -- ffi/capi/bindings/dart/DataProvider.g.dart | 24 +---- ffi/capi/bindings/js/ICU4XDataProvider.d.ts | 15 +--- ffi/capi/bindings/js/ICU4XDataProvider.mjs | 17 ---- ffi/capi/src/provider.rs | 40 ++------- provider/adapters/Cargo.toml | 3 - provider/adapters/src/fallback/mod.rs | 89 +++---------------- provider/blob/benches/auxkey_bench.rs | 2 +- provider/source/src/collator/mod.rs | 7 +- tutorials/data_management.md | 14 +-- tutorials/data_provider.md | 2 +- 15 files changed, 44 insertions(+), 194 deletions(-) diff --git a/components/icu/README.md b/components/icu/README.md index 4571736edfb..6d4f17a22db 100644 --- a/components/icu/README.md +++ b/components/icu/README.md @@ -53,6 +53,7 @@ special constructors: ```rust use icu::datetime::DateTimeFormatter; use icu::locale::locale; +use icu::locale::fallback::LocaleFallbacker; use icu_provider_adapters::fallback::LocaleFallbackProvider; use icu_provider_blob::BlobDataProvider; @@ -61,10 +62,11 @@ let data: Box<[u8]> = todo!(); let provider = BlobDataProvider::try_new_from_blob(data) .expect("data should be valid"); -let provider = - LocaleFallbackProvider::try_new_with_buffer_provider(provider) +let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&provider) .expect("provider should include fallback data"); +let provider = LocaleFallbackProvider::new(provider, fallbacker); + let dtf = DateTimeFormatter::try_new_with_buffer_provider( &provider, &locale!("es-US").into(), diff --git a/components/icu/src/lib.rs b/components/icu/src/lib.rs index f0f18d005dc..27ffc111677 100644 --- a/components/icu/src/lib.rs +++ b/components/icu/src/lib.rs @@ -53,6 +53,7 @@ //! ```no_run //! use icu::datetime::DateTimeFormatter; //! use icu::locale::locale; +//! use icu::locale::fallback::LocaleFallbacker; //! use icu_provider_adapters::fallback::LocaleFallbackProvider; //! use icu_provider_blob::BlobDataProvider; //! @@ -61,10 +62,11 @@ //! let provider = BlobDataProvider::try_new_from_blob(data) //! .expect("data should be valid"); //! -//! let provider = -//! LocaleFallbackProvider::try_new_with_buffer_provider(provider) +//! let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&provider) //! .expect("provider should include fallback data"); //! +//! let provider = LocaleFallbackProvider::new(provider, fallbacker); +//! //! let dtf = DateTimeFormatter::try_new_with_buffer_provider( //! &provider, //! &locale!("es-US").into(), diff --git a/ffi/capi/bindings/c/ICU4XDataProvider.h b/ffi/capi/bindings/c/ICU4XDataProvider.h index 1004909b047..6a247b52561 100644 --- a/ffi/capi/bindings/c/ICU4XDataProvider.h +++ b/ffi/capi/bindings/c/ICU4XDataProvider.h @@ -33,9 +33,6 @@ ICU4XDataProvider_fork_by_key_result ICU4XDataProvider_fork_by_key(ICU4XDataProv typedef struct ICU4XDataProvider_fork_by_locale_result {union { ICU4XDataError err;}; bool is_ok;} ICU4XDataProvider_fork_by_locale_result; ICU4XDataProvider_fork_by_locale_result ICU4XDataProvider_fork_by_locale(ICU4XDataProvider* self, ICU4XDataProvider* other); -typedef struct ICU4XDataProvider_enable_locale_fallback_result {union { ICU4XDataError err;}; bool is_ok;} ICU4XDataProvider_enable_locale_fallback_result; -ICU4XDataProvider_enable_locale_fallback_result ICU4XDataProvider_enable_locale_fallback(ICU4XDataProvider* self); - typedef struct ICU4XDataProvider_enable_locale_fallback_with_result {union { ICU4XDataError err;}; bool is_ok;} ICU4XDataProvider_enable_locale_fallback_with_result; ICU4XDataProvider_enable_locale_fallback_with_result ICU4XDataProvider_enable_locale_fallback_with(ICU4XDataProvider* self, const ICU4XLocaleFallbacker* fallbacker); diff --git a/ffi/capi/bindings/cpp/ICU4XDataProvider.d.hpp b/ffi/capi/bindings/cpp/ICU4XDataProvider.d.hpp index 4b801230578..9024cd34744 100644 --- a/ffi/capi/bindings/cpp/ICU4XDataProvider.d.hpp +++ b/ffi/capi/bindings/cpp/ICU4XDataProvider.d.hpp @@ -33,8 +33,6 @@ class ICU4XDataProvider { inline diplomat::result fork_by_locale(ICU4XDataProvider& other); - inline diplomat::result enable_locale_fallback(); - inline diplomat::result enable_locale_fallback_with(const ICU4XLocaleFallbacker& fallbacker); inline const capi::ICU4XDataProvider* AsFFI() const; diff --git a/ffi/capi/bindings/cpp/ICU4XDataProvider.hpp b/ffi/capi/bindings/cpp/ICU4XDataProvider.hpp index 6c379fb1987..4930493aa16 100644 --- a/ffi/capi/bindings/cpp/ICU4XDataProvider.hpp +++ b/ffi/capi/bindings/cpp/ICU4XDataProvider.hpp @@ -33,9 +33,6 @@ namespace capi { typedef struct ICU4XDataProvider_fork_by_locale_result {union { ICU4XDataError err;}; bool is_ok;} ICU4XDataProvider_fork_by_locale_result; ICU4XDataProvider_fork_by_locale_result ICU4XDataProvider_fork_by_locale(ICU4XDataProvider* self, ICU4XDataProvider* other); - typedef struct ICU4XDataProvider_enable_locale_fallback_result {union { ICU4XDataError err;}; bool is_ok;} ICU4XDataProvider_enable_locale_fallback_result; - ICU4XDataProvider_enable_locale_fallback_result ICU4XDataProvider_enable_locale_fallback(ICU4XDataProvider* self); - typedef struct ICU4XDataProvider_enable_locale_fallback_with_result {union { ICU4XDataError err;}; bool is_ok;} ICU4XDataProvider_enable_locale_fallback_with_result; ICU4XDataProvider_enable_locale_fallback_with_result ICU4XDataProvider_enable_locale_fallback_with(ICU4XDataProvider* self, const ICU4XLocaleFallbacker* fallbacker); @@ -79,11 +76,6 @@ inline diplomat::result ICU4XDataProvider::fork_ return result.is_ok ? diplomat::result(diplomat::Ok()) : diplomat::result(diplomat::Err(ICU4XDataError::FromFFI(result.err))); } -inline diplomat::result ICU4XDataProvider::enable_locale_fallback() { - auto result = capi::ICU4XDataProvider_enable_locale_fallback(this->AsFFI()); - return result.is_ok ? diplomat::result(diplomat::Ok()) : diplomat::result(diplomat::Err(ICU4XDataError::FromFFI(result.err))); -} - inline diplomat::result ICU4XDataProvider::enable_locale_fallback_with(const ICU4XLocaleFallbacker& fallbacker) { auto result = capi::ICU4XDataProvider_enable_locale_fallback_with(this->AsFFI(), fallbacker.AsFFI()); diff --git a/ffi/capi/bindings/dart/DataProvider.g.dart b/ffi/capi/bindings/dart/DataProvider.g.dart index ac34cd13747..24421d507fe 100644 --- a/ffi/capi/bindings/dart/DataProvider.g.dart +++ b/ffi/capi/bindings/dart/DataProvider.g.dart @@ -92,24 +92,7 @@ final class DataProvider implements ffi.Finalizable { } - /// Enables locale fallbacking for data requests made to this provider. - /// - /// Note that the test provider (from `create_test`) already has fallbacking enabled. - /// - /// See the [Rust documentation for `try_new`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.try_new) for more information. - /// - /// Additional information: [1](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html) - /// - /// Throws [DataError] on failure. - void enableLocaleFallback() { - final result = _ICU4XDataProvider_enable_locale_fallback(_ffi); - if (!result.isOk) { - throw DataError.values[result.union.err]; - } - - } - - /// See the [Rust documentation for `new_with_fallbacker`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.new_with_fallbacker) for more information. + /// See the [Rust documentation for `new`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.new) for more information. /// /// Additional information: [1](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html) /// @@ -153,11 +136,6 @@ external _ResultVoidInt32 _ICU4XDataProvider_fork_by_key(ffi.Pointer // ignore: non_constant_identifier_names external _ResultVoidInt32 _ICU4XDataProvider_fork_by_locale(ffi.Pointer self, ffi.Pointer other); -@meta.ResourceIdentifier('ICU4XDataProvider_enable_locale_fallback') -@ffi.Native<_ResultVoidInt32 Function(ffi.Pointer)>(isLeaf: true, symbol: 'ICU4XDataProvider_enable_locale_fallback') -// ignore: non_constant_identifier_names -external _ResultVoidInt32 _ICU4XDataProvider_enable_locale_fallback(ffi.Pointer self); - @meta.ResourceIdentifier('ICU4XDataProvider_enable_locale_fallback_with') @ffi.Native<_ResultVoidInt32 Function(ffi.Pointer, ffi.Pointer)>(isLeaf: true, symbol: 'ICU4XDataProvider_enable_locale_fallback_with') // ignore: non_constant_identifier_names diff --git a/ffi/capi/bindings/js/ICU4XDataProvider.d.ts b/ffi/capi/bindings/js/ICU4XDataProvider.d.ts index 932b229b52a..e2c1a65e98f 100644 --- a/ffi/capi/bindings/js/ICU4XDataProvider.d.ts +++ b/ffi/capi/bindings/js/ICU4XDataProvider.d.ts @@ -70,20 +70,7 @@ export class ICU4XDataProvider { /** - * Enables locale fallbacking for data requests made to this provider. - - * Note that the test provider (from `create_test`) already has fallbacking enabled. - - * See the {@link https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.try_new Rust documentation for `try_new`} for more information. - - * Additional information: {@link https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html 1} - * @throws {@link FFIError}<{@link ICU4XDataError}> - */ - enable_locale_fallback(): void | never; - - /** - - * See the {@link https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.new_with_fallbacker Rust documentation for `new_with_fallbacker`} for more information. + * See the {@link https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.new Rust documentation for `new`} for more information. * Additional information: {@link https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html 1} * @throws {@link FFIError}<{@link ICU4XDataError}> diff --git a/ffi/capi/bindings/js/ICU4XDataProvider.mjs b/ffi/capi/bindings/js/ICU4XDataProvider.mjs index 5d4aa1a8a22..3aaf06d6eec 100644 --- a/ffi/capi/bindings/js/ICU4XDataProvider.mjs +++ b/ffi/capi/bindings/js/ICU4XDataProvider.mjs @@ -98,23 +98,6 @@ export class ICU4XDataProvider { })(); } - enable_locale_fallback() { - return (() => { - const diplomat_receive_buffer = wasm.diplomat_alloc(5, 4); - wasm.ICU4XDataProvider_enable_locale_fallback(diplomat_receive_buffer, this.underlying); - const is_ok = diplomatRuntime.resultFlag(wasm, diplomat_receive_buffer, 4); - if (is_ok) { - const ok_value = {}; - wasm.diplomat_free(diplomat_receive_buffer, 5, 4); - return ok_value; - } else { - const throw_value = ICU4XDataError_rust_to_js[diplomatRuntime.enumDiscriminant(wasm, diplomat_receive_buffer)]; - wasm.diplomat_free(diplomat_receive_buffer, 5, 4); - throw new diplomatRuntime.FFIError(throw_value); - } - })(); - } - enable_locale_fallback_with(arg_fallbacker) { return (() => { const diplomat_receive_buffer = wasm.diplomat_alloc(5, 4); diff --git a/ffi/capi/src/provider.rs b/ffi/capi/src/provider.rs index 405cc180701..c6a93e8621b 100644 --- a/ffi/capi/src/provider.rs +++ b/ffi/capi/src/provider.rs @@ -176,39 +176,8 @@ pub mod ffi { Ok(()) } - /// Enables locale fallbacking for data requests made to this provider. - /// - /// Note that the test provider (from `create_test`) already has fallbacking enabled. - #[diplomat::rust_link( - icu_provider_adapters::fallback::LocaleFallbackProvider::try_new, - FnInStruct - )] - #[diplomat::rust_link( - icu_provider_adapters::fallback::LocaleFallbackProvider, - Struct, - compact - )] - pub fn enable_locale_fallback(&mut self) -> Result<(), ICU4XDataError> { - use ICU4XDataProviderInner::*; - *self = match core::mem::replace(&mut self.0, Destroyed) { - Destroyed => Err(icu_provider::DataError::custom( - "This provider has been destroyed", - ))?, - #[cfg(feature = "compiled_data")] - Compiled => Err(icu_provider::DataError::custom( - "The compiled provider cannot be modified", - ))?, - Empty => Err(icu_provider::DataErrorKind::MarkerNotFound.into_error())?, - #[cfg(feature = "buffer_provider")] - Buffer(inner) => convert_buffer_provider( - LocaleFallbackProvider::try_new_with_buffer_provider(inner)?, - ), - }; - Ok(()) - } - #[diplomat::rust_link( - icu_provider_adapters::fallback::LocaleFallbackProvider::new_with_fallbacker, + icu_provider_adapters::fallback::LocaleFallbackProvider::new, FnInStruct )] #[diplomat::rust_link( @@ -233,9 +202,10 @@ pub mod ffi { ))?, Empty => Err(icu_provider::DataErrorKind::MarkerNotFound.into_error())?, #[cfg(feature = "buffer_provider")] - Buffer(inner) => convert_buffer_provider( - LocaleFallbackProvider::new_with_fallbacker(inner, fallbacker.0.clone()), - ), + Buffer(inner) => convert_buffer_provider(LocaleFallbackProvider::new( + inner, + fallbacker.0.clone(), + )), }; Ok(()) } diff --git a/provider/adapters/Cargo.toml b/provider/adapters/Cargo.toml index 5a30c9369f8..457e844680c 100644 --- a/provider/adapters/Cargo.toml +++ b/provider/adapters/Cargo.toml @@ -29,6 +29,3 @@ serde = { workspace = true, features = ["derive", "alloc"], optional = true } icu_provider = { path = "../../provider/core", features = ["macros", "deserialize_json"] } icu_locale = { path = "../../components/locale" } writeable = { path = "../../utils/writeable" } - -[features] -serde = ["dep:serde", "zerovec/serde", "icu_locale/serde", "icu_provider/serde"] diff --git a/provider/adapters/src/fallback/mod.rs b/provider/adapters/src/fallback/mod.rs index c580c1026ca..b93b7b7e766 100644 --- a/provider/adapters/src/fallback/mod.rs +++ b/provider/adapters/src/fallback/mod.rs @@ -5,8 +5,8 @@ //! A data provider wrapper that performs locale fallback. use crate::helpers::result_is_err_missing_locale; -use icu_locale::provider::*; -use icu_locale::LocaleFallbacker; +#[doc(no_inline)] +pub use icu_locale::LocaleFallbacker; use icu_provider::prelude::*; use icu_provider::DryDataProvider; use icu_provider::DynamicDryDataProvider; @@ -20,13 +20,9 @@ use icu_provider::DynamicDryDataProvider; /// use icu_locale::langid; /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; -/// # let provider = HelloWorldProvider; -/// # struct LocaleFallbackProvider; -/// # impl LocaleFallbackProvider { -/// # fn try_new_unstable(provider: HelloWorldProvider) -> Result, ()> { -/// # Ok(icu_provider_adapters::fallback::LocaleFallbackProvider::new_with_fallbacker(provider, icu_locale::LocaleFallbacker::new().static_to_owned())) -/// # } -/// # } +/// use icu_provider_adapters::fallback::LocaleFallbackProvider; +/// +/// let provider = HelloWorldProvider; /// /// let id = DataIdentifierCow::from_locale(langid!("ja-JP").into()); /// @@ -37,8 +33,7 @@ use icu_provider::DynamicDryDataProvider; /// }).expect_err("No fallback"); /// /// // But if we wrap the provider in a fallback provider... -/// let provider = LocaleFallbackProvider::try_new_unstable(provider) -/// .expect("Fallback data present"); +/// let provider = LocaleFallbackProvider::new(provider, icu_locale::LocaleFallbacker::new().static_to_owned()); /// /// // ...then we can load "ja-JP" based on "ja" data /// let response = @@ -62,67 +57,11 @@ pub struct LocaleFallbackProvider

{ fallbacker: LocaleFallbacker, } -impl

LocaleFallbackProvider

-where - P: DataProvider - + DataProvider - + DataProvider, -{ - /// Create a [`LocaleFallbackProvider`] by wrapping another data provider and then loading - /// fallback data from it. - /// - /// If the data provider being wrapped does not contain fallback data, use - /// [`LocaleFallbackProvider::new_with_fallbacker`]. - pub fn try_new_unstable(provider: P) -> Result { - let fallbacker = LocaleFallbacker::try_new_unstable(&provider)?; - Ok(Self { - inner: provider, - fallbacker, - }) - } -} - -impl

LocaleFallbackProvider

-where - P: AnyProvider, -{ - /// Create a [`LocaleFallbackProvider`] by wrapping another data provider and then loading - /// fallback data from it. - /// - /// If the data provider being wrapped does not contain fallback data, use - /// [`LocaleFallbackProvider::new_with_fallbacker`]. - pub fn try_new_with_any_provider(provider: P) -> Result { - let fallbacker = LocaleFallbacker::try_new_with_any_provider(&provider)?; - Ok(Self { - inner: provider, - fallbacker, - }) - } -} - -#[cfg(feature = "serde")] -impl

LocaleFallbackProvider

-where - P: BufferProvider, -{ - /// Create a [`LocaleFallbackProvider`] by wrapping another data provider and then loading - /// fallback data from it. - /// - /// If the data provider being wrapped does not contain fallback data, use - /// [`LocaleFallbackProvider::new_with_fallbacker`]. - pub fn try_new_with_buffer_provider(provider: P) -> Result { - let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&provider)?; - Ok(Self { - inner: provider, - fallbacker, - }) - } -} - impl

LocaleFallbackProvider

{ - /// Wrap a provider with an arbitrary fallback engine. + /// Wraps a provider with a provider performing fallback under the given fallbacker. /// - /// This relaxes the requirement that the wrapped provider contains its own fallback data. + /// If the underlying provider contains deduplicated data, it is important to use the + /// same fallback data that `ExportDriver` used. /// /// # Examples /// @@ -147,7 +86,7 @@ impl

LocaleFallbackProvider

{ /// // `HelloWorldProvider` does not contain fallback data, /// // but we can construct a fallbacker with `icu_locale`'s /// // compiled data. - /// let provider = LocaleFallbackProvider::new_with_fallbacker( + /// let provider = LocaleFallbackProvider::new( /// provider, /// LocaleFallbacker::new().static_to_owned(), /// ); @@ -162,7 +101,7 @@ impl

LocaleFallbackProvider

{ /// /// assert_eq!("Hallo Welt", german_hello_world.payload.get().message); /// ``` - pub fn new_with_fallbacker(provider: P, fallbacker: LocaleFallbacker) -> Self { + pub fn new(provider: P, fallbacker: LocaleFallbacker) -> Self { Self { inner: provider, fallbacker, @@ -340,10 +279,8 @@ fn dry_test() { } } - let provider = LocaleFallbackProvider::new_with_fallbacker( - TestProvider, - LocaleFallbacker::new().static_to_owned(), - ); + let provider = + LocaleFallbackProvider::new(TestProvider, LocaleFallbacker::new().static_to_owned()); assert_eq!( provider diff --git a/provider/blob/benches/auxkey_bench.rs b/provider/blob/benches/auxkey_bench.rs index 3e867e12b70..e3e486305e0 100644 --- a/provider/blob/benches/auxkey_bench.rs +++ b/provider/blob/benches/auxkey_bench.rs @@ -311,7 +311,7 @@ fn auxkey_bench_for_version(c: &mut Criterion, blob: &[u8], version_id: &str) { b.iter(|| BlobDataProvider::try_new_from_blob(black_box(blob).into()).unwrap()); }); - let provider = LocaleFallbackProvider::new_with_fallbacker( + let provider = LocaleFallbackProvider::new( BlobDataProvider::try_new_from_blob(black_box(blob).into()).unwrap(), LocaleFallbacker::new().static_to_owned(), ); diff --git a/provider/source/src/collator/mod.rs b/provider/source/src/collator/mod.rs index 95f48618cdb..2c59cc9dcd0 100644 --- a/provider/source/src/collator/mod.rs +++ b/provider/source/src/collator/mod.rs @@ -291,10 +291,13 @@ collation_provider!( fn test_zh_non_baked() { use core::cmp::Ordering; use icu::collator::{Collator, CollatorOptions}; + use icu::locale::fallback::LocaleFallbacker; use icu_provider_adapters::fallback::LocaleFallbackProvider; - let provider = - LocaleFallbackProvider::try_new_unstable(SourceDataProvider::new_testing()).unwrap(); + let provider = LocaleFallbackProvider::new( + SourceDataProvider::new_testing(), + LocaleFallbacker::new_without_data(), + ); // Note: ㄅ is Bopomofo. { diff --git a/tutorials/data_management.md b/tutorials/data_management.md index 729ab4ed148..ed23d358aa4 100644 --- a/tutorials/data_management.md +++ b/tutorials/data_management.md @@ -149,12 +149,12 @@ This will generate a `my_data_blob.postcard` file containing the serialized data Unlike `BakedDataProvider`, `BlobDataProvider` (and `FsDataProvider`) does not perform locale fallbacking. For example, if `en-US` is requested but only `en` data is available, then the data request will fail. To enable fallback, we can wrap the provider in a `LocaleFallbackProvider`. -Note that fallback comes at a cost, as fallbacking code and data has to be included and executed on every request. If you don't need fallback (disclaimer: you probably do), you can use the `BlobDataProvider` directly (for `BakedDataProvider`, see [`FallbackMode::Preresolved`](https://docs.rs/icu_datagen/latest/icu_datagen/enum.FallbackMode.html)). +Note that fallback comes at a cost, as fallbacking code and data has to be included and executed on every request. If you don't need fallback (disclaimer: you probably do), you can use the `BlobDataProvider` directly (for baked data, see [`Options::skip_internal_fallback`](https://docs.rs/icu_provider_baked/latest/icu_provider_baked/export/struct.Options.html)). We can then use the provider in our code: ```rust,no_run -use icu::locale::{locale, Locale}; +use icu::locale::{locale, Locale, fallback::LocaleFallbacker}; use icu::calendar::DateTime; use icu::datetime::{DateTimeFormatter, options::length}; use icu_provider_adapters::fallback::LocaleFallbackProvider; @@ -168,9 +168,11 @@ fn main() { BlobDataProvider::try_new_from_blob(blob.into_boxed_slice()) .expect("blob should be valid"); - let buffer_provider = LocaleFallbackProvider::try_new_with_buffer_provider(buffer_provider) + let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&buffer_provider) .expect("Provider should contain fallback rules"); + let buffer_provider = LocaleFallbackProvider::new(buffer_provider, fallbacker); + let options = length::Bag::from_date_time_style(length::Date::Long, length::Time::Medium); let dtf = DateTimeFormatter::try_new_with_buffer_provider(&buffer_provider, &LOCALE.into(), options.into()) @@ -203,7 +205,7 @@ But there is more to optimize. You might have noticed this in the output of the We can instead use `TypedDateTimeFormatter`, which only supports formatting `DateTime`s: ```rust,no_run -use icu::locale::{locale, Locale}; +use icu::locale::{locale, Locale, fallback::LocaleFallbacker}; use icu::calendar::{DateTime, Gregorian}; use icu::datetime::{TypedDateTimeFormatter, options::length}; use icu_provider_adapters::fallback::LocaleFallbackProvider; @@ -217,9 +219,11 @@ fn main() { BlobDataProvider::try_new_from_blob(blob.into_boxed_slice()) .expect("blob should be valid"); - let buffer_provider = LocaleFallbackProvider::try_new_with_buffer_provider(buffer_provider) + let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&buffer_provider) .expect("Provider should contain fallback rules"); + let buffer_provider = LocaleFallbackProvider::new(buffer_provider, fallbacker); + let options = length::Bag::from_date_time_style(length::Date::Long, length::Time::Medium); let dtf = TypedDateTimeFormatter::::try_new_with_buffer_provider(&buffer_provider, &LOCALE.into(), options.into()) diff --git a/tutorials/data_provider.md b/tutorials/data_provider.md index 789b366ae72..3788721e3bc 100644 --- a/tutorials/data_provider.md +++ b/tutorials/data_provider.md @@ -291,7 +291,7 @@ where // Set up a HelloWorldProvider with fallback let provider = ResolvedLocaleProvider { - inner: LocaleFallbackProvider::new_with_fallbacker( + inner: LocaleFallbackProvider::new( HelloWorldProvider, LocaleFallbacker::new().static_to_owned(), ),