1mod ointer;
4pub use ointer::*;
5pub mod boxed;
6pub use boxed::*;
7pub mod rc;
8pub mod sync;
9
10pub type Ox<T> = OBox<T>;
12pub type Oc<T> = rc::ORc<T>;
14pub type Ok<T> = rc::OWeak<T>;
16pub type Orc<T> = sync::OArc<T>;
18pub type Oak<T> = sync::OWeak<T>;
20
21#[cfg(test)]
23mod tests {
24 use super::{rc::*, sync::*, *};
26 use std::{mem::size_of, pin::Pin, rc::Rc, sync::*};
27
28 #[test]
30 fn test() {
31 {
32 let mut o = OBox::new(1);
34 assert_eq!(*o, 1);
35 assert_eq!(o.get::<bool>(), false);
36 assert_eq!(*o, 1);
37 *o = i32::default();
38 assert_eq!(*o, i32::default());
39 o.set_bool(true);
40 let b = o.get_bool();
41 assert_eq!(b, true);
42 o.set_mut(false);
43 assert_eq!(o, Pin::into_inner(OBox::pin(Default::default())));
44 }
45 {
46 let mut o = BArc::new(1);
48 assert_eq!(*o, 1);
49 assert_eq!(o.get::<bool>(), false);
50
51 #[derive(Clone, Copy, PartialEq, Debug)]
53 enum MySmallEnum {
54 _A,
55 B,
56 _C,
57 }
58 assert_eq!(size_of::<MySmallEnum>(), 1);
59
60 o.set_mut(MySmallEnum::B);
61 assert_eq!(*o, 1);
62 assert_eq!(o.get::<MySmallEnum>(), MySmallEnum::B);
63
64 o.map_mut(|b: &mut bool, p| {
66 *b = !*b;
67 *p = Default::default();
68 });
69 assert_eq!(*o.downgrade().upgrade().unwrap(), Default::default());
70 }
71 {
72 let mut a = Arc::new(13);
73
74 define_enum_ointers!(
76 MyEnumOinters {
77 Box<f64> = 1,
78 Arc<i32> = 2,
79 i32 = 5
80 },
81 8
82 );
83
84 let mut e = MyEnumOinters::new(2, a.clone());
85 assert_eq!(Arc::strong_count(&a), 2);
86
87 assert_eq!(
89 e.map_enum_mut(
90 |_| panic!(),
91 |p| {
92 let i = **p;
93 *p = Arc::new(15);
94 assert_eq!(Arc::strong_count(&a), 1);
95 a = p.clone();
96 i
97 },
98 |_| panic!()
99 ),
100 13
101 );
102 assert_eq!(e.map_enum(|_| panic!(), |p1| **p1, |_| panic!()), 15);
103 assert_eq!(Arc::strong_count(&a), 2);
104
105 e.set_mut(1, Box::new(2.0));
107 assert_eq!(Arc::strong_count(&a), 1);
108 assert_eq!(e.map_enum(|p| **p, |_| panic!(), |_| panic!()), 2.0);
109 assert_eq!(size_of::<Option<MyEnumOinters>>(), size_of::<usize>());
110 }
111
112 assert_eq!(size_of::<Rc<i32>>(), size_of::<Option<BRc<i32>>>());
114 }
115}