hydra_dashmap/
util.rs

1//! This module is full of hackery and dark magic.
2//! Either spend a day fixing it and quietly submit a PR or don't mention it to anybody.
3use core::cell::UnsafeCell;
4use core::{mem, ptr};
5
6pub const fn ptr_size_bits() -> usize {
7    mem::size_of::<usize>() * 8
8}
9
10pub fn map_in_place_2<T, U, F: FnOnce(U, T) -> T>((k, v): (U, &mut T), f: F) {
11    unsafe {
12        // # Safety
13        //
14        // If the closure panics, we must abort otherwise we could double drop `T`
15        let promote_panic_to_abort = AbortOnPanic;
16
17        ptr::write(v, f(k, ptr::read(v)));
18
19        // If we made it here, the calling thread could have already have panicked, in which case
20        // We know that the closure did not panic, so don't bother checking.
21        std::mem::forget(promote_panic_to_abort);
22    }
23}
24
25/// # Safety
26///
27/// Requires that you ensure the reference does not become invalid.
28/// The object has to outlive the reference.
29pub unsafe fn change_lifetime_const<'b, T>(x: &T) -> &'b T {
30    &*(x as *const T)
31}
32
33/// # Safety
34///
35/// Requires that you ensure the reference does not become invalid.
36/// The object has to outlive the reference.
37pub unsafe fn change_lifetime_mut<'b, T>(x: &mut T) -> &'b mut T {
38    &mut *(x as *mut T)
39}
40
41/// A simple wrapper around `T`
42///
43/// This is to prevent UB when using `HashMap::get_key_value`, because
44/// `HashMap` doesn't expose an api to get the key and value, where
45/// the value is a `&mut T`.
46///
47/// See [#10](https://github.com/xacrimon/dashmap/issues/10) for details
48///
49/// This type is meant to be an implementation detail, but must be exposed due to the `Dashmap::shards`
50#[repr(transparent)]
51pub struct SharedValue<T> {
52    value: UnsafeCell<T>,
53}
54
55impl<T: Clone> Clone for SharedValue<T> {
56    fn clone(&self) -> Self {
57        let inner = self.get().clone();
58
59        Self {
60            value: UnsafeCell::new(inner),
61        }
62    }
63}
64
65unsafe impl<T: Send> Send for SharedValue<T> {}
66
67unsafe impl<T: Sync> Sync for SharedValue<T> {}
68
69impl<T> SharedValue<T> {
70    /// Create a new `SharedValue<T>`
71    pub const fn new(value: T) -> Self {
72        Self {
73            value: UnsafeCell::new(value),
74        }
75    }
76
77    /// Get a shared reference to `T`
78    pub fn get(&self) -> &T {
79        unsafe { &*self.value.get() }
80    }
81
82    /// Get an unique reference to `T`
83    pub fn get_mut(&mut self) -> &mut T {
84        unsafe { &mut *self.value.get() }
85    }
86
87    /// Unwraps the value
88    pub fn into_inner(self) -> T {
89        self.value.into_inner()
90    }
91
92    /// Get a mutable raw pointer to the underlying value
93    pub(crate) fn as_ptr(&self) -> *mut T {
94        self.value.get()
95    }
96}
97
98struct AbortOnPanic;
99
100impl Drop for AbortOnPanic {
101    fn drop(&mut self) {
102        if std::thread::panicking() {
103            std::process::abort()
104        }
105    }
106}