summaryrefslogtreecommitdiffstats
path: root/third_party/rust/dashmap/src/util.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/dashmap/src/util.rs
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/dashmap/src/util.rs')
-rw-r--r--third_party/rust/dashmap/src/util.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/third_party/rust/dashmap/src/util.rs b/third_party/rust/dashmap/src/util.rs
new file mode 100644
index 0000000000..d84e37db9b
--- /dev/null
+++ b/third_party/rust/dashmap/src/util.rs
@@ -0,0 +1,102 @@
+//! This module is full of hackery and dark magic.
+//! Either spend a day fixing it and quietly submit a PR or don't mention it to anybody.
+use core::cell::UnsafeCell;
+use core::{mem, ptr};
+
+pub const fn ptr_size_bits() -> usize {
+ mem::size_of::<usize>() * 8
+}
+
+pub fn map_in_place_2<T, U, F: FnOnce(U, T) -> T>((k, v): (U, &mut T), f: F) {
+ unsafe {
+ // # Safety
+ //
+ // If the closure panics, we must abort otherwise we could double drop `T`
+ let _promote_panic_to_abort = AbortOnPanic;
+
+ ptr::write(v, f(k, ptr::read(v)));
+ }
+}
+
+/// # Safety
+///
+/// Requires that you ensure the reference does not become invalid.
+/// The object has to outlive the reference.
+pub unsafe fn change_lifetime_const<'a, 'b, T>(x: &'a T) -> &'b T {
+ &*(x as *const T)
+}
+
+/// # Safety
+///
+/// Requires that you ensure the reference does not become invalid.
+/// The object has to outlive the reference.
+pub unsafe fn change_lifetime_mut<'a, 'b, T>(x: &'a mut T) -> &'b mut T {
+ &mut *(x as *mut T)
+}
+
+/// A simple wrapper around `T`
+///
+/// This is to prevent UB when using `HashMap::get_key_value`, because
+/// `HashMap` doesn't expose an api to get the key and value, where
+/// the value is a `&mut T`.
+///
+/// See [#10](https://github.com/xacrimon/dashmap/issues/10) for details
+///
+/// This type is meant to be an implementation detail, but must be exposed due to the `Dashmap::shards`
+#[repr(transparent)]
+pub struct SharedValue<T> {
+ value: UnsafeCell<T>,
+}
+
+impl<T: Clone> Clone for SharedValue<T> {
+ fn clone(&self) -> Self {
+ let inner = self.get().clone();
+
+ Self {
+ value: UnsafeCell::new(inner),
+ }
+ }
+}
+
+unsafe impl<T: Send> Send for SharedValue<T> {}
+
+unsafe impl<T: Sync> Sync for SharedValue<T> {}
+
+impl<T> SharedValue<T> {
+ /// Create a new `SharedValue<T>`
+ pub const fn new(value: T) -> Self {
+ Self {
+ value: UnsafeCell::new(value),
+ }
+ }
+
+ /// Get a shared reference to `T`
+ pub fn get(&self) -> &T {
+ unsafe { &*self.value.get() }
+ }
+
+ /// Get an unique reference to `T`
+ pub fn get_mut(&mut self) -> &mut T {
+ unsafe { &mut *self.value.get() }
+ }
+
+ /// Unwraps the value
+ pub fn into_inner(self) -> T {
+ self.value.into_inner()
+ }
+
+ /// Get a mutable raw pointer to the underlying value
+ pub(crate) fn as_ptr(&self) -> *mut T {
+ self.value.get()
+ }
+}
+
+struct AbortOnPanic;
+
+impl Drop for AbortOnPanic {
+ fn drop(&mut self) {
+ if std::thread::panicking() {
+ std::process::abort()
+ }
+ }
+}