Skip to main content

linera_persistent/
memory.rs

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