Struct icu_provider_adapters::filter::FilterDataProvider

source ·
pub struct FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool,
{ pub inner: D, pub predicate: F, pub filter_name: &'static str, }
Expand description

A data provider that selectively filters out data requests.

Data requests that are rejected by the filter will return a [DataError] with kind Filtered, and they will not be returned by [IterableDynamicDataProvider::iter_ids_for_marker].

Although this struct can be created directly, the traits in this module provide helper functions for common filtering patterns.

Fields§

§inner: D

The data provider to which we delegate requests.

§predicate: F

The predicate function. A return value of true indicates that the request should proceed as normal; a return value of false will reject the request.

§filter_name: &'static str

A name for this filter, used in error messages.

Implementations§

source§

impl<D> FilterDataProvider<D, fn(_: DataIdentifierBorrowed<'_>) -> bool>

source

pub fn new(provider: D, filter_name: &'static str) -> Self

Creates a FilterDataProvider that does not do any filtering.

Filters can be added using Self::with_filter.

source§

impl<D, F> FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool + Sync,

source

pub fn with_filter<'a>( self, predicate: impl Fn(DataIdentifierBorrowed<'_>) -> bool + Sync + 'a, ) -> FilterDataProvider<D, Box<dyn Fn(DataIdentifierBorrowed<'_>) -> bool + Sync + 'a>>
where F: 'a,

Filter out data requests with certain langids according to the predicate function. The predicate should return true to allow a langid and false to reject a langid.

§Examples
use icu_locale::LanguageIdentifier;
use icu_locale::{langid, subtags::language};
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::filter::FilterDataProvider;

let provider = FilterDataProvider::new(HelloWorldProvider, "Demo no-English filter")
    .with_filter(|id| id.locale.language != language!("en"));

// German requests should succeed:
let de = DataIdentifierCow::from_locale(langid!("de").into());
let response: Result<DataResponse<HelloWorldV1Marker>, _> =
    provider.load(DataRequest {
        id: de.as_borrowed(),
        ..Default::default()
});
assert!(matches!(response, Ok(_)));

// English requests should fail:
let en = DataIdentifierCow::from_locale(langid!("en-US").into());
let response: Result<DataResponse<HelloWorldV1Marker>, _> =
    provider.load(DataRequest {
    id: en.as_borrowed(),
    ..Default::default()
});
let response: Result<DataResponse<HelloWorldV1Marker>, _> =
    provider.load(DataRequest {
    id: en.as_borrowed(),
    ..Default::default()
});
assert!(matches!(
    response,
    Err(DataError {
        kind: DataErrorKind::IdentifierNotFound,
        ..
    })
));

// English should not appear in the iterator result:
let available_ids = provider
    .iter_ids()
    .expect("Should successfully make an iterator of supported locales");
assert!(available_ids.contains(&DataIdentifierCow::from_locale(langid!("de").into())));
assert!(!available_ids.contains(&DataIdentifierCow::from_locale(langid!("en").into())));

Trait Implementations§

source§

impl<D, F> AnyProvider for FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool, D: AnyProvider,

source§

fn load_any( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<AnyResponse, DataError>

Loads an [AnyPayload] according to the marker and request.
source§

impl<D, F, M> DataProvider<M> for FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool, M: DataMarker, D: DataProvider<M>,

source§

fn load(&self, req: DataRequest<'_>) -> Result<DataResponse<M>, DataError>

Query the provider for data, returning the result. Read more
source§

impl<D: Debug, F> Debug for FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<D, F, M> DryDataProvider<M> for FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool, M: DataMarker, D: DryDataProvider<M>,

source§

fn dry_load( &self, req: DataRequest<'_>, ) -> Result<DataResponseMetadata, DataError>

This method goes through the motions of load, but only returns the metadata. Read more
source§

impl<D, F, M> DynamicDataProvider<M> for FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool, M: DynamicDataMarker, D: DynamicDataProvider<M>,

source§

fn load_data( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<DataResponse<M>, DataError>

Query the provider for data, returning the result. Read more
source§

impl<D, F, M> DynamicDryDataProvider<M> for FilterDataProvider<D, F>
where F: Fn(DataIdentifierBorrowed<'_>) -> bool, M: DynamicDataMarker, D: DynamicDryDataProvider<M>,

source§

fn dry_load_data( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<DataResponseMetadata, DataError>

This method goes through the motions of load_data, but only returns the metadata. Read more
source§

impl<P0, F> ExportableProvider for FilterDataProvider<P0, F>
where P0: ExportableProvider, F: Fn(DataIdentifierBorrowed<'_>) -> bool + Sync,

source§

fn supported_markers(&self) -> HashSet<DataMarkerInfo>

Returns the set of supported markers
source§

impl<M, D, F> IterableDataProvider<M> for FilterDataProvider<D, F>
where M: DataMarker, F: Fn(DataIdentifierBorrowed<'_>) -> bool, D: IterableDataProvider<M>,

source§

fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError>

Returns a set of [DataIdentifierCow].
source§

impl<M, D, F> IterableDynamicDataProvider<M> for FilterDataProvider<D, F>
where M: DynamicDataMarker, F: Fn(DataIdentifierBorrowed<'_>) -> bool, D: IterableDynamicDataProvider<M>,

source§

fn iter_ids_for_marker( &self, marker: DataMarkerInfo, ) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError>

Given a [DataMarkerInfo], returns a set of [DataIdentifierCow].

Auto Trait Implementations§

§

impl<D, F> Freeze for FilterDataProvider<D, F>
where D: Freeze, F: Freeze,

§

impl<D, F> RefUnwindSafe for FilterDataProvider<D, F>

§

impl<D, F> Send for FilterDataProvider<D, F>
where D: Send, F: Send,

§

impl<D, F> Sync for FilterDataProvider<D, F>
where D: Sync, F: Sync,

§

impl<D, F> Unpin for FilterDataProvider<D, F>
where D: Unpin, F: Unpin,

§

impl<D, F> UnwindSafe for FilterDataProvider<D, F>
where D: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<P> AsDeserializingBufferProvider for P
where P: BufferProvider + ?Sized,

§

fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>

Wrap this [BufferProvider] in a [DeserializingBufferProvider].

This requires enabling the deserialization Cargo feature for the expected format(s):

  • deserialize_json
  • deserialize_postcard_1
  • deserialize_bincode_1
§

impl<P> AsDowncastingAnyProvider for P
where P: AnyProvider + ?Sized,

§

fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>

Returns an object implementing DynamicDataProvider<M> when called on AnyProvider
§

impl<P> AsDynamicDataProviderAnyMarkerWrap for P
where P: DynamicDataProvider<AnyMarker> + ?Sized,

§

fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>

Returns an object implementing AnyProvider when called on DynamicDataProvider<AnyMarker>
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<P> BufferProvider for P
where P: DynamicDataProvider<BufferMarker> + ?Sized,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,