async_compression/unshared.rs
1#![allow(dead_code)] // unused without any features
2
3use core::fmt::{self, Debug};
4
5/// Wraps a type and only allows unique borrowing, the main use case is to wrap a `!Sync` type and
6/// implement `Sync` for it as this type blocks having multiple shared references to the inner
7/// value.
8///
9/// # Safety
10///
11/// We must be careful when accessing `inner`, there must be no way to create a shared reference to
12/// it from a shared reference to an `Unshared`, as that would allow creating shared references on
13/// multiple threads.
14///
15/// As an example deriving or implementing `Clone` is impossible, two threads could attempt to
16/// clone a shared `Unshared<T>` reference which would result in accessing the same inner value
17/// concurrently.
18pub struct Unshared<T> {
19 inner: T,
20}
21
22impl<T> Unshared<T> {
23 pub fn new(inner: T) -> Self {
24 Unshared { inner }
25 }
26
27 pub fn get_mut(&mut self) -> &mut T {
28 &mut self.inner
29 }
30}
31
32/// Safety: See comments on main docs for `Unshared`
33unsafe impl<T> Sync for Unshared<T> {}
34
35impl<T> Debug for Unshared<T> {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 f.debug_struct(core::any::type_name::<T>()).finish()
38 }
39}