fp_library/classes/send_ref_counted_pointer.rs
1//! A trait for thread-safe reference-counted pointers.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{brands::*, functions::*};
7//!
8//! let ptr = send_ref_counted_pointer_new::<ArcBrand, _>(42);
9//! assert_eq!(*ptr, 42);
10//! ```
11
12use super::RefCountedPointer;
13use std::ops::Deref;
14
15/// Extension trait for thread-safe reference-counted pointers.
16///
17/// This follows the same pattern as `SendCloneableFn` extends `CloneableFn`,
18/// adding a `SendOf` associated type with explicit `Send + Sync` bounds.
19pub trait SendRefCountedPointer: RefCountedPointer {
20 /// The thread-safe pointer type constructor.
21 ///
22 /// For `ArcBrand`, this is `Arc<T>` where `T: Send + Sync`.
23 type SendOf<T: ?Sized + Send + Sync>: Clone + Send + Sync + Deref<Target = T>;
24
25 /// Wraps a sized value in a thread-safe pointer.
26 ///
27 /// ### Type Signature
28 ///
29 /// `forall a. Send a => a -> SendRefCountedPointer a`
30 ///
31 /// ### Type Parameters
32 ///
33 /// * `T`: The type of the value to wrap.
34 ///
35 /// ### Parameters
36 ///
37 /// * `value`: The value to wrap.
38 ///
39 /// ### Returns
40 ///
41 /// The value wrapped in the thread-safe pointer type.
42 ///
43 /// ### Examples
44 ///
45 /// ```
46 /// use fp_library::{brands::*, functions::*};
47 ///
48 /// let ptr = send_ref_counted_pointer_new::<ArcBrand, _>(42);
49 /// assert_eq!(*ptr, 42);
50 /// ```
51 fn send_new<T: Send + Sync>(value: T) -> Self::SendOf<T>
52 where
53 Self::SendOf<T>: Sized;
54}
55
56/// Wraps a sized value in a thread-safe pointer.
57///
58/// ### Type Signature
59///
60/// `forall p a. (SendRefCountedPointer p, Send a) => a -> SendRefCountedPointer a`
61///
62/// ### Type Parameters
63///
64/// * `P`: The pointer brand.
65/// * `T`: The type of the value to wrap.
66///
67/// ### Parameters
68///
69/// * `value`: The value to wrap.
70///
71/// ### Returns
72///
73/// The value wrapped in the thread-safe pointer type.
74///
75/// ### Examples
76///
77/// ```
78/// use fp_library::{brands::*, functions::*};
79///
80/// let ptr = send_ref_counted_pointer_new::<ArcBrand, _>(42);
81/// assert_eq!(*ptr, 42);
82/// ```
83pub fn send_new<P: SendRefCountedPointer, T: Send + Sync>(value: T) -> P::SendOf<T>
84where
85 P::SendOf<T>: Sized,
86{
87 P::send_new(value)
88}