ointer/
lib.rs

1//! This crate provides a set of traits and macros to enable the creation of custom pointers in Rust, allowing for the storage of extra information in the high bits of a pointer. This extra information can be of various types, and the crate provides utilities for working with these custom pointers efficiently. The crate also offers convenient macros for defining custom `ointer`s and `enum ointers` and managing them.
2
3mod ointer;
4pub use ointer::*;
5pub mod boxed;
6pub use boxed::*;
7pub mod rc;
8pub mod sync;
9
10/// Type alias for `boxed::OBox`
11pub type Ox<T> = OBox<T>;
12/// Type alias for `rc::ORc`
13pub type Oc<T> = rc::ORc<T>;
14/// Type alias for `rc::OWeak`
15pub type Ok<T> = rc::OWeak<T>;
16/// Type alias for `sync::OArc`
17pub type Orc<T> = sync::OArc<T>;
18/// Type alias for `sync::OWeak`
19pub type Oak<T> = sync::OWeak<T>;
20
21/// Test the Ointer Library
22#[cfg(test)]
23mod tests {
24    // Import the necessary modules and types.
25    use super::{rc::*, sync::*, *};
26    use std::{mem::size_of, pin::Pin, rc::Rc, sync::*};
27
28    // Define a test function.
29    #[test]
30    fn test() {
31        {
32            // Test custom ointers (OBox).
33            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            // Test custom strong ointers (BArc).
47            let mut o = BArc::new(1);
48            assert_eq!(*o, 1);
49            assert_eq!(o.get::<bool>(), false);
50
51            // Define a small enum for testing.
52            #[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            // Modify the bool and pointer inside the ointer.
65            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 custom enum ointers using MyEnumOinters.
75            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            // Perform operations on the enum ointer.
88            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            // Set the enum ointer to a new value (Box<f64>).
106            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        // Test size comparison of Rc<i32> and Option<BRc<i32>>.
113        assert_eq!(size_of::<Rc<i32>>(), size_of::<Option<BRc<i32>>>());
114    }
115}