I am struggling to express something in rust.
I have the following trait
#[async_trait]
pub trait Store {
type Error: Send + Sync;
type Model;
async fn create(&self, model: &Self::Model) -> Result<StoreCreateResult, Self::Error>;
async fn update(&self, id: &str, update_payload: &Self::Model) -> Result<(), Self::Error>;
async fn delete(&self, id: &str) -> Result<(), Self::Error>;
async fn get(&self, id: &str) -> Result<Option<Self::Model>, Self::Error>;
}
I want to express to consumers of this Trait that the following signature
async fn update(&self, id: &str, update_payload: &Self::Model) -> Result<(), Self::Error>;
Accepts a Partial<Self::Model>
. Ie. a struct which is identical to the original struct, but with all fields marked as Option<T>
I have found the following topics:
- Any Crates for Deriving an "All Fields Optional" Version of Struct for Patching the Original? but a builder pattern is not what I require here, sure consumers could use a builder pattern to arrive on the type I require, but that is not the concern of my trait.
And the optional_struct crate
optional_struct
does exactly what I want. But is there a way to use this macro in the signature of a trait function?
Or what is the idiomatic way to express this in rust?