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
34impl<T: ?Sized> ArcExt for T {}
35
36pub trait ArcExt {
37    #[inline]
38    fn arc(self) -> Arc<Self>
39    where
40        Self: Sized,
41    {
42        Arc::new(self)
43    }
44}
45
46pub trait AtomicExt<T> {
47    fn atomic(self) -> T;
48}
49
50impl AtomicExt<AtomicUsize> for usize {
51    #[inline]
52    fn atomic(self) -> AtomicUsize {
53        AtomicUsize::new(self)
54    }
55}
56
57impl AtomicExt<AtomicIsize> for isize {
58    #[inline]
59    fn atomic(self) -> AtomicIsize {
60        AtomicIsize::new(self)
61    }
62}
63
64impl AtomicExt<AtomicU64> for u64 {
65    #[inline]
66    fn atomic(self) -> AtomicU64 {
67        AtomicU64::new(self)
68    }
69}
70
71impl AtomicExt<AtomicI64> for i64 {
72    #[inline]
73    fn atomic(self) -> AtomicI64 {
74        AtomicI64::new(self)
75    }
76}
77
78impl AtomicExt<AtomicBool> for bool {
79    #[inline]
80    fn atomic(self) -> AtomicBool {
81        AtomicBool::new(self)
82    }
83}
84
85#[test]
86fn test_atomic() {
87    use std::sync::atomic::Ordering;
88
89    assert_eq!(100usize.atomic().load(Ordering::SeqCst), 100);
90    assert_eq!(100isize.atomic().load(Ordering::SeqCst), 100);
91    assert_eq!(100u64.atomic().load(Ordering::SeqCst), 100);
92    assert_eq!(100i64.atomic().load(Ordering::SeqCst), 100);
93    assert!(true.atomic().load(Ordering::SeqCst))
94}