ostd/task/
utils.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
// SPDX-License-Identifier: MPL-2.0

use alloc::fmt;

use safety::safety;

/// Always [`Sync`], but unsafe to reference the data.
pub(super) struct ForceSync<T>(T);

impl<T> fmt::Debug for ForceSync<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ForceSync").finish_non_exhaustive()
    }
}

// SAFETY: The caller of the `ForceSync::get` method must ensure that the underlying data is not
// concurrently accessed if the underlying type is not `Sync`.
unsafe impl<T> Sync for ForceSync<T> {}

impl<T> ForceSync<T> {
    /// Creates an instance with `data` as the inner data.
    pub(super) fn new(data: T) -> Self {
        Self(data)
    }

    /// Returns a reference to the inner data.
    #[safety {
        Sync(data)
    }]
    pub(super) unsafe fn get(&self) -> &T {
        &self.0
    }
}