1#![no_std]
22#![deny(clippy::correctness)]
23#![warn(
24 clippy::perf,
25 clippy::complexity,
26 clippy::style,
27 clippy::nursery,
28 clippy::pedantic,
29 clippy::clone_on_ref_ptr,
30 clippy::decimal_literal_representation,
31 clippy::float_cmp_const,
32 clippy::missing_docs_in_private_items,
33 clippy::multiple_inherent_impl,
34 clippy::unwrap_used,
35 clippy::cargo_common_metadata,
36 clippy::used_underscore_binding
37)]
38#![allow(clippy::inline_always)]
39
40use core::fmt::{Formatter, Pointer};
41use core::ops::Deref;
42
43macro_rules! trait_impl {
60 ($SelfType:ident) => {
61 impl<T> Clone for $SelfType<T> {
62 #[inline(always)]
63 fn clone(&self) -> Self {
64 *self
65 }
66 }
67
68 impl<T> Copy for $SelfType<T> {}
69 impl<T> Pointer for $SelfType<T> {
70 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
71 core::fmt::Pointer::fmt(&self.0, f)
72 }
73 }
74
75 impl<T> Eq for $SelfType<T> {}
76 impl<T> PartialEq for $SelfType<T> {
77 #[inline(always)]
78 fn eq(&self, other: &Self) -> bool {
79 PartialEq::eq(&self.0, &other.0)
80 }
81 }
82
83 impl<T> PartialOrd for $SelfType<T> {
84 #[inline(always)]
85 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
86 Some(self.cmp(other))
87 }
88 }
89
90 impl<T> Ord for $SelfType<T> {
91 #[inline(always)]
92 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
93 Ord::cmp(&self.0, &other.0)
94 }
95 }
96
97 impl<T> core::fmt::Debug for $SelfType<T> {
98 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
99 f.debug_tuple(stringify!($SelfType)).field(&self.0).finish()
100 }
101 }
102
103 impl<T> core::hash::Hash for $SelfType<T> {
104 #[inline(always)]
105 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
106 core::hash::Hash::hash(&self.0, state);
107 }
108 }
109
110 impl<T> From<$SelfType<T>> for usize {
111 #[inline(always)]
112 fn from(val: $SelfType<T>) -> Self {
113 val.as_address()
114 }
115 }
116
117 impl<T> From<usize> for $SelfType<T> {
118 #[inline(always)]
119 fn from(value: usize) -> Self {
120 Self::from_address(value)
121 }
122 }
123 };
124}
125
126#[repr(transparent)]
130pub struct SyncMutPtr<T>(*mut T);
131
132unsafe impl<T> Sync for SyncMutPtr<T> {}
133unsafe impl<T> Send for SyncMutPtr<T> {}
134
135trait_impl!(SyncMutPtr);
136impl<T> Deref for SyncMutPtr<T> {
137 type Target = *mut T;
138
139 #[inline(always)]
140 fn deref(&self) -> &Self::Target {
141 &self.0
142 }
143}
144
145impl<T> From<*mut T> for SyncMutPtr<T> {
146 #[inline(always)]
147 fn from(value: *mut T) -> Self {
148 Self(value)
149 }
150}
151
152impl<T> From<SyncMutPtr<T>> for *mut T {
153 #[inline(always)]
154 fn from(val: SyncMutPtr<T>) -> Self {
155 val.inner()
156 }
157}
158
159impl<T> From<SyncMutPtr<T>> for *const T {
160 #[inline(always)]
161 fn from(val: SyncMutPtr<T>) -> Self {
162 val.inner()
163 }
164}
165
166impl<T> SyncMutPtr<T> {
167 #[inline(always)]
179 #[must_use]
180 pub const fn new(ptr: *mut T) -> Self {
181 Self(ptr)
182 }
183
184 #[inline(always)]
189 #[must_use]
190 pub const fn from_address(addr: usize) -> Self {
191 Self(addr as *mut T)
192 }
193
194 #[inline(always)]
206 #[must_use]
207 pub fn as_address(&self) -> usize {
208 self.0 as usize
209 }
210
211 #[inline(always)]
215 #[must_use]
216 pub const fn null() -> Self {
217 Self(core::ptr::null_mut())
218 }
219
220 #[inline(always)]
224 #[must_use]
225 pub const fn cast<Y>(&self) -> SyncMutPtr<Y> {
226 SyncMutPtr(self.0.cast())
227 }
228
229 #[inline(always)]
233 #[must_use]
234 pub const fn inner(&self) -> *mut T {
235 self.0
236 }
237
238 #[inline(always)]
242 #[must_use]
243 pub const fn as_sync_const(&self) -> SyncConstPtr<T> {
244 SyncConstPtr(self.0)
245 }
246
247 #[inline(always)]
251 #[must_use]
252 pub const fn as_send_const(&self) -> SendConstPtr<T> {
253 SendConstPtr(self.0)
254 }
255
256 #[inline(always)]
260 #[must_use]
261 pub const fn as_sync_mut(&self) -> Self {
262 Self(self.0)
263 }
264
265 #[inline(always)]
269 #[must_use]
270 pub const fn as_send_mut(&self) -> SendMutPtr<T> {
271 SendMutPtr(self.0)
272 }
273}
274
275#[repr(transparent)]
279pub struct SyncConstPtr<T>(*const T);
280
281unsafe impl<T> Sync for SyncConstPtr<T> {}
282unsafe impl<T> Send for SyncConstPtr<T> {}
283
284trait_impl!(SyncConstPtr);
285
286impl<T> Deref for SyncConstPtr<T> {
287 type Target = *const T;
288
289 #[inline(always)]
290 fn deref(&self) -> &Self::Target {
291 &self.0
292 }
293}
294
295impl<T> From<*mut T> for SyncConstPtr<T> {
296 #[inline(always)]
297 fn from(value: *mut T) -> Self {
298 Self(value)
299 }
300}
301
302impl<T> From<*const T> for SyncConstPtr<T> {
303 #[inline(always)]
304 fn from(value: *const T) -> Self {
305 Self(value)
306 }
307}
308
309impl<T> From<SyncConstPtr<T>> for *const T {
310 #[inline(always)]
311 fn from(val: SyncConstPtr<T>) -> Self {
312 val.inner()
313 }
314}
315
316impl<T> SyncConstPtr<T> {
317 #[inline(always)]
321 #[must_use]
322 pub const fn new(ptr: *const T) -> Self {
323 Self(ptr)
324 }
325
326 #[inline(always)]
331 #[must_use]
332 pub const fn from_address(addr: usize) -> Self {
333 Self(addr as *mut T)
334 }
335
336 #[inline(always)]
348 #[must_use]
349 pub fn as_address(&self) -> usize {
350 self.0 as usize
351 }
352
353 #[inline(always)]
357 #[must_use]
358 pub const fn null() -> Self {
359 Self(core::ptr::null())
360 }
361
362 #[inline(always)]
366 #[must_use]
367 pub const fn cast<Y>(&self) -> SyncConstPtr<Y> {
368 SyncConstPtr(self.0.cast())
369 }
370
371 #[inline(always)]
375 #[must_use]
376 pub const fn inner(&self) -> *const T {
377 self.0
378 }
379
380 #[inline(always)]
384 #[must_use]
385 pub const fn as_sync_const(&self) -> Self {
386 Self(self.0)
387 }
388
389 #[inline(always)]
393 #[must_use]
394 pub const fn as_send_const(&self) -> SendConstPtr<T> {
395 SendConstPtr(self.0)
396 }
397
398 #[inline(always)]
405 #[must_use]
406 pub const fn as_sync_mut(&self) -> SyncMutPtr<T> {
407 SyncMutPtr(self.0.cast_mut())
408 }
409
410 #[inline(always)]
417 #[must_use]
418 pub const fn as_send_mut(&self) -> SendMutPtr<T> {
419 SendMutPtr(self.0.cast_mut())
420 }
421}
422
423#[repr(transparent)]
427pub struct SendMutPtr<T>(*mut T);
428
429unsafe impl<T> Send for SendMutPtr<T> {}
430
431trait_impl!(SendMutPtr);
432
433impl<T> Deref for SendMutPtr<T> {
434 type Target = *mut T;
435
436 #[inline(always)]
437 fn deref(&self) -> &Self::Target {
438 &self.0
439 }
440}
441
442impl<T> From<SendMutPtr<T>> for *mut T {
443 #[inline(always)]
444 fn from(val: SendMutPtr<T>) -> Self {
445 val.inner()
446 }
447}
448
449impl<T> From<SendMutPtr<T>> for *const T {
450 #[inline(always)]
451 fn from(val: SendMutPtr<T>) -> Self {
452 val.inner()
453 }
454}
455
456impl<T> SendMutPtr<T> {
457 #[inline(always)]
461 #[must_use]
462 pub const fn new(ptr: *mut T) -> Self {
463 Self(ptr)
464 }
465
466 #[inline(always)]
471 #[must_use]
472 pub const fn from_address(addr: usize) -> Self {
473 Self(addr as *mut T)
474 }
475
476 #[inline(always)]
488 #[must_use]
489 pub fn as_address(&self) -> usize {
490 self.0 as usize
491 }
492
493 #[inline(always)]
497 #[must_use]
498 pub const fn null() -> Self {
499 Self(core::ptr::null_mut())
500 }
501
502 #[inline(always)]
506 #[must_use]
507 pub const fn cast<Y>(&self) -> SendMutPtr<Y> {
508 SendMutPtr(self.0.cast())
509 }
510
511 #[inline(always)]
515 #[must_use]
516 pub const fn inner(&self) -> *mut T {
517 self.0
518 }
519
520 #[inline(always)]
524 #[must_use]
525 pub const fn as_sync_const(&self) -> SyncConstPtr<T> {
526 SyncConstPtr(self.0)
527 }
528
529 #[inline(always)]
533 #[must_use]
534 pub const fn as_send_const(&self) -> SendConstPtr<T> {
535 SendConstPtr(self.0)
536 }
537
538 #[inline(always)]
542 #[must_use]
543 pub const fn as_sync_mut(&self) -> SyncMutPtr<T> {
544 SyncMutPtr(self.0)
545 }
546
547 #[inline(always)]
551 #[must_use]
552 pub const fn as_send_mut(&self) -> Self {
553 Self(self.0)
554 }
555}
556
557#[repr(transparent)]
561pub struct SendConstPtr<T>(*const T);
562
563unsafe impl<T> Send for SendConstPtr<T> {}
564
565trait_impl!(SendConstPtr);
566
567impl<T> Deref for SendConstPtr<T> {
568 type Target = *const T;
569
570 #[inline(always)]
571 fn deref(&self) -> &Self::Target {
572 &self.0
573 }
574}
575
576impl<T> From<SendConstPtr<T>> for *const T {
577 #[inline(always)]
578 fn from(val: SendConstPtr<T>) -> *const T {
579 val.inner()
580 }
581}
582
583impl<T> SendConstPtr<T> {
584 #[inline(always)]
588 #[must_use]
589 pub const fn new(ptr: *const T) -> Self {
590 Self(ptr)
591 }
592
593 #[inline(always)]
598 #[must_use]
599 pub const fn from_address(addr: usize) -> Self {
600 Self(addr as *mut T)
601 }
602
603 #[inline(always)]
616 #[must_use]
617 pub fn as_address(&self) -> usize {
618 self.0 as usize
619 }
620
621 #[inline(always)]
625 #[must_use]
626 pub const fn null() -> Self {
627 Self(core::ptr::null())
628 }
629
630 #[inline(always)]
634 #[must_use]
635 pub const fn cast<Y>(&self) -> SendConstPtr<Y> {
636 SendConstPtr(self.0.cast())
637 }
638
639 #[inline(always)]
643 #[must_use]
644 pub const fn inner(&self) -> *const T {
645 self.0
646 }
647
648 #[inline(always)]
652 #[must_use]
653 pub const fn as_sync_const(&self) -> SyncConstPtr<T> {
654 SyncConstPtr(self.0)
655 }
656
657 #[inline(always)]
661 #[must_use]
662 pub const fn as_send_const(&self) -> Self {
663 Self(self.0)
664 }
665
666 #[inline(always)]
673 #[must_use]
674 pub const fn as_sync_mut(&self) -> SyncMutPtr<T> {
675 SyncMutPtr(self.0.cast_mut())
676 }
677
678 #[inline(always)]
685 #[must_use]
686 pub const fn as_send_mut(&self) -> SendMutPtr<T> {
687 SendMutPtr(self.0.cast_mut())
688 }
689}
690
691pub trait FromConstPtr<T>: Sized {
695 fn as_sync_const(&self) -> SyncConstPtr<T>;
699
700 fn as_send_const(&self) -> SendConstPtr<T>;
704}
705
706pub trait FromMutPtr<T>: FromConstPtr<T> {
710 fn as_sync_mut(&self) -> SyncMutPtr<T>;
714
715 fn as_send_mut(&self) -> SendMutPtr<T>;
719}
720
721impl<T> FromConstPtr<T> for *const T {
722 #[inline(always)]
723 fn as_sync_const(&self) -> SyncConstPtr<T> {
724 SyncConstPtr(self.cast())
725 }
726
727 #[inline(always)]
728 fn as_send_const(&self) -> SendConstPtr<T> {
729 SendConstPtr(self.cast())
730 }
731}
732
733impl<T> FromConstPtr<T> for *mut T {
734 #[inline(always)]
735 fn as_sync_const(&self) -> SyncConstPtr<T> {
736 SyncConstPtr(self.cast())
737 }
738
739 #[inline(always)]
740 fn as_send_const(&self) -> SendConstPtr<T> {
741 SendConstPtr(self.cast())
742 }
743}
744
745impl<T> FromMutPtr<T> for *mut T {
746 #[inline(always)]
747 fn as_sync_mut(&self) -> SyncMutPtr<T> {
748 SyncMutPtr(self.cast())
749 }
750
751 #[inline(always)]
752 fn as_send_mut(&self) -> SendMutPtr<T> {
753 SendMutPtr(self.cast())
754 }
755}