1 use super::{Guard, RefCnt};
2 
3 mod sealed {
4     pub trait Sealed {}
5 }
6 
7 use self::sealed::Sealed;
8 
9 /// A trait describing things that can be turned into a raw pointer.
10 ///
11 /// This is just an abstraction of things that can be passed to the
12 /// [`compare_and_swap`](struct.ArcSwapAny.html#method.compare_and_swap).
13 ///
14 /// # Examples
15 ///
16 /// ```
17 /// use std::ptr;
18 /// use std::sync::Arc;
19 ///
20 /// use arc_swap::ArcSwapOption;
21 ///
22 /// let a = Arc::new(42);
23 /// let shared = ArcSwapOption::from(Some(Arc::clone(&a)));
24 ///
25 /// shared.compare_and_swap(&a, Some(Arc::clone(&a)));
26 /// shared.compare_and_swap(&None::<Arc<_>>, Some(Arc::clone(&a)));
27 /// shared.compare_and_swap(shared.load(), Some(Arc::clone(&a)));
28 /// shared.compare_and_swap(&shared.load(), Some(Arc::clone(&a)));
29 /// shared.compare_and_swap(ptr::null(), Some(Arc::clone(&a)));
30 /// ```
31 pub trait AsRaw<T>: Sealed {
32     /// Converts the value into a raw pointer.
as_raw(&self) -> *mut T33     fn as_raw(&self) -> *mut T;
34 }
35 
36 impl<'a, T: RefCnt> Sealed for &'a T {}
37 impl<'a, T: RefCnt> AsRaw<T::Base> for &'a T {
as_raw(&self) -> *mut T::Base38     fn as_raw(&self) -> *mut T::Base {
39         T::as_ptr(self)
40     }
41 }
42 
43 impl<'a, T: RefCnt> Sealed for &'a Guard<T> {}
44 impl<'a, T: RefCnt> AsRaw<T::Base> for &'a Guard<T> {
as_raw(&self) -> *mut T::Base45     fn as_raw(&self) -> *mut T::Base {
46         T::as_ptr(&self)
47     }
48 }
49 
50 impl<'a, T: RefCnt> Sealed for Guard<T> {}
51 impl<'a, T: RefCnt> AsRaw<T::Base> for Guard<T> {
as_raw(&self) -> *mut T::Base52     fn as_raw(&self) -> *mut T::Base {
53         T::as_ptr(&self)
54     }
55 }
56 
57 impl<T> Sealed for *mut T {}
58 impl<T> AsRaw<T> for *mut T {
as_raw(&self) -> *mut T59     fn as_raw(&self) -> *mut T {
60         *self
61     }
62 }
63 
64 impl<T> Sealed for *const T {}
65 impl<T> AsRaw<T> for *const T {
as_raw(&self) -> *mut T66     fn as_raw(&self) -> *mut T {
67         *self as *mut T
68     }
69 }
70