Skip to main content

walrus_memory/mem/
mod.rs

1//! In-memory implementation of the Memory trait.
2
3use crate::Memory;
4use std::sync::{Arc, Mutex};
5
6/// In-memory store backed by `Arc<Mutex<Vec<(String, String)>>>`.
7///
8/// `Clone` is cheap — clones share the same underlying storage.
9#[derive(Default, Debug, Clone)]
10pub struct InMemory {
11    entries: Arc<Mutex<Vec<(String, String)>>>,
12}
13
14impl InMemory {
15    /// Create an empty store.
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Create a store pre-populated with entries.
21    pub fn with_entries(entries: impl IntoIterator<Item = (String, String)>) -> Self {
22        Self {
23            entries: Arc::new(Mutex::new(entries.into_iter().collect())),
24        }
25    }
26}
27
28impl Memory for InMemory {
29    fn get(&self, key: &str) -> Option<String> {
30        let entries = self.entries.lock().unwrap();
31        entries
32            .iter()
33            .find(|(k, _)| k == key)
34            .map(|(_, v)| v.clone())
35    }
36
37    fn entries(&self) -> Vec<(String, String)> {
38        self.entries.lock().unwrap().clone()
39    }
40
41    fn set(&self, key: impl Into<String>, value: impl Into<String>) -> Option<String> {
42        let key = key.into();
43        let value = value.into();
44        let mut entries = self.entries.lock().unwrap();
45        if let Some(existing) = entries.iter_mut().find(|(k, _)| *k == key) {
46            Some(std::mem::replace(&mut existing.1, value))
47        } else {
48            entries.push((key, value));
49            None
50        }
51    }
52
53    fn remove(&self, key: &str) -> Option<String> {
54        let mut entries = self.entries.lock().unwrap();
55        let idx = entries.iter().position(|(k, _)| k == key)?;
56        Some(entries.remove(idx).1)
57    }
58}