concurrent_slice/
guard.rs

1use crate::common::*;
2
3/// The guard is used to recover the owning data from [Chunks](crate::chunks::Chunks).
4#[derive(Debug)]
5#[repr(transparent)]
6pub struct Guard<S> {
7    pub(super) data: Arc<S>,
8}
9
10impl<S> Guard<S>
11where
12    S: Send,
13{
14    /// Tries to recover the owning data.
15    ///
16    /// The method succeeds if the referencing chunk iterator and all chunks are dropped.
17    /// Otherwise, it returns the guard intact.
18    pub fn try_unwrap(self) -> Result<S, Self> {
19        Arc::try_unwrap(self.data).map_err(|data| Guard { data })
20    }
21
22    pub fn unwrap(self) -> S
23    where
24        S: Debug,
25    {
26        self.try_unwrap().unwrap()
27    }
28
29    /// Gets the reference count on the owning data.
30    pub fn ref_count(&self) -> usize {
31        Arc::strong_count(&self.data)
32    }
33}