1#![no_std]
8#![deny(clippy::correctness)]
9#![warn(
10 clippy::perf,
11 clippy::complexity,
12 clippy::style,
13 clippy::nursery,
14 clippy::pedantic,
15 clippy::clone_on_ref_ptr,
16 clippy::decimal_literal_representation,
17 clippy::float_cmp_const,
18 clippy::missing_docs_in_private_items,
19 clippy::multiple_inherent_impl,
20 clippy::unwrap_used,
21 clippy::cargo_common_metadata,
22 clippy::used_underscore_binding
23)]
24#![allow(clippy::inline_always)]
25extern crate alloc;
26
27use core::fmt::{Formatter, Pointer};
28use core::ops::Deref;
29
30#[repr(transparent)]
34#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
35pub struct SyncMutPtr<T>(*mut T);
36
37unsafe impl<T> Sync for SyncMutPtr<T> {}
38unsafe impl<T> Send for SyncMutPtr<T> {}
39
40impl<T> Clone for SyncMutPtr<T> {
41 #[inline(always)]
42 fn clone(&self) -> Self {
43 *self
44 }
45}
46
47impl<T> Copy for SyncMutPtr<T> {}
48impl<T> Pointer for SyncMutPtr<T> {
49 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
50 core::fmt::Pointer::fmt(&self.0, f)
51 }
52}
53
54impl<T> SyncMutPtr<T> {
55 #[inline(always)]
64 #[must_use]
65 pub const unsafe fn new(ptr: *mut T) -> Self {
66 Self(ptr)
67 }
68
69 #[inline(always)]
73 #[must_use]
74 pub const fn null() -> Self {
75 Self(core::ptr::null_mut())
76 }
77
78 #[inline(always)]
82 #[must_use]
83 pub const fn cast<Y>(&self) -> SyncMutPtr<Y> {
84 SyncMutPtr(self.0.cast())
85 }
86
87 #[inline(always)]
91 #[must_use]
92 pub const fn inner(&self) -> *mut T {
93 self.0
94 }
95
96 #[inline(always)]
100 #[must_use]
101 pub const fn as_sync_const(&self) -> SyncConstPtr<T> {
102 SyncConstPtr(self.0)
103 }
104
105 #[inline(always)]
109 #[must_use]
110 pub const fn as_send_const(&self) -> SendConstPtr<T> {
111 SendConstPtr(self.0)
112 }
113
114 #[inline(always)]
118 #[must_use]
119 pub const fn as_sync_mut(&self) -> Self {
120 Self(self.0)
121 }
122
123 #[inline(always)]
127 #[must_use]
128 pub const fn as_send_mut(&self) -> SendMutPtr<T> {
129 SendMutPtr(self.0)
130 }
131}
132
133impl<T> Deref for SyncMutPtr<T> {
134 type Target = *mut T;
135
136 #[inline(always)]
137 fn deref(&self) -> &Self::Target {
138 &self.0
139 }
140}
141
142impl<T> From<SyncMutPtr<T>> for *mut T {
143 #[inline(always)]
144 fn from(val: SyncMutPtr<T>) -> Self {
145 val.inner()
146 }
147}
148
149impl<T> From<SyncMutPtr<T>> for *const T {
150 #[inline(always)]
151 fn from(val: SyncMutPtr<T>) -> Self {
152 val.inner()
153 }
154}
155
156#[repr(transparent)]
160#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
161pub struct SyncConstPtr<T>(*const T);
162
163unsafe impl<T> Sync for SyncConstPtr<T> {}
164unsafe impl<T> Send for SyncConstPtr<T> {}
165
166impl<T> Clone for SyncConstPtr<T> {
167 #[inline(always)]
168 fn clone(&self) -> Self {
169 *self
170 }
171}
172
173impl<T> Copy for SyncConstPtr<T> {}
174
175impl<T> Pointer for SyncConstPtr<T> {
176 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
177 core::fmt::Pointer::fmt(&self.0, f)
178 }
179}
180
181impl<T> SyncConstPtr<T> {
182 #[inline(always)]
191 #[must_use]
192 pub const unsafe fn new(ptr: *const T) -> Self {
193 Self(ptr)
194 }
195
196 #[inline(always)]
200 #[must_use]
201 pub const fn null() -> Self {
202 Self(core::ptr::null())
203 }
204
205 #[inline(always)]
209 #[must_use]
210 pub const fn cast<Y>(&self) -> SyncConstPtr<Y> {
211 SyncConstPtr(self.0.cast())
212 }
213
214 #[inline(always)]
218 #[must_use]
219 pub const fn inner(&self) -> *const T {
220 self.0
221 }
222
223 #[inline(always)]
227 #[must_use]
228 pub const fn as_sync_const(&self) -> Self {
229 Self(self.0)
230 }
231
232 #[inline(always)]
236 #[must_use]
237 pub const fn as_send_const(&self) -> SendConstPtr<T> {
238 SendConstPtr(self.0)
239 }
240
241 #[inline(always)]
248 #[must_use]
249 pub const fn as_sync_mut(&self) -> SyncMutPtr<T> {
250 SyncMutPtr(self.0.cast_mut())
251 }
252
253 #[inline(always)]
260 #[must_use]
261 pub const fn as_send_mut(&self) -> SendMutPtr<T> {
262 SendMutPtr(self.0.cast_mut())
263 }
264}
265
266impl<T> Deref for SyncConstPtr<T> {
267 type Target = *const T;
268
269 #[inline(always)]
270 fn deref(&self) -> &Self::Target {
271 &self.0
272 }
273}
274
275impl<T> From<SyncConstPtr<T>> for *const T {
276 #[inline(always)]
277 fn from(val: SyncConstPtr<T>) -> Self {
278 val.inner()
279 }
280}
281
282#[repr(transparent)]
286#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
287pub struct SendMutPtr<T>(*mut T);
288
289unsafe impl<T> Send for SendMutPtr<T> {}
290
291impl<T> Clone for SendMutPtr<T> {
292 #[inline(always)]
293 fn clone(&self) -> Self {
294 *self
295 }
296}
297
298impl<T> Copy for SendMutPtr<T> {}
299
300impl<T> Pointer for SendMutPtr<T> {
301 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
302 core::fmt::Pointer::fmt(&self.0, f)
303 }
304}
305
306impl<T> SendMutPtr<T> {
307 #[inline(always)]
316 #[must_use]
317 pub const unsafe fn new(ptr: *mut T) -> Self {
318 Self(ptr)
319 }
320 #[inline(always)]
324 #[must_use]
325 pub const fn null() -> Self {
326 Self(core::ptr::null_mut())
327 }
328
329 #[inline(always)]
333 #[must_use]
334 pub const fn cast<Y>(&self) -> SendMutPtr<Y> {
335 SendMutPtr(self.0.cast())
336 }
337
338 #[inline(always)]
342 #[must_use]
343 pub const fn inner(&self) -> *mut T {
344 self.0
345 }
346
347 #[inline(always)]
356 #[must_use]
357 pub const unsafe fn as_sync_const(&self) -> SyncConstPtr<T> {
358 SyncConstPtr(self.0)
359 }
360
361 #[inline(always)]
365 #[must_use]
366 pub const fn as_send_const(&self) -> SendConstPtr<T> {
367 SendConstPtr(self.0)
368 }
369
370 #[inline(always)]
379 #[must_use]
380 pub const unsafe fn as_sync_mut(&self) -> SyncMutPtr<T> {
381 SyncMutPtr(self.0)
382 }
383
384 #[inline(always)]
388 #[must_use]
389 pub const fn as_send_mut(&self) -> Self {
390 Self(self.0)
391 }
392}
393
394impl<T> Deref for SendMutPtr<T> {
395 type Target = *mut T;
396
397 #[inline(always)]
398 fn deref(&self) -> &Self::Target {
399 &self.0
400 }
401}
402
403impl<T> From<SendMutPtr<T>> for *mut T {
404 #[inline(always)]
405 fn from(val: SendMutPtr<T>) -> Self {
406 val.inner()
407 }
408}
409
410impl<T> From<SendMutPtr<T>> for *const T {
411 #[inline(always)]
412 fn from(val: SendMutPtr<T>) -> Self {
413 val.inner()
414 }
415}
416
417#[repr(transparent)]
421#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
422pub struct SendConstPtr<T>(*const T);
423
424unsafe impl<T> Send for SendConstPtr<T> {}
425
426impl<T> Clone for SendConstPtr<T> {
427 #[inline(always)]
428 fn clone(&self) -> Self {
429 *self
430 }
431}
432
433impl<T> Copy for SendConstPtr<T> {}
434
435impl<T> Pointer for SendConstPtr<T> {
436 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
437 core::fmt::Pointer::fmt(&self.0, f)
438 }
439}
440
441impl<T> SendConstPtr<T> {
442 #[inline(always)]
451 #[must_use]
452 pub const unsafe fn new(ptr: *const T) -> Self {
453 Self(ptr)
454 }
455
456 #[inline(always)]
460 #[must_use]
461 pub const fn null() -> Self {
462 Self(core::ptr::null())
463 }
464
465 #[inline(always)]
469 #[must_use]
470 pub const fn cast<Y>(&self) -> SendConstPtr<Y> {
471 SendConstPtr(self.0.cast())
472 }
473
474 #[inline(always)]
478 #[must_use]
479 pub const fn inner(&self) -> *const T {
480 self.0
481 }
482
483 #[inline(always)]
492 #[must_use]
493 pub const unsafe fn as_sync_const(&self) -> SyncConstPtr<T> {
494 SyncConstPtr(self.0)
495 }
496
497 #[inline(always)]
501 #[must_use]
502 pub const fn as_send_const(&self) -> Self {
503 Self(self.0)
504 }
505
506 #[inline(always)]
517 #[must_use]
518 pub const unsafe fn as_sync_mut(&self) -> SyncMutPtr<T> {
519 SyncMutPtr(self.0.cast_mut())
520 }
521
522 #[inline(always)]
529 #[must_use]
530 pub const fn as_send_mut(&self) -> SendMutPtr<T> {
531 SendMutPtr(self.0.cast_mut())
532 }
533}
534
535impl<T> Deref for SendConstPtr<T> {
536 type Target = *const T;
537
538 #[inline(always)]
539 fn deref(&self) -> &Self::Target {
540 &self.0
541 }
542}
543
544impl<T> From<SendConstPtr<T>> for *const T {
545 #[inline(always)]
546 fn from(val: SendConstPtr<T>) -> *const T {
547 val.inner()
548 }
549}
550
551pub trait FromConstPtr<T>: Sized {
552 unsafe fn as_sync_const(&self) -> SyncConstPtr<T>;
561
562 unsafe fn as_send_const(&self) -> SendConstPtr<T>;
571}
572
573pub trait FromMutPtr<T>: FromConstPtr<T> {
574 unsafe fn as_sync_mut(&self) -> SyncMutPtr<T>;
583
584 unsafe fn as_send_mut(&self) -> SendMutPtr<T>;
593}
594
595impl<T> FromConstPtr<T> for *const T {
596 #[inline(always)]
597 unsafe fn as_sync_const(&self) -> SyncConstPtr<T> {
598 SyncConstPtr(self.cast())
599 }
600
601 #[inline(always)]
602 unsafe fn as_send_const(&self) -> SendConstPtr<T> {
603 SendConstPtr(self.cast())
604 }
605}
606
607impl<T> FromConstPtr<T> for *mut T {
608 #[inline(always)]
609 unsafe fn as_sync_const(&self) -> SyncConstPtr<T> {
610 SyncConstPtr(self.cast())
611 }
612
613 #[inline(always)]
614 unsafe fn as_send_const(&self) -> SendConstPtr<T> {
615 SendConstPtr(self.cast())
616 }
617}
618
619impl<T> FromMutPtr<T> for *mut T {
620 #[inline(always)]
621 unsafe fn as_sync_mut(&self) -> SyncMutPtr<T> {
622 SyncMutPtr(self.cast())
623 }
624
625 #[inline(always)]
626 unsafe fn as_send_mut(&self) -> SendMutPtr<T> {
627 SendMutPtr(self.cast())
628 }
629}