1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use super::{Dirty, Persist};

pub type Error = std::convert::Infallible;

/// A dummy [`Persist`] implementation that doesn't persist anything, but holds the value
/// in memory.
#[derive(derive_more::Deref)]
pub struct Memory<T> {
    #[deref]
    value: T,
    dirty: Dirty,
}

impl<T> Memory<T> {
    pub fn new(value: T) -> Self {
        Self {
            value,
            dirty: Dirty::new(true),
        }
    }
}

impl<T: Send> Persist for Memory<T> {
    type Error = Error;

    fn as_mut(&mut self) -> &mut T {
        *self.dirty = true;
        &mut self.value
    }

    fn into_value(self) -> T {
        self.value
    }

    async fn persist(&mut self) -> Result<(), Error> {
        *self.dirty = false;
        Ok(())
    }
}