std_ext/
lib.rs

1use core::sync::atomic::{AtomicBool, AtomicI64, AtomicIsize, AtomicU64, AtomicUsize};
2use std::sync::Arc;
3
4pub use map::{CacheMapExt, EntryExt, TimedValue};
5pub use wrapper::{HashExt, OrdExt, OrdHashExt};
6
7pub mod map;
8pub mod wrapper;
9
10#[macro_export]
11macro_rules! tuple_deref {
12    ($Name:ty) => {
13        impl<T> std::ops::Deref for $Name {
14            type Target = T;
15            #[inline]
16            fn deref(&self) -> &Self::Target {
17                &self.0
18            }
19        }
20    };
21}
22
23#[macro_export]
24macro_rules! tuple_deref_mut {
25    ($Name:ty) => {
26        impl<T> std::ops::DerefMut for $Name {
27            fn deref_mut(&mut self) -> &mut Self::Target {
28                &mut self.0
29            }
30        }
31    };
32}
33
34#[macro_export]
35macro_rules! tuple_take {
36    ($Name:ty) => {
37        impl<T> $Name {
38            #[inline]
39            pub fn take(self) -> T {
40                self.0
41            }
42        }
43    };
44}
45
46impl<T: ?Sized> ArcExt for T {}
47
48pub trait ArcExt {
49    #[inline]
50    fn arc(self) -> Arc<Self>
51    where
52        Self: Sized,
53    {
54        Arc::new(self)
55    }
56}
57
58pub trait AtomicExt<T> {
59    fn atomic(self) -> T;
60}
61
62impl AtomicExt<AtomicUsize> for usize {
63    #[inline]
64    fn atomic(self) -> AtomicUsize {
65        AtomicUsize::new(self)
66    }
67}
68
69impl AtomicExt<AtomicIsize> for isize {
70    #[inline]
71    fn atomic(self) -> AtomicIsize {
72        AtomicIsize::new(self)
73    }
74}
75
76impl AtomicExt<AtomicU64> for u64 {
77    #[inline]
78    fn atomic(self) -> AtomicU64 {
79        AtomicU64::new(self)
80    }
81}
82
83impl AtomicExt<AtomicI64> for i64 {
84    #[inline]
85    fn atomic(self) -> AtomicI64 {
86        AtomicI64::new(self)
87    }
88}
89
90impl AtomicExt<AtomicBool> for bool {
91    #[inline]
92    fn atomic(self) -> AtomicBool {
93        AtomicBool::new(self)
94    }
95}
96
97#[test]
98fn test_atomic() {
99    use std::sync::atomic::Ordering;
100
101    assert_eq!(100usize.atomic().load(Ordering::SeqCst), 100);
102    assert_eq!(100isize.atomic().load(Ordering::SeqCst), 100);
103    assert_eq!(100u64.atomic().load(Ordering::SeqCst), 100);
104    assert_eq!(100i64.atomic().load(Ordering::SeqCst), 100);
105    assert!(true.atomic().load(Ordering::SeqCst))
106}