use core::marker::PhantomData; use yoke::Yokeable;
usecrate::prelude::*;
/// A data provider that loads data for a specific [`DataMarkerInfo`]. pubtrait DataProvider<M> 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(&self, req: DataRequest) -> Result<DataResponse<M>, DataError>;
}
/// A data provider that can determine whether it can load a particular data identifier, /// potentially cheaper than actually performing the load. pubtrait DryDataProvider<M: DataMarker>: DataProvider<M> { /// This method goes through the motions of [`load`], but only returns the metadata. /// /// If `dry_load` returns an error, [`load`] must return the same error, but /// not vice-versa. Concretely, [`load`] could return deserialization or I/O errors /// that `dry_load` cannot predict. /// /// [`load`]: DataProvider::load fn dry_load(&self, req: DataRequest) -> Result<DataResponseMetadata, DataError>;
}
/// A [`DataProvider`] that can iterate over all supported [`DataIdentifierCow`]s. /// /// The provider is not allowed to return `Ok` for requests that were not returned by `iter_ids`, /// and must not fail with a [`DataErrorKind::IdentifierNotFound`] for requests that were returned. /// /// ✨ *Enabled with the `alloc` Cargo feature.* #[cfg(feature = "alloc")] pubtrait IterableDataProvider<M: DataMarker>: DataProvider<M> { /// Returns a set of [`DataIdentifierCow`]. fn iter_ids(&self) -> Result<alloc::collections::BTreeSet<DataIdentifierCow<'_>>, DataError>;
}
/// A data provider that loads data for a specific data type. /// /// Unlike [`DataProvider`], there may be multiple markers corresponding to the same data type. pubtrait DynamicDataProvider<M> where
M: DynamicDataMarker,
{ /// 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_data(
&self,
marker: DataMarkerInfo,
req: DataRequest,
) -> Result<DataResponse<M>, DataError>;
}
/// A dynanmic data provider that can determine whether it can load a particular data identifier, /// potentially cheaper than actually performing the load. pubtrait DynamicDryDataProvider<M: DynamicDataMarker>: DynamicDataProvider<M> { /// This method goes through the motions of [`load_data`], but only returns the metadata. /// /// If `dry_load_data` returns an error, [`load_data`] must return the same error, but /// not vice-versa. Concretely, [`load_data`] could return deserialization or I/O errors /// that `dry_load_data` cannot predict. /// /// [`load_data`]: DynamicDataProvider::load_data fn dry_load_data(
&self,
marker: DataMarkerInfo,
req: DataRequest,
) -> Result<DataResponseMetadata, DataError>;
}
/// A [`DynamicDataProvider`] that can iterate over all supported [`DataIdentifierCow`]s for a certain marker. /// /// The provider is not allowed to return `Ok` for requests that were not returned by `iter_ids`, /// and must not fail with a [`DataErrorKind::IdentifierNotFound`] for requests that were returned. /// /// ✨ *Enabled with the `alloc` Cargo feature.* #[cfg(feature = "alloc")] pubtrait IterableDynamicDataProvider<M: DynamicDataMarker>: DynamicDataProvider<M> { /// Given a [`DataMarkerInfo`], returns a set of [`DataIdentifierCow`]. fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<alloc::collections::BTreeSet<DataIdentifierCow<'_>>, DataError>;
}
/// A data provider that loads data for a specific data type. /// /// Unlike [`DataProvider`], the provider is bound to a specific marker ahead of time. /// /// This crate provides [`DataProviderWithMarker`] which implements this trait on a single provider /// with a single marker. However, this trait can also be implemented on providers that fork between /// multiple markers that all return the same data type. For example, it can abstract over many /// calendar systems in the datetime formatter. pubtrait BoundDataProvider<M> where
M: DynamicDataMarker,
{ /// 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 [`DataMarkerInfo`] that this provider uses for loading data. fn bound_marker(&self) -> DataMarkerInfo;
}
usesuper::*; usecrate::hello_world::*; use alloc::borrow::Cow; use alloc::string::String; use core::fmt::Debug; use serde::{Deserialize, Serialize};
// This tests DataProvider borrow semantics with a dummy data provider based on a // JSON string. It also exercises most of the data provider code paths.
/// A data struct serialization-compatible with HelloWorld used for testing mismatched types #[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, yoke::Yokeable)] pubstruct HelloAlt {
message: String,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.