linera_persistent/
memory.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{Dirty, Persist};
5
6pub type Error = std::convert::Infallible;
7
8/// A dummy [`Persist`] implementation that doesn't persist anything, but holds the value
9/// in memory.
10#[derive(Default, derive_more::Deref)]
11pub struct Memory<T> {
12    #[deref]
13    value: T,
14    dirty: Dirty,
15}
16
17impl<T> Memory<T> {
18    pub fn new(value: T) -> Self {
19        Self {
20            value,
21            dirty: Dirty::new(true),
22        }
23    }
24}
25
26impl<T: Send> Persist for Memory<T> {
27    type Error = Error;
28
29    fn as_mut(&mut self) -> &mut T {
30        *self.dirty = true;
31        &mut self.value
32    }
33
34    fn into_value(self) -> T {
35        self.value
36    }
37
38    async fn persist(&mut self) -> Result<(), Error> {
39        *self.dirty = false;
40        Ok(())
41    }
42}