std_ext/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use core::sync::atomic::{AtomicBool, AtomicI64, AtomicIsize, AtomicU64, AtomicUsize};
use std::sync::Arc;

pub use map::{CacheMapExt, EntryExt, TimedValue};
pub use wrapper::{HashExt, OrdExt, OrdHashExt};

pub mod map;
pub mod wrapper;

#[macro_export]
macro_rules! tuple_deref {
    ($Name:ty) => {
        impl<T> std::ops::Deref for $Name {
            type Target = T;
            #[inline]
            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }
    };
}

#[macro_export]
macro_rules! tuple_deref_mut {
    ($Name:ty) => {
        impl<T> std::ops::DerefMut for $Name {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }
    };
}

impl<T: ?Sized> ArcExt for T {}

pub trait ArcExt {
    #[inline]
    fn arc(self) -> Arc<Self>
    where
        Self: Sized,
    {
        Arc::new(self)
    }
}

pub trait AtomicExt<T> {
    fn atomic(self) -> T;
}

impl AtomicExt<AtomicUsize> for usize {
    #[inline]
    fn atomic(self) -> AtomicUsize {
        AtomicUsize::new(self)
    }
}

impl AtomicExt<AtomicIsize> for isize {
    #[inline]
    fn atomic(self) -> AtomicIsize {
        AtomicIsize::new(self)
    }
}

impl AtomicExt<AtomicU64> for u64 {
    #[inline]
    fn atomic(self) -> AtomicU64 {
        AtomicU64::new(self)
    }
}

impl AtomicExt<AtomicI64> for i64 {
    #[inline]
    fn atomic(self) -> AtomicI64 {
        AtomicI64::new(self)
    }
}

impl AtomicExt<AtomicBool> for bool {
    #[inline]
    fn atomic(self) -> AtomicBool {
        AtomicBool::new(self)
    }
}

#[test]
fn test_atomic() {
    use std::sync::atomic::Ordering;

    assert_eq!(100usize.atomic().load(Ordering::SeqCst), 100);
    assert_eq!(100isize.atomic().load(Ordering::SeqCst), 100);
    assert_eq!(100u64.atomic().load(Ordering::SeqCst), 100);
    assert_eq!(100i64.atomic().load(Ordering::SeqCst), 100);
    assert!(true.atomic().load(Ordering::SeqCst))
}