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 fp_macros::*,
19 std::ops::Deref,
20 };
21
22 /// Thread-safe counterpart to
23 /// [`RefCountedPointer`](crate::classes::RefCountedPointer).
24 ///
25 /// This is an independent trait (not a supertrait of `RefCountedPointer`),
26 /// matching the pattern used by `SendCloneFn` (independent of `CloneFn`).
27 /// Both traits have their own associated type with different bounds:
28 /// `RefCountedPointer::Of` requires `Clone + Deref`, while
29 /// `SendRefCountedPointer::Of` requires `Clone + Send + Sync + Deref`.
30 pub trait SendRefCountedPointer {
31 /// The thread-safe pointer type constructor.
32 ///
33 /// For `ArcBrand`, this is `Arc<T>` where `T: Send + Sync`.
34 type Of<'a, T: ?Sized + Send + Sync + 'a>: Clone + Send + Sync + Deref<Target = T> + 'a;
35
36 /// Wraps a sized value in a thread-safe pointer.
37 #[document_signature]
38 ///
39 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
40 ///
41 #[document_parameters("The value to wrap.")]
42 ///
43 #[document_returns("The value wrapped in the thread-safe pointer type.")]
44 #[document_examples]
45 ///
46 /// ```
47 /// use fp_library::{
48 /// brands::*,
49 /// functions::*,
50 /// };
51 ///
52 /// let ptr = send_ref_counted_pointer_new::<ArcBrand, _>(42);
53 /// assert_eq!(*ptr, 42);
54 /// ```
55 fn new<'a, T: Send + Sync + 'a>(value: T) -> Self::Of<'a, T>
56 where
57 Self::Of<'a, T>: Sized;
58 }
59
60 /// Wraps a sized value in a thread-safe pointer.
61 #[document_signature]
62 ///
63 #[document_type_parameters(
64 "The pointer brand.",
65 "The lifetime of the value.",
66 "The type of the value to wrap."
67 )]
68 ///
69 #[document_parameters("The value to wrap.")]
70 ///
71 #[document_returns("The value wrapped in the thread-safe pointer type.")]
72 #[document_examples]
73 ///
74 /// ```
75 /// use fp_library::{
76 /// brands::*,
77 /// functions::*,
78 /// };
79 ///
80 /// let ptr = send_ref_counted_pointer_new::<ArcBrand, _>(42);
81 /// assert_eq!(*ptr, 42);
82 /// ```
83 pub fn new<'a, P: SendRefCountedPointer, T: Send + Sync + 'a>(value: T) -> P::Of<'a, T>
84 where
85 P::Of<'a, T>: Sized, {
86 P::new(value)
87 }
88}
89
90pub use inner::*;