You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implementors of the DataProvider trait need to implement load_to_receiver. However, load_payload is easier to think about and use. I currently implement load_payload via impl dyn, like this:
/// An abstract data provider that loads a payload given a request object.pubtraitDataProvider<'d>{/// Query the provider for data, loading it into a DataReceiver.////// Returns Ok if the request successfully loaded data. If data failed to load, returns an/// Error with more information.fnload_to_receiver(&self,req:&DataRequest,receiver:&mutdynDataReceiver<'d,'static>,) -> Result<DataResponse,Error>;}/// A response object containing an object as payload and metadata about it.pubstructDataResponseWithPayload<'d,T>whereT:Clone + Debug,{/// Metadata about the returned object.pubresponse:DataResponse,/// The object itself; None if it was not loaded.pubpayload:Option<Cow<'d,T>>,}impl<'d>dynDataProvider<'d> + 'd {/// 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.pubfnload_payload<T>(&self,req:&DataRequest,) -> Result<DataResponseWithPayload<'d,T>,Error>whereT: serde::Deserialize<'static> + erased_serde::Serialize + Any + Clone + Debug,{letmut receiver = DataReceiverForType::<T>::new();let response = self.load_to_receiver(req,&mut receiver)?;Ok(DataResponseWithPayload{
response,payload: receiver.payload,})}}
However, I could instead implement it in a few other ways:
Function on DataProvider with a default implementation. I think this won't work, because DataProvider is trait object safe.
New trait, like DataProviderPlusPlus, with a impl<S> DataProviderPlusPlus for S where S: DataProvider
Standalone function, called like icu_provider::load_payload::<T>(&provider, &req)
I would ideally like to reach a decision that applies not only to this specific case of load_payload, but also more generally when we have low-level traits intended to be implemented paired with high-level traits intended to be used. I have a few other examples where this has come up already.
I'm not really in favor of impl dyn, it means the method is only available on the trait object.
I really like impl TraitExt for T where T: Trait + ?Sized. It is something people have to import, but the TraitExt pattern is reasonably idiomatic rust.
I wish Rust had a way to just .. imply traits this way so that they don't need to be imported.
Standalone functions are also fine, but I prefer methods when possible.
Implementors of the DataProvider trait need to implement
load_to_receiver
. However,load_payload
is easier to think about and use. I currently implementload_payload
viaimpl dyn
, like this:However, I could instead implement it in a few other ways:
Function on DataProvider with a default implementation.I think this won't work, because DataProvider is trait object safe.DataProviderPlusPlus
, with aimpl<S> DataProviderPlusPlus for S where S: DataProvider
icu_provider::load_payload::<T>(&provider, &req)
I would ideally like to reach a decision that applies not only to this specific case of
load_payload
, but also more generally when we have low-level traits intended to be implemented paired with high-level traits intended to be used. I have a few other examples where this has come up already.CC @Manishearth
The text was updated successfully, but these errors were encountered: