|
| 1 | +use core::convert::Infallible; |
| 2 | + |
| 3 | +use crate::Append; |
| 4 | + |
| 5 | +/// `Persist` wraps a [`PersistBackend`] (`B`) to create a convenient staging area for changes (`C`) |
| 6 | +/// before they are persisted. |
| 7 | +/// |
| 8 | +/// Not all changes to the in-memory representation needs to be written to disk right away, so |
| 9 | +/// [`Persist::stage`] can be used to *stage* changes first and then [`Persist::commit`] can be used |
| 10 | +/// to write changes to disk. |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct Persist<B, C> { |
| 13 | + backend: B, |
| 14 | + stage: C, |
| 15 | +} |
| 16 | + |
| 17 | +impl<B, C> Persist<B, C> |
| 18 | +where |
| 19 | + B: PersistBackend<C>, |
| 20 | + C: Default + Append, |
| 21 | +{ |
| 22 | + /// Create a new [`Persist`] from [`PersistBackend`]. |
| 23 | + pub fn new(backend: B) -> Self { |
| 24 | + Self { |
| 25 | + backend, |
| 26 | + stage: Default::default(), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Stage a `changeset` to be commited later with [`commit`]. |
| 31 | + /// |
| 32 | + /// [`commit`]: Self::commit |
| 33 | + pub fn stage(&mut self, changeset: C) { |
| 34 | + self.stage.append(changeset) |
| 35 | + } |
| 36 | + |
| 37 | + /// Get the changes that have not been commited yet. |
| 38 | + pub fn staged(&self) -> &C { |
| 39 | + &self.stage |
| 40 | + } |
| 41 | + |
| 42 | + /// Commit the staged changes to the underlying persistance backend. |
| 43 | + /// |
| 44 | + /// Returns a backend-defined error if this fails. |
| 45 | + pub fn commit(&mut self) -> Result<(), B::WriteError> { |
| 46 | + let mut temp = C::default(); |
| 47 | + core::mem::swap(&mut temp, &mut self.stage); |
| 48 | + self.backend.write_changes(&temp) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// A persistence backend for [`Persist`]. |
| 53 | +/// |
| 54 | +/// `C` represents the changeset; a datatype that records changes made to in-memory data structures |
| 55 | +/// that are to be persisted, or retrieved from persistence. |
| 56 | +pub trait PersistBackend<C> { |
| 57 | + /// The error the backend returns when it fails to write. |
| 58 | + type WriteError: core::fmt::Debug; |
| 59 | + |
| 60 | + /// The error the backend returns when it fails to load changesets `C`. |
| 61 | + type LoadError: core::fmt::Debug; |
| 62 | + |
| 63 | + /// Writes a changeset to the persistence backend. |
| 64 | + /// |
| 65 | + /// It is up to the backend what it does with this. It could store every changeset in a list or |
| 66 | + /// it inserts the actual changes into a more structured database. All it needs to guarantee is |
| 67 | + /// that [`load_from_persistence`] restores a keychain tracker to what it should be if all |
| 68 | + /// changesets had been applied sequentially. |
| 69 | + /// |
| 70 | + /// [`load_from_persistence`]: Self::load_from_persistence |
| 71 | + fn write_changes(&mut self, changeset: &C) -> Result<(), Self::WriteError>; |
| 72 | + |
| 73 | + /// Return the aggregate changeset `C` from persistence. |
| 74 | + fn load_from_persistence(&mut self) -> Result<C, Self::LoadError>; |
| 75 | +} |
| 76 | + |
| 77 | +impl<C: Default> PersistBackend<C> for () { |
| 78 | + type WriteError = Infallible; |
| 79 | + |
| 80 | + type LoadError = Infallible; |
| 81 | + |
| 82 | + fn write_changes(&mut self, _changeset: &C) -> Result<(), Self::WriteError> { |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | + |
| 86 | + fn load_from_persistence(&mut self) -> Result<C, Self::LoadError> { |
| 87 | + Ok(C::default()) |
| 88 | + } |
| 89 | +} |
0 commit comments