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}
20
21pub unsafe fn change_lifetime_const<'a, 'b, T>(x: &'a T) -> &'b T {
26 &*(x as *const T)
27}
28
29pub unsafe fn change_lifetime_mut<'a, 'b, T>(x: &'a mut T) -> &'b mut T {
34 &mut *(x as *mut T)
35}
36
37#[repr(transparent)]
47pub struct SharedValue<T> {
48 value: UnsafeCell<T>,
49}
50
51impl<T: Clone> Clone for SharedValue<T> {
52 fn clone(&self) -> Self {
53 let inner = self.get().clone();
54
55 Self {
56 value: UnsafeCell::new(inner),
57 }
58 }
59}
60
61unsafe impl<T: Send> Send for SharedValue<T> {}
62
63unsafe impl<T: Sync> Sync for SharedValue<T> {}
64
65impl<T> SharedValue<T> {
66 pub const fn new(value: T) -> Self {
68 Self {
69 value: UnsafeCell::new(value),
70 }
71 }
72
73 pub fn get(&self) -> &T {
75 unsafe { &*self.value.get() }
76 }
77
78 pub fn get_mut(&mut self) -> &mut T {
80 unsafe { &mut *self.value.get() }
81 }
82
83 pub fn into_inner(self) -> T {
85 self.value.into_inner()
86 }
87
88 pub(crate) fn as_ptr(&self) -> *mut T {
90 self.value.get()
91 }
92}
93
94struct AbortOnPanic;
95
96impl Drop for AbortOnPanic {
97 fn drop(&mut self) {
98 if std::thread::panicking() {
99 std::process::abort()
100 }
101 }
102}