left_right/read/
factory.rs

1use super::ReadHandle;
2use crate::sync::{Arc, AtomicPtr};
3use std::fmt;
4
5/// A type that is both `Sync` and `Send` and lets you produce new [`ReadHandle`] instances.
6///
7/// This serves as a handy way to distribute read handles across many threads without requiring
8/// additional external locking to synchronize access to the non-`Sync` [`ReadHandle`] type. Note
9/// that this _internally_ takes a lock whenever you call [`ReadHandleFactory::handle`], so
10/// you should not expect producing new handles rapidly to scale well.
11pub struct ReadHandleFactory<T> {
12    pub(super) inner: Arc<AtomicPtr<T>>,
13    pub(super) epochs: crate::Epochs,
14}
15
16impl<T> fmt::Debug for ReadHandleFactory<T> {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        f.debug_struct("ReadHandleFactory")
19            .field("epochs", &self.epochs)
20            .finish()
21    }
22}
23
24impl<T> Clone for ReadHandleFactory<T> {
25    fn clone(&self) -> Self {
26        Self {
27            inner: Arc::clone(&self.inner),
28            epochs: Arc::clone(&self.epochs),
29        }
30    }
31}
32
33impl<T> ReadHandleFactory<T> {
34    /// Produce a new [`ReadHandle`] to the same left-right data structure as this factory was
35    /// originally produced from.
36    pub fn handle(&self) -> ReadHandle<T> {
37        ReadHandle::new_with_arc(Arc::clone(&self.inner), Arc::clone(&self.epochs))
38    }
39}