fp_library/types/
arc_ptr.rs1#[fp_macros::document_module]
18mod inner {
19 use {
20 crate::{
21 brands::ArcBrand,
22 classes::{
23 Pointer,
24 RefCountedPointer,
25 SendRefCountedPointer,
26 SendUnsizedCoercible,
27 UnsizedCoercible,
28 },
29 },
30 fp_macros::*,
31 std::sync::{
32 Arc,
33 Mutex,
34 },
35 };
36
37 impl Pointer for ArcBrand {
38 type Of<'a, T: ?Sized + 'a> = Arc<T>;
39
40 #[document_signature]
42 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
44 #[document_parameters("The value to wrap.")]
46 #[document_returns("The value wrapped in an `Arc`.")]
48 #[document_examples]
50 fn new<'a, T: 'a>(value: T) -> Arc<T> {
61 Arc::new(value)
62 }
63 }
64
65 impl RefCountedPointer for ArcBrand {
66 type CloneableOf<'a, T: ?Sized + 'a> = Arc<T>;
67 type TakeCellOf<'a, T: 'a> = Arc<Mutex<Option<T>>>;
68
69 #[document_signature]
71 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
73 #[document_parameters("The value to wrap.")]
75 #[document_returns("The value wrapped in an `Arc`.")]
77 #[document_examples]
79 fn cloneable_new<'a, T: 'a>(value: T) -> Arc<T> {
90 Arc::new(value)
91 }
92
93 #[document_signature]
95 #[document_type_parameters(
97 "The lifetime of the wrapped value.",
98 "The type of the wrapped value."
99 )]
100 #[document_parameters("The pointer to attempt to unwrap.")]
102 #[document_returns("`Ok(value)` if this is the sole reference, otherwise `Err(ptr)`.")]
104 #[document_examples]
106 fn try_unwrap<'a, T: 'a>(ptr: Arc<T>) -> Result<T, Arc<T>> {
117 Arc::try_unwrap(ptr)
118 }
119
120 #[document_signature]
122 #[document_type_parameters("The lifetime of the value.", "The type of the value to store.")]
124 #[document_parameters("The value to store in the cell.")]
126 #[document_returns("A new `Arc<Mutex<Option<T>>>` containing the value.")]
128 #[document_examples]
130 fn take_cell_new<'a, T: 'a>(value: T) -> Arc<Mutex<Option<T>>> {
141 Arc::new(Mutex::new(Some(value)))
142 }
143
144 #[document_signature]
146 #[document_type_parameters("The lifetime of the value.", "The type of the stored value.")]
148 #[document_parameters("The cell to take the value from.")]
150 #[document_returns("`Some(value)` if the cell still contains a value, `None` otherwise.")]
152 #[document_examples]
154 fn take_cell_take<'a, T: 'a>(cell: &Arc<Mutex<Option<T>>>) -> Option<T> {
166 #[allow(clippy::unwrap_used)]
168 cell.lock().unwrap().take()
169 }
170 }
171
172 impl SendRefCountedPointer for ArcBrand {
173 type SendOf<'a, T: ?Sized + Send + Sync + 'a> = Arc<T>;
174
175 #[document_signature]
177 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
179 #[document_parameters("The value to wrap.")]
181 #[document_returns("The value wrapped in an `Arc`.")]
183 #[document_examples]
185 fn send_new<'a, T: Send + Sync + 'a>(value: T) -> Arc<T> {
196 Arc::new(value)
197 }
198 }
199
200 impl UnsizedCoercible for ArcBrand {
201 #[document_signature]
203 #[document_type_parameters(
205 "The lifetime of the closure.",
206 "The input type of the function.",
207 "The output type of the function."
208 )]
209 #[document_parameters("The closure to coerce.")]
211 #[document_returns("The closure wrapped in an `Arc` as a trait object.")]
213 #[document_examples]
215 fn coerce_fn<'a, A: 'a, B: 'a>(f: impl 'a + Fn(A) -> B) -> Arc<dyn 'a + Fn(A) -> B> {
226 Arc::new(f)
227 }
228 }
229
230 impl SendUnsizedCoercible for ArcBrand {
231 #[document_signature]
233 #[document_type_parameters(
235 "The lifetime of the closure.",
236 "The input type of the function.",
237 "The output type of the function."
238 )]
239 #[document_parameters("The closure to coerce.")]
241 #[document_returns("The closure wrapped in an `Arc` as a thread-safe trait object.")]
243 #[document_examples]
245 fn coerce_send_fn<'a, A: 'a, B: 'a>(
256 f: impl 'a + Fn(A) -> B + Send + Sync
257 ) -> Arc<dyn 'a + Fn(A) -> B + Send + Sync> {
258 Arc::new(f)
259 }
260 }
261}
262
263#[cfg(test)]
264mod tests {
265 use crate::{
266 brands::ArcBrand,
267 classes::{
268 RefCountedPointer,
269 pointer::new,
270 ref_counted_pointer::cloneable_new,
271 send_ref_counted_pointer::send_new,
272 },
273 };
274
275 #[test]
277 fn test_arc_new() {
278 let ptr = new::<ArcBrand, _>(42);
279 assert_eq!(*ptr, 42);
280 }
281
282 #[test]
284 fn test_arc_cloneable_new() {
285 let ptr = cloneable_new::<ArcBrand, _>(42);
286 assert_eq!(*ptr, 42);
287 }
288
289 #[test]
291 fn test_arc_send_new() {
292 let ptr = send_new::<ArcBrand, _>(42);
293 assert_eq!(*ptr, 42);
294 }
295
296 #[test]
298 fn test_arc_clone() {
299 let ptr = cloneable_new::<ArcBrand, _>(42);
300 let clone = ptr.clone();
301 assert_eq!(*clone, 42);
302 }
303
304 #[test]
308 fn test_arc_try_unwrap() {
309 let ptr = cloneable_new::<ArcBrand, _>(42);
310 assert_eq!(ArcBrand::try_unwrap(ptr), Ok(42));
311
312 let ptr = cloneable_new::<ArcBrand, _>(42);
313 let _clone = ptr.clone();
314 assert!(ArcBrand::try_unwrap(ptr).is_err());
315 }
316}