Skip to content

Commit ef6eb83

Browse files
authored
Merge pull request #41 from kadiwa4/impl_traits
Implement some useful traits
2 parents 0d09121 + 56dc7f9 commit ef6eb83

File tree

3 files changed

+105
-12
lines changed

3 files changed

+105
-12
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Add implementations for `fmt::Pointer`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`.
4+
35
# 0.5.1 – 2023-06-24
46

57
- Fix: Add missing documentation of the `map` macro

src/volatile_ptr/mod.rs

+50-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::{fmt, marker::PhantomData, ptr::NonNull};
1+
use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};
22

33
use crate::access::ReadWrite;
44

@@ -47,12 +47,56 @@ where
4747

4848
impl<T, A> fmt::Debug for VolatilePtr<'_, T, A>
4949
where
50-
T: Copy + fmt::Debug + ?Sized,
50+
T: ?Sized,
5151
{
5252
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53-
f.debug_struct("VolatilePtr")
54-
.field("pointer", &self.pointer)
55-
.field("access", &self.access)
56-
.finish()
53+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
54+
}
55+
}
56+
57+
impl<T, A> fmt::Pointer for VolatilePtr<'_, T, A>
58+
where
59+
T: ?Sized,
60+
{
61+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
63+
}
64+
}
65+
66+
impl<T, A> PartialEq for VolatilePtr<'_, T, A>
67+
where
68+
T: ?Sized,
69+
{
70+
fn eq(&self, other: &Self) -> bool {
71+
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
72+
}
73+
}
74+
75+
impl<T, A> Eq for VolatilePtr<'_, T, A> where T: ?Sized {}
76+
77+
impl<T, A> PartialOrd for VolatilePtr<'_, T, A>
78+
where
79+
T: ?Sized,
80+
{
81+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
82+
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
83+
}
84+
}
85+
86+
impl<T, A> Ord for VolatilePtr<'_, T, A>
87+
where
88+
T: ?Sized,
89+
{
90+
fn cmp(&self, other: &Self) -> Ordering {
91+
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
92+
}
93+
}
94+
95+
impl<T, A> hash::Hash for VolatilePtr<'_, T, A>
96+
where
97+
T: ?Sized,
98+
{
99+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
100+
self.pointer.as_ptr().hash(state);
57101
}
58102
}

src/volatile_ref.rs

+53-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
access::{Access, Copyable, ReadOnly, ReadWrite, WriteOnly},
33
volatile_ptr::VolatilePtr,
44
};
5-
use core::{fmt, marker::PhantomData, ptr::NonNull};
5+
use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};
66

77
/// Volatile pointer type that respects Rust's aliasing rules.
88
///
@@ -12,6 +12,9 @@ use core::{fmt, marker::PhantomData, ptr::NonNull};
1212
/// - only read-only types implement [`Clone`] and [`Copy`]
1313
/// - [`Send`] and [`Sync`] are implemented if `T: Sync`
1414
///
15+
/// However, trait implementations like [`fmt::Debug`] and [`Eq`] behave like they do on pointer
16+
/// types and don't access the referenced value.
17+
///
1518
/// To perform volatile operations on `VolatileRef` types, use the [`as_ptr`][Self::as_ptr]
1619
/// or [`as_mut_ptr`](Self::as_mut_ptr) methods to create a temporary
1720
/// [`VolatilePtr`][crate::VolatilePtr] instance.
@@ -239,12 +242,56 @@ unsafe impl<T, A> Sync for VolatileRef<'_, T, A> where T: Sync {}
239242

240243
impl<T, A> fmt::Debug for VolatileRef<'_, T, A>
241244
where
242-
T: Copy + fmt::Debug + ?Sized,
245+
T: ?Sized,
246+
{
247+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
249+
}
250+
}
251+
252+
impl<T, A> fmt::Pointer for VolatileRef<'_, T, A>
253+
where
254+
T: ?Sized,
243255
{
244256
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245-
f.debug_struct("VolatileRef")
246-
.field("pointer", &self.pointer)
247-
.field("access", &self.access)
248-
.finish()
257+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
258+
}
259+
}
260+
261+
impl<T, A> PartialEq for VolatileRef<'_, T, A>
262+
where
263+
T: ?Sized,
264+
{
265+
fn eq(&self, other: &Self) -> bool {
266+
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
267+
}
268+
}
269+
270+
impl<T, A> Eq for VolatileRef<'_, T, A> where T: ?Sized {}
271+
272+
impl<T, A> PartialOrd for VolatileRef<'_, T, A>
273+
where
274+
T: ?Sized,
275+
{
276+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
277+
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
278+
}
279+
}
280+
281+
impl<T, A> Ord for VolatileRef<'_, T, A>
282+
where
283+
T: ?Sized,
284+
{
285+
fn cmp(&self, other: &Self) -> Ordering {
286+
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
287+
}
288+
}
289+
290+
impl<T, A> hash::Hash for VolatileRef<'_, T, A>
291+
where
292+
T: ?Sized,
293+
{
294+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
295+
self.pointer.as_ptr().hash(state);
249296
}
250297
}

0 commit comments

Comments
 (0)