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