Skip to main content

cinderblock_core/data_layer/
mod.rs

1use crate::Resource;
2
3pub mod in_memory;
4
5/// Persistence backend for a specific resource type.
6///
7/// The trait is parameterized on `R` so that different data layers can
8/// impose different bounds on the resources they support. For example,
9/// `InMemoryDataLayer` has a blanket impl for all `R: Resource`, while
10/// a SQLite data layer can additionally require `R: SqlResource`.
11pub trait DataLayer<R: Resource>: std::fmt::Debug + Send + Sync + 'static {
12    /// Persist a new resource and return the stored version.
13    ///
14    /// The returned resource may differ from the input when the data layer
15    /// provides server-generated values (e.g. autoincrement primary keys).
16    fn create(&self, resource: R) -> impl Future<Output = Result<R, crate::CreateError>> + Send;
17
18    fn read(
19        &self,
20        primary_key: &R::PrimaryKey,
21    ) -> impl Future<Output = Result<R, crate::ReadError>> + Send;
22
23    fn update(&self, resource: R) -> impl Future<Output = Result<(), crate::UpdateError>> + Send;
24
25    /// Remove a resource by primary key, returning the deleted resource.
26    fn destroy(
27        &self,
28        primary_key: &R::PrimaryKey,
29    ) -> impl Future<Output = Result<R, crate::DestroyError>> + Send;
30}