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
use error::Error;
use std::sync::Mutex;

/// A managed instance that can be shared by cloning across threads.
#[derive(Debug)]
pub struct Snapshot<T> {
    inner: Mutex<T>,
}

impl<T> Snapshot<T> {
    /// Create a new Snapshot value.
    pub fn new(inner: T) -> Self {
        Self {
            inner: Mutex::new(inner),
        }
    }

    /// Create a clone of the underlying value and return it.
    pub fn get(&self) -> Result<T, Error>
    where
        T: Clone,
    {
        let inner = self.inner.lock().map_err(|_| "lock poisoned")?;
        Ok(inner.clone())
    }
}