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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::borrow::Borrow;

use super::Map;
use crate::encoding::{Decode, Encode};
use crate::state::State;
use crate::store::{Read, Store};
use crate::Result;

/// A set data structure.
pub struct Set<S: Read, T: Encode + Decode> {
    map: Map<S, T, ()>,
}

impl<S: Read, T: Encode + Decode> State<S> for Set<S, T> {
    /// Constructs a `Set` which is backed by the given store.
    fn wrap_store(store: S) -> Result<Self> {
        Ok(Self {
            map: Map::wrap_store(store)?,
        })
    }
}

impl<S: Read, T: Encode + Decode> Set<S, T> {
    /// Return true if the set contains the given value, or false otherwise.
    ///
    /// If an error is encountered while accessing the store, it will be
    /// returned.
    pub fn contains<U: Borrow<T>>(&self, value: U) -> Result<bool> {
        Ok(self.map.get(value)?.is_some())
    }
}

impl<S: Store, T: Encode + Decode> Set<S, T> {
    /// Inserts the given value into the set. If the value is already in the
    /// set, this is a no-op.
    ///
    /// If an error is encountered while writing to the store, it will be
    /// returned.
    pub fn insert(&mut self, value: T) -> Result<()> {
        self.map.insert(value, ())
    }

    /// Removes the given value from the set.
    ///
    /// If the value is not in the set, this method is a no-op. However, it will
    /// still issue a deletion to the underlying store which may have some
    /// overhead.
    ///
    /// If an error is encountered while deleting from the store, it will be
    /// returned.
    pub fn delete<U: Borrow<T>>(&mut self, value: U) -> Result<()> {
        self.map.delete(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::*;

    #[test]
    fn simple() {
        let mut store = MapStore::new();
        let mut set: Set<_, u64> = Set::wrap_store(&mut store).unwrap();

        assert_eq!(set.contains(1234).unwrap(), false);

        set.insert(1234).unwrap();
        assert_eq!(set.contains(1234).unwrap(), true);

        set.insert(1234).unwrap();

        set.delete(1234).unwrap();
        assert_eq!(set.contains(1234).unwrap(), false);
        set.delete(1234).unwrap();
    }

    #[test]
    fn read_only() {
        let mut store = MapStore::new();
        let mut set: Set<_, u64> = (&mut store).wrap().unwrap();
        set.insert(1234).unwrap();
        set.insert(5678).unwrap();

        let store = store;
        let set: Set<_, u64> = store.wrap().unwrap();
        assert_eq!(set.contains(0).unwrap(), false);
        assert_eq!(set.contains(1234).unwrap(), true);
        assert_eq!(set.contains(5678).unwrap(), true);
    }
}