Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create BoundDataProvider, a DataProvider that always loads for the same key #4883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions provider/core/src/data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use core::marker::PhantomData;
use yoke::Yokeable;

use crate::error::DataError;
use crate::key::DataKey;
use crate::marker::{DataMarker, KeyedDataMarker};
Expand Down Expand Up @@ -127,6 +130,125 @@ where
}
}

/// A data provider that loads data for a specific data type.
///
/// Unlike [`DataProvider`], the provider is bound to a specific key ahead of time.
///
/// [`AnyMarker`]: crate::any::AnyMarker
pub trait BoundDataProvider<M>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Manishearth suggests documenting the purpose for the type.

where
M: DataMarker,
{
/// Query the provider for data, returning the result.
///
/// Returns [`Ok`] if the request successfully loaded data. If data failed to load, returns an
/// Error with more information.
fn load_bound(&self, req: DataRequest) -> Result<DataResponse<M>, DataError>;
/// Returns the [`DataKey`] that this provider uses for loading data.
fn bound_key(&self) -> DataKey;
}

impl<'a, M, P> BoundDataProvider<M> for &'a P
where
M: DataMarker,
P: BoundDataProvider<M> + ?Sized,
{
#[inline]
fn load_bound(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
(*self).load_bound(req)
}
#[inline]
fn bound_key(&self) -> DataKey {
(*self).bound_key()
}
}

impl<M, P> BoundDataProvider<M> for alloc::boxed::Box<P>
where
M: DataMarker,
P: BoundDataProvider<M> + ?Sized,
{
#[inline]
fn load_bound(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
(**self).load_bound(req)
}
#[inline]
fn bound_key(&self) -> DataKey {
(**self).bound_key()
}
}

impl<M, P> BoundDataProvider<M> for alloc::rc::Rc<P>
where
M: DataMarker,
P: BoundDataProvider<M> + ?Sized,
{
#[inline]
fn load_bound(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
(**self).load_bound(req)
}
#[inline]
fn bound_key(&self) -> DataKey {
(**self).bound_key()
}
}

#[cfg(target_has_atomic = "ptr")]
impl<M, P> BoundDataProvider<M> for alloc::sync::Arc<P>
where
M: DataMarker,
P: BoundDataProvider<M> + ?Sized,
{
#[inline]
fn load_bound(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
(**self).load_bound(req)
}
#[inline]
fn bound_key(&self) -> DataKey {
(**self).bound_key()
}
}

/// A [`DataProvider`] associated with a specific key.
///
/// Implements [`BoundDataProvider`].
#[derive(Debug)]
pub struct DataProviderWithKey<M, P> {
inner: P,
_marker: PhantomData<M>,
}

impl<M, P> DataProviderWithKey<M, P>
where
M: KeyedDataMarker,
P: DataProvider<M>,
{
/// Creates a [`DataProviderWithKey`] from a [`DataProvider`] with a [`KeyedDataMarker`].
pub const fn new(inner: P) -> Self {
Self {
inner,
_marker: PhantomData,
}
}
}

impl<M, M0, Y, P> BoundDataProvider<M0> for DataProviderWithKey<M, P>
where
M: KeyedDataMarker<Yokeable = Y>,
M0: DataMarker<Yokeable = Y>,
Y: for<'a> Yokeable<'a>,
P: DataProvider<M>,
{
#[inline]
fn load_bound(&self, req: DataRequest) -> Result<DataResponse<M0>, DataError> {
self.inner.load(req).map(DataResponse::cast)
}
#[inline]
fn bound_key(&self) -> DataKey {
M::KEY
}
}

#[cfg(test)]
mod test {

Expand Down
4 changes: 4 additions & 0 deletions provider/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ pub mod marker;
pub mod serde;

// Types from private modules
pub use crate::data_provider::BoundDataProvider;
pub use crate::data_provider::DataProvider;
pub use crate::data_provider::DataProviderWithKey;
pub use crate::data_provider::DynamicDataProvider;
pub use crate::error::DataError;
pub use crate::error::DataErrorKind;
Expand Down Expand Up @@ -213,6 +215,8 @@ pub mod prelude {
#[cfg(feature = "experimental")]
pub use crate::AuxiliaryKeys;
#[doc(no_inline)]
pub use crate::BoundDataProvider;
#[doc(no_inline)]
pub use crate::BufferMarker;
#[doc(no_inline)]
pub use crate::BufferProvider;
Expand Down
11 changes: 10 additions & 1 deletion provider/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use core::marker::PhantomData;

use crate::{data_key, key::DataKey};
use crate::{data_key, DataKey, DataProvider, DataProviderWithKey};
use yoke::Yokeable;

/// Trait marker for data structs. All types delivered by the data provider must be associated with
Expand Down Expand Up @@ -84,6 +84,15 @@ pub trait DataMarker: 'static {
pub trait KeyedDataMarker: DataMarker {
/// The single [`DataKey`] associated with this marker.
const KEY: DataKey;

/// Binds this [`KeyedDataMarker`] to a provider supporting it.
fn bind<P>(provider: P) -> DataProviderWithKey<Self, P>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this helper function because type inference works better at the call site. Instead of doing

DataProviderWithKey::<MyDataMarkerV1, _>::new(provider)

I can just write

MyDataMarkerV1::bind(provider)

where
P: DataProvider<Self>,
Self: Sized,
{
DataProviderWithKey::new(provider)
}
}

/// A [`DataMarker`] that never returns data.
Expand Down
Loading