From fffa6242a5dec396be4b5477a415db39cc96d4e5 Mon Sep 17 00:00:00 2001 From: Robert Bastian <4706271+robertbastian@users.noreply.github.com> Date: Mon, 3 Jun 2024 23:45:46 +0200 Subject: [PATCH] `BufferProvider` is `DynamicDataProvider` (#4992) --- provider/adapters/src/either.rs | 15 ------ provider/adapters/src/empty.rs | 10 ---- provider/adapters/src/fallback/mod.rs | 18 ------- provider/adapters/src/filter/mod.rs | 20 -------- provider/adapters/src/fork/by_error.rs | 44 ---------------- provider/adapters/src/fork/mod.rs | 4 +- provider/blob/benches/blob_version_bench.rs | 4 +- provider/blob/src/blob_data_provider.rs | 10 ++-- provider/blob/src/export/iter.rs | 10 ---- provider/core/src/buf.rs | 56 ++------------------- provider/core/src/hello_world.rs | 6 +-- provider/core/src/serde/mod.rs | 2 +- provider/fs/src/fs_data_provider.rs | 4 +- 13 files changed, 18 insertions(+), 185 deletions(-) diff --git a/provider/adapters/src/either.rs b/provider/adapters/src/either.rs index eefb0c1106f..49b457e4d70 100644 --- a/provider/adapters/src/either.rs +++ b/provider/adapters/src/either.rs @@ -33,21 +33,6 @@ impl AnyProvider for EitherProvider { } } -impl BufferProvider for EitherProvider { - #[inline] - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - use EitherProvider::*; - match self { - A(p) => p.load_buffer(key, req), - B(p) => p.load_buffer(key, req), - } - } -} - impl, P1: DynamicDataProvider> DynamicDataProvider for EitherProvider { diff --git a/provider/adapters/src/empty.rs b/provider/adapters/src/empty.rs index 333f21464a8..55ec88e351b 100644 --- a/provider/adapters/src/empty.rs +++ b/provider/adapters/src/empty.rs @@ -60,16 +60,6 @@ impl AnyProvider for EmptyDataProvider { } } -impl BufferProvider for EmptyDataProvider { - fn load_buffer( - &self, - key: DataKey, - base_req: DataRequest, - ) -> Result, DataError> { - Err(self.error_kind.with_req(key, base_req)) - } -} - impl DynamicDataProvider for EmptyDataProvider where M: DataMarker, diff --git a/provider/adapters/src/fallback/mod.rs b/provider/adapters/src/fallback/mod.rs index 459f5e59b92..98d22dee2cc 100644 --- a/provider/adapters/src/fallback/mod.rs +++ b/provider/adapters/src/fallback/mod.rs @@ -249,24 +249,6 @@ where } } -impl

BufferProvider for LocaleFallbackProvider

-where - P: BufferProvider, -{ - fn load_buffer( - &self, - key: DataKey, - base_req: DataRequest, - ) -> Result, DataError> { - self.run_fallback( - key, - base_req, - |req| self.inner.load_buffer(key, req), - |res| &mut res.metadata, - ) - } -} - impl DynamicDataProvider for LocaleFallbackProvider

where P: DynamicDataProvider, diff --git a/provider/adapters/src/filter/mod.rs b/provider/adapters/src/filter/mod.rs index 8c409258358..072ea3500c5 100644 --- a/provider/adapters/src/filter/mod.rs +++ b/provider/adapters/src/filter/mod.rs @@ -99,26 +99,6 @@ where } } -impl BufferProvider for RequestFilterDataProvider -where - F: Fn(DataRequest) -> bool, - D: BufferProvider, -{ - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - if (self.predicate)(req) { - self.inner.load_buffer(key, req) - } else { - Err(DataErrorKind::FilteredResource - .with_str_context(self.filter_name) - .with_req(key, req)) - } - } -} - impl AnyProvider for RequestFilterDataProvider where F: Fn(DataRequest) -> bool, diff --git a/provider/adapters/src/fork/by_error.rs b/provider/adapters/src/fork/by_error.rs index 855ac66302a..2cca0a678d0 100644 --- a/provider/adapters/src/fork/by_error.rs +++ b/provider/adapters/src/fork/by_error.rs @@ -45,27 +45,6 @@ impl ForkByErrorProvider { } } -impl BufferProvider for ForkByErrorProvider -where - P0: BufferProvider, - P1: BufferProvider, - F: ForkByErrorPredicate, -{ - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - let result = self.0.load_buffer(key, req); - match result { - Ok(ok) => return Ok(ok), - Err(err) if !self.2.test(key, Some(req), err) => return Err(err), - _ => (), - }; - self.1.load_buffer(key, req) - } -} - impl AnyProvider for ForkByErrorProvider where P0: AnyProvider, @@ -169,29 +148,6 @@ impl MultiForkByErrorProvider { } } -impl BufferProvider for MultiForkByErrorProvider -where - P: BufferProvider, - F: ForkByErrorPredicate, -{ - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - let mut last_error = F::UNIT_ERROR.with_key(key); - for provider in self.providers.iter() { - let result = provider.load_buffer(key, req); - match result { - Ok(ok) => return Ok(ok), - Err(err) if !self.predicate.test(key, Some(req), err) => return Err(err), - Err(err) => last_error = err, - }; - } - Err(last_error) - } -} - impl AnyProvider for MultiForkByErrorProvider where P: AnyProvider, diff --git a/provider/adapters/src/fork/mod.rs b/provider/adapters/src/fork/mod.rs index 24fa3065ac2..8a58fe45367 100644 --- a/provider/adapters/src/fork/mod.rs +++ b/provider/adapters/src/fork/mod.rs @@ -65,8 +65,8 @@ use predicates::MissingDataKeyPredicate; /// use icu_provider_adapters::fork::ForkByKeyProvider; /// /// struct DummyBufferProvider; -/// impl BufferProvider for DummyBufferProvider { -/// fn load_buffer( +/// impl DynamicDataProvider for DummyBufferProvider { +/// fn load_data( /// &self, /// key: DataKey, /// req: DataRequest, diff --git a/provider/blob/benches/blob_version_bench.rs b/provider/blob/benches/blob_version_bench.rs index b3e7e9db8dd..312d02a04d5 100644 --- a/provider/blob/benches/blob_version_bench.rs +++ b/provider/blob/benches/blob_version_bench.rs @@ -29,7 +29,7 @@ fn blob_version_bench(c: &mut Criterion) { b.iter(|| { for (locale, key_attributes) in black_box(&locales).iter() { black_box(&provider) - .load_buffer( + .load_data( HelloWorldV1Marker::KEY, DataRequest { locale, @@ -46,7 +46,7 @@ fn blob_version_bench(c: &mut Criterion) { b.iter(|| { for (locale, key_attributes) in black_box(&locales).iter() { black_box(&provider) - .load_buffer( + .load_data( HelloWorldV1Marker::KEY, DataRequest { locale, diff --git a/provider/blob/src/blob_data_provider.rs b/provider/blob/src/blob_data_provider.rs index 890146baa4d..b7fbd2f5e66 100644 --- a/provider/blob/src/blob_data_provider.rs +++ b/provider/blob/src/blob_data_provider.rs @@ -119,8 +119,8 @@ impl BlobDataProvider { } } -impl BufferProvider for BlobDataProvider { - fn load_buffer( +impl DynamicDataProvider for BlobDataProvider { + fn load_data( &self, key: DataKey, req: DataRequest, @@ -169,7 +169,7 @@ mod test { assert!( matches!( - provider.load_buffer(HelloWorldV1Marker::KEY, Default::default()), + provider.load_data(HelloWorldV1Marker::KEY, Default::default()), Err(DataError { kind: DataErrorKind::MissingLocale, .. @@ -201,7 +201,7 @@ mod test { assert!( matches!( - provider.load_buffer( + provider.load_data( HelloSingletonV1Marker::KEY, DataRequest { locale: &icu_locale_core::langid!("de").into(), @@ -218,7 +218,7 @@ mod test { assert!( matches!( - provider.load_buffer(HelloSingletonV1Marker::KEY, Default::default()), + provider.load_data(HelloSingletonV1Marker::KEY, Default::default()), Err(DataError { kind: DataErrorKind::MissingLocale, .. diff --git a/provider/blob/src/export/iter.rs b/provider/blob/src/export/iter.rs index 6d3edfeb8e1..b17ea2c20fc 100644 --- a/provider/blob/src/export/iter.rs +++ b/provider/blob/src/export/iter.rs @@ -8,16 +8,6 @@ use crate::BlobDataProvider; use icu_provider::datagen::*; use icu_provider::prelude::*; -impl DynamicDataProvider for BlobDataProvider { - fn load_data( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - self.load_buffer(key, req) - } -} - impl IterableDynamicDataProvider for BlobDataProvider { fn supported_requests_for_key( &self, diff --git a/provider/core/src/buf.rs b/provider/core/src/buf.rs index b54d105f6be..f92a8720e5d 100644 --- a/provider/core/src/buf.rs +++ b/provider/core/src/buf.rs @@ -53,7 +53,7 @@ impl DataMarker for BufferMarker { /// assert_eq!( /// serde_json::from_slice::( /// buffer_provider -/// .load_buffer(HelloWorldV1Marker::KEY, req) +/// .load_data(HelloWorldV1Marker::KEY, req) /// .expect("load should succeed") /// .take_payload() /// .unwrap() @@ -84,59 +84,9 @@ impl DataMarker for BufferMarker { /// ``` /// /// [`as_deserializing()`]: AsDeserializingBufferProvider::as_deserializing -pub trait BufferProvider { - /// Loads a [`DataPayload`]`<`[`BufferMarker`]`>` according to the key and request. - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError>; -} - -impl<'a, T: BufferProvider + ?Sized> BufferProvider for &'a T { - #[inline] - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - (**self).load_buffer(key, req) - } -} - -impl BufferProvider for alloc::boxed::Box { - #[inline] - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - (**self).load_buffer(key, req) - } -} - -impl BufferProvider for alloc::rc::Rc { - #[inline] - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - (**self).load_buffer(key, req) - } -} +pub trait BufferProvider: DynamicDataProvider {} -#[cfg(target_has_atomic = "ptr")] -impl BufferProvider for alloc::sync::Arc { - #[inline] - fn load_buffer( - &self, - key: DataKey, - req: DataRequest, - ) -> Result, DataError> { - (**self).load_buffer(key, req) - } -} +impl + ?Sized> BufferProvider for P {} /// An enum expressing all Serde formats known to ICU4X. #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] diff --git a/provider/core/src/hello_world.rs b/provider/core/src/hello_world.rs index ff1d37296df..e5444073962 100644 --- a/provider/core/src/hello_world.rs +++ b/provider/core/src/hello_world.rs @@ -185,7 +185,7 @@ icu_provider::impl_dynamic_data_provider!(HelloWorldProvider, [HelloWorldV1Marke /// /// let german_hello_world = HelloWorldProvider /// .into_json_provider() -/// .load_buffer(HelloWorldV1Marker::KEY, DataRequest { +/// .load_data(HelloWorldV1Marker::KEY, DataRequest { /// locale: &langid!("de").into(), /// ..Default::default() /// }) @@ -198,8 +198,8 @@ icu_provider::impl_dynamic_data_provider!(HelloWorldProvider, [HelloWorldV1Marke pub struct HelloWorldJsonProvider; #[cfg(feature = "deserialize_json")] -impl BufferProvider for HelloWorldJsonProvider { - fn load_buffer( +impl DynamicDataProvider for HelloWorldJsonProvider { + fn load_data( &self, key: DataKey, req: DataRequest, diff --git a/provider/core/src/serde/mod.rs b/provider/core/src/serde/mod.rs index edd827c3121..f8654714e4b 100644 --- a/provider/core/src/serde/mod.rs +++ b/provider/core/src/serde/mod.rs @@ -165,7 +165,7 @@ where /// - `deserialize_postcard_1` /// - `deserialize_bincode_1` fn load_data(&self, key: DataKey, req: DataRequest) -> Result, DataError> { - let buffer_response = BufferProvider::load_buffer(self.0, key, req)?; + let buffer_response = self.0.load_data(key, req)?; let buffer_format = buffer_response.metadata.buffer_format.ok_or_else(|| { DataError::custom("BufferProvider didn't set BufferFormat").with_req(key, req) })?; diff --git a/provider/fs/src/fs_data_provider.rs b/provider/fs/src/fs_data_provider.rs index 3c3a3c28872..96d4068dd4a 100644 --- a/provider/fs/src/fs_data_provider.rs +++ b/provider/fs/src/fs_data_provider.rs @@ -62,8 +62,8 @@ impl FsDataProvider { } } -impl BufferProvider for FsDataProvider { - fn load_buffer( +impl DynamicDataProvider for FsDataProvider { + fn load_data( &self, key: DataKey, req: DataRequest,