1 | pub(crate) struct SyncWrapper<T>(T); |
2 | |
3 | impl<T> SyncWrapper<T> { |
4 | /// Creates a new SyncWrapper containing the given value. |
5 | /// |
6 | /// # Examples |
7 | /// |
8 | /// ```ignore |
9 | /// use hyper::common::sync_wrapper::SyncWrapper; |
10 | /// |
11 | /// let wrapped = SyncWrapper::new(42); |
12 | /// ``` |
13 | pub(crate) fn new(value: T) -> Self { |
14 | Self(value) |
15 | } |
16 | |
17 | /// Acquires a reference to the protected value. |
18 | /// |
19 | /// This is safe because it requires an exclusive reference to the wrapper. Therefore this method |
20 | /// neither panics nor does it return an error. This is in contrast to [`Mutex::get_mut`] which |
21 | /// returns an error if another thread panicked while holding the lock. It is not recommended |
22 | /// to send an exclusive reference to a potentially damaged value to another thread for further |
23 | /// processing. |
24 | /// |
25 | /// [`Mutex::get_mut`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut |
26 | /// |
27 | /// # Examples |
28 | /// |
29 | /// ```ignore |
30 | /// use hyper::common::sync_wrapper::SyncWrapper; |
31 | /// |
32 | /// let mut wrapped = SyncWrapper::new(42); |
33 | /// let value = wrapped.get_mut(); |
34 | /// *value = 0; |
35 | /// assert_eq!(*wrapped.get_mut(), 0); |
36 | /// ``` |
37 | pub(crate) fn get_mut(&mut self) -> &mut T { |
38 | &mut self.0 |
39 | } |
40 | |
41 | /// Consumes this wrapper, returning the underlying data. |
42 | /// |
43 | /// This is safe because it requires ownership of the wrapper, aherefore this method will neither |
44 | /// panic nor does it return an error. This is in contrast to [`Mutex::into_inner`] which |
45 | /// returns an error if another thread panicked while holding the lock. It is not recommended |
46 | /// to send an exclusive reference to a potentially damaged value to another thread for further |
47 | /// processing. |
48 | /// |
49 | /// [`Mutex::into_inner`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.into_inner |
50 | /// |
51 | /// # Examples |
52 | /// |
53 | /// ```ignore |
54 | /// use hyper::common::sync_wrapper::SyncWrapper; |
55 | /// |
56 | /// let mut wrapped = SyncWrapper::new(42); |
57 | /// assert_eq!(wrapped.into_inner(), 42); |
58 | /// ``` |
59 | #[allow (dead_code)] |
60 | pub(crate) fn into_inner(self) -> T { |
61 | self.0 |
62 | } |
63 | } |
64 | |
65 | // this is safe because the only operations permitted on this data structure require exclusive |
66 | // access or ownership |
67 | unsafe impl<T: Send> Sync for SyncWrapper<T> {} |
68 | |