summaryrefslogtreecommitdiffstats
path: root/third_party/rust/foreign-types-shared/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/foreign-types-shared/src
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/foreign-types-shared/src')
-rw-r--r--third_party/rust/foreign-types-shared/src/lib.rs92
1 files changed, 92 insertions, 0 deletions
diff --git a/third_party/rust/foreign-types-shared/src/lib.rs b/third_party/rust/foreign-types-shared/src/lib.rs
new file mode 100644
index 0000000000..2f58b53f18
--- /dev/null
+++ b/third_party/rust/foreign-types-shared/src/lib.rs
@@ -0,0 +1,92 @@
+//! Internal crate used by foreign-types
+
+#![no_std]
+#![warn(missing_docs)]
+#![doc(html_root_url = "https://docs.rs/foreign-types-shared/0.3")]
+
+use core::cell::UnsafeCell;
+use core::marker::PhantomData;
+use core::mem;
+
+/// An opaque type used to define `ForeignTypeRef` types.
+///
+/// A type implementing `ForeignTypeRef` should simply be a newtype wrapper around this type.
+pub struct Opaque(PhantomData<UnsafeCell<*mut ()>>);
+
+/// A type implemented by wrappers over foreign types.
+///
+/// # Safety
+///
+/// Implementations of `ForeignType` must guarantee the following:
+/// - `Self::from_ptr(x).as_ptr() == x`
+/// - `Self::from_ptr(x).into_ptr(x) == x`
+/// - `Self::from_ptr(x).deref().as_ptr(x) == x`
+/// - `Self::from_ptr(x).deref_mut().as_ptr(x) == x`
+/// - `Self::from_ptr(x).as_ref().as_ptr(x) == x`
+/// - `Self::from_ptr(x).as_mut().as_ptr(x) == x`
+pub unsafe trait ForeignType: Sized {
+ /// The raw C type.
+ type CType;
+
+ /// The type representing a reference to this type.
+ type Ref: ForeignTypeRef<CType = Self::CType>;
+
+ /// Constructs an instance of this type from its raw type.
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must be a valid, owned instance of the native type.
+ unsafe fn from_ptr(ptr: *mut Self::CType) -> Self;
+
+ /// Returns a raw pointer to the wrapped value.
+ fn as_ptr(&self) -> *mut Self::CType;
+
+ /// Consumes the wrapper and returns the raw pointer.
+ #[inline]
+ fn into_ptr(self) -> *mut Self::CType {
+ let ptr = self.as_ptr();
+ mem::forget(self);
+ ptr
+ }
+}
+
+/// A trait implemented by types which reference borrowed foreign types.
+///
+/// # Safety
+///
+/// Implementations of `ForeignTypeRef` must guarantee the following:
+///
+/// - `Self::from_ptr(x).as_ptr() == x`
+/// - `Self::from_mut_ptr(x).as_ptr() == x`
+pub unsafe trait ForeignTypeRef: Sized {
+ /// The raw C type.
+ type CType;
+
+ /// Constructs a shared instance of this type from its raw type.
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must be a valid, immutable, instance of the type for the `'a` lifetime.
+ #[inline]
+ unsafe fn from_ptr<'a>(ptr: *mut Self::CType) -> &'a Self {
+ debug_assert!(!ptr.is_null());
+ &*(ptr as *mut _)
+ }
+
+ /// Constructs a mutable reference of this type from its raw type.
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must be a valid, unique, instance of the type for the `'a` lifetime.
+ #[inline]
+ unsafe fn from_ptr_mut<'a>(ptr: *mut Self::CType) -> &'a mut Self {
+ debug_assert!(!ptr.is_null());
+ &mut *(ptr as *mut _)
+ }
+
+ /// Returns a raw pointer to the wrapped value.
+ #[inline]
+ fn as_ptr(&self) -> *mut Self::CType {
+ self as *const _ as *mut _
+ }
+}