1use 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 let promote_panic_to_abort = AbortOnPanic;
16
17 ptr::write(v, f(k, ptr::read(v)));
18
19 std::mem::forget(promote_panic_to_abort);
22 }
23}
24
25#[repr(transparent)]
35pub struct SharedValue<T> {
36 value: UnsafeCell<T>,
37}
38
39impl<T: Clone> Clone for SharedValue<T> {
40 fn clone(&self) -> Self {
41 let inner = self.get().clone();
42
43 Self {
44 value: UnsafeCell::new(inner),
45 }
46 }
47}
48
49unsafe impl<T: Send> Send for SharedValue<T> {}
50
51unsafe impl<T: Sync> Sync for SharedValue<T> {}
52
53impl<T> SharedValue<T> {
54 pub const fn new(value: T) -> Self {
56 Self {
57 value: UnsafeCell::new(value),
58 }
59 }
60
61 pub fn get(&self) -> &T {
63 unsafe { &*self.value.get() }
64 }
65
66 pub fn get_mut(&mut self) -> &mut T {
68 unsafe { &mut *self.value.get() }
69 }
70
71 pub fn into_inner(self) -> T {
73 self.value.into_inner()
74 }
75
76 pub(crate) fn as_ptr(&self) -> *mut T {
78 self.value.get()
79 }
80}
81
82struct AbortOnPanic;
83
84impl Drop for AbortOnPanic {
85 fn drop(&mut self) {
86 if std::thread::panicking() {
87 std::process::abort()
88 }
89 }
90}