wasmtime/runtime/vm/
send_sync_ptr.rs1use core::fmt;
2use core::hash::{Hash, Hasher};
3use core::ptr::{self, NonNull};
4
5#[repr(transparent)]
9pub struct SendSyncPtr<T: ?Sized>(NonNull<T>);
10
11unsafe impl<T: Send + ?Sized> Send for SendSyncPtr<T> {}
12unsafe impl<T: Sync + ?Sized> Sync for SendSyncPtr<T> {}
13
14impl<T: ?Sized> SendSyncPtr<T> {
15 #[inline]
17 pub fn new(ptr: NonNull<T>) -> SendSyncPtr<T> {
18 SendSyncPtr(ptr)
19 }
20
21 #[inline]
23 pub fn as_ptr(&self) -> *mut T {
24 self.0.as_ptr()
25 }
26
27 #[inline]
30 pub unsafe fn as_ref<'a>(&self) -> &'a T {
31 self.0.as_ref()
32 }
33
34 #[inline]
37 pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
38 self.0.as_mut()
39 }
40
41 #[inline]
43 pub fn as_non_null(&self) -> NonNull<T> {
44 self.0
45 }
46
47 #[inline]
49 pub fn cast<U>(&self) -> SendSyncPtr<U> {
50 SendSyncPtr(self.0.cast::<U>())
51 }
52}
53
54impl<T> SendSyncPtr<[T]> {
55 #[inline]
57 pub fn len(&self) -> usize {
58 self.0.len()
59 }
60}
61
62impl<T: ?Sized, U> From<U> for SendSyncPtr<T>
63where
64 U: Into<NonNull<T>>,
65{
66 #[inline]
67 fn from(ptr: U) -> SendSyncPtr<T> {
68 SendSyncPtr::new(ptr.into())
69 }
70}
71
72impl<T: ?Sized> fmt::Debug for SendSyncPtr<T> {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 self.as_ptr().fmt(f)
75 }
76}
77
78impl<T: ?Sized> fmt::Pointer for SendSyncPtr<T> {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 self.as_ptr().fmt(f)
81 }
82}
83
84impl<T: ?Sized> Clone for SendSyncPtr<T> {
85 #[inline]
86 fn clone(&self) -> Self {
87 *self
88 }
89}
90
91impl<T: ?Sized> Copy for SendSyncPtr<T> {}
92
93impl<T: ?Sized> PartialEq for SendSyncPtr<T> {
94 #[inline]
95 fn eq(&self, other: &SendSyncPtr<T>) -> bool {
96 ptr::eq(self.0.as_ptr(), other.0.as_ptr())
97 }
98}
99
100impl<T: ?Sized> Eq for SendSyncPtr<T> {}
101
102impl<T: ?Sized> Hash for SendSyncPtr<T> {
103 fn hash<H: Hasher>(&self, state: &mut H) {
104 self.as_ptr().hash(state);
105 }
106}