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