sel4_abstract_ptr/abstract_ptr/
mod.rs

1//
2// Copyright 2024, Colias Group, LLC
3// Copyright (c) 2020 Philipp Oppermann
4//
5// SPDX-License-Identifier: MIT OR Apache-2.0
6//
7
8use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};
9
10use crate::access::ReadWrite;
11
12mod atomic_operations;
13mod macros;
14mod operations;
15mod slice_operations;
16
17#[must_use]
18#[repr(transparent)]
19pub struct AbstractPtr<'a, M, T, A = ReadWrite>
20where
21    T: ?Sized,
22{
23    pointer: NonNull<T>,
24    memory_type: PhantomData<M>,
25    reference: PhantomData<&'a T>,
26    access: PhantomData<A>,
27}
28
29impl<M, T, A> Copy for AbstractPtr<'_, M, T, A> where T: ?Sized {}
30
31impl<M, T, A> Clone for AbstractPtr<'_, M, T, A>
32where
33    T: ?Sized,
34{
35    fn clone(&self) -> Self {
36        *self
37    }
38}
39
40impl<M, T, A> fmt::Debug for AbstractPtr<'_, M, T, A>
41where
42    T: ?Sized,
43{
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
46    }
47}
48
49impl<M, T, A> fmt::Pointer for AbstractPtr<'_, M, T, A>
50where
51    T: ?Sized,
52{
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
55    }
56}
57
58impl<M, T, A> PartialEq for AbstractPtr<'_, M, T, A>
59where
60    T: ?Sized,
61{
62    fn eq(&self, other: &Self) -> bool {
63        core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
64    }
65}
66
67impl<M, T, A> Eq for AbstractPtr<'_, M, T, A> where T: ?Sized {}
68
69impl<M, T, A> PartialOrd for AbstractPtr<'_, M, T, A>
70where
71    T: ?Sized,
72{
73    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
74        Some(self.cmp(other))
75    }
76}
77
78impl<M, T, A> Ord for AbstractPtr<'_, M, T, A>
79where
80    T: ?Sized,
81{
82    fn cmp(&self, other: &Self) -> Ordering {
83        #[allow(ambiguous_wide_pointer_comparisons)]
84        Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
85    }
86}
87
88impl<M, T, A> hash::Hash for AbstractPtr<'_, M, T, A>
89where
90    T: ?Sized,
91{
92    fn hash<H: hash::Hasher>(&self, state: &mut H) {
93        self.pointer.as_ptr().hash(state);
94    }
95}