/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-util-0.1.10/src/common/sync.rs
Line | Count | Source |
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 | 0 | pub(crate) fn new(value: T) -> Self { |
14 | 0 | Self(value) |
15 | 0 | } |
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 | 0 | pub(crate) fn get_mut(&mut self) -> &mut T { |
38 | 0 | &mut self.0 |
39 | 0 | } |
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 | 0 | pub(crate) fn into_inner(self) -> T { |
61 | 0 | self.0 |
62 | 0 | } |
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> {} |