fp_library/types/
rc_ptr.rs1#[fp_macros::document_module]
18mod inner {
19 use {
20 crate::{
21 brands::RcBrand,
22 classes::{
23 Pointer,
24 RefCountedPointer,
25 UnsizedCoercible,
26 },
27 },
28 fp_macros::*,
29 std::{
30 cell::RefCell,
31 rc::Rc,
32 },
33 };
34
35 impl Pointer for RcBrand {
36 type Of<'a, T: ?Sized + 'a> = Rc<T>;
37
38 #[document_signature]
40 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
42 #[document_parameters("The value to wrap.")]
44 #[document_returns("The value wrapped in an `Rc`.")]
46 #[document_examples]
48 fn new<'a, T: 'a>(value: T) -> Rc<T> {
59 Rc::new(value)
60 }
61 }
62
63 impl RefCountedPointer for RcBrand {
64 type CloneableOf<'a, T: ?Sized + 'a> = Rc<T>;
65 type TakeCellOf<'a, T: 'a> = Rc<RefCell<Option<T>>>;
66
67 #[document_signature]
69 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
71 #[document_parameters("The value to wrap.")]
73 #[document_returns("The value wrapped in an `Rc`.")]
75 #[document_examples]
77 fn cloneable_new<'a, T: 'a>(value: T) -> Rc<T> {
88 Rc::new(value)
89 }
90
91 #[document_signature]
93 #[document_type_parameters(
95 "The lifetime of the wrapped value.",
96 "The type of the wrapped value."
97 )]
98 #[document_parameters("The pointer to attempt to unwrap.")]
100 #[document_returns("`Ok(value)` if this is the sole reference, otherwise `Err(ptr)`.")]
102 #[document_examples]
104 fn try_unwrap<'a, T: 'a>(ptr: Rc<T>) -> Result<T, Rc<T>> {
115 Rc::try_unwrap(ptr)
116 }
117
118 #[document_signature]
120 #[document_type_parameters("The lifetime of the value.", "The type of the value to store.")]
122 #[document_parameters("The value to store in the cell.")]
124 #[document_returns("A new `Rc<RefCell<Option<T>>>` containing the value.")]
126 #[document_examples]
128 fn take_cell_new<'a, T: 'a>(value: T) -> Rc<RefCell<Option<T>>> {
139 Rc::new(RefCell::new(Some(value)))
140 }
141
142 #[document_signature]
144 #[document_type_parameters("The lifetime of the value.", "The type of the stored value.")]
146 #[document_parameters("The cell to take the value from.")]
148 #[document_returns("`Some(value)` if the cell still contains a value, `None` otherwise.")]
150 #[document_examples]
152 fn take_cell_take<'a, T: 'a>(cell: &Rc<RefCell<Option<T>>>) -> Option<T> {
164 cell.borrow_mut().take()
165 }
166 }
167
168 impl UnsizedCoercible for RcBrand {
169 #[document_signature]
171 #[document_type_parameters(
173 "The lifetime of the closure.",
174 "The input type of the function.",
175 "The output type of the function."
176 )]
177 #[document_parameters("The closure to coerce.")]
179 #[document_returns("The closure wrapped in an `Rc` as a trait object.")]
181 #[document_examples]
183 fn coerce_fn<'a, A: 'a, B: 'a>(f: impl 'a + Fn(A) -> B) -> Rc<dyn 'a + Fn(A) -> B> {
194 Rc::new(f)
195 }
196 }
197}
198
199#[cfg(test)]
200mod tests {
201
202 use crate::{
203 brands::RcBrand,
204 classes::{
205 RefCountedPointer,
206 pointer::new,
207 ref_counted_pointer::cloneable_new,
208 },
209 };
210
211 #[test]
213 fn test_rc_new() {
214 let ptr = new::<RcBrand, _>(42);
215 assert_eq!(*ptr, 42);
216 }
217
218 #[test]
220 fn test_rc_cloneable_new() {
221 let ptr = cloneable_new::<RcBrand, _>(42);
222 assert_eq!(*ptr, 42);
223 }
224
225 #[test]
227 fn test_rc_clone() {
228 let ptr = cloneable_new::<RcBrand, _>(42);
229 let clone = ptr.clone();
230 assert_eq!(*clone, 42);
231 }
232
233 #[test]
237 fn test_rc_try_unwrap() {
238 let ptr = cloneable_new::<RcBrand, _>(42);
239 assert_eq!(RcBrand::try_unwrap(ptr), Ok(42));
240
241 let ptr = cloneable_new::<RcBrand, _>(42);
242 let _clone = ptr.clone();
243 assert!(RcBrand::try_unwrap(ptr).is_err());
244 }
245}