Skip to content

Commit c70044c

Browse files
committed
implement some useful traits
1 parent 0d09121 commit c70044c

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
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

+49-2
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,7 +47,7 @@ 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 {
5353
f.debug_struct("VolatilePtr")
@@ -56,3 +56,50 @@ where
5656
.finish()
5757
}
5858
}
59+
60+
impl<T, A> fmt::Pointer for VolatilePtr<'_, T, A>
61+
where
62+
T: ?Sized,
63+
{
64+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
66+
}
67+
}
68+
69+
impl<T, A> PartialEq for VolatilePtr<'_, T, A>
70+
where
71+
T: ?Sized,
72+
{
73+
fn eq(&self, other: &Self) -> bool {
74+
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
75+
}
76+
}
77+
78+
impl<T, A> Eq for VolatilePtr<'_, T, A> where T: ?Sized {}
79+
80+
impl<T, A> PartialOrd for VolatilePtr<'_, T, A>
81+
where
82+
T: ?Sized,
83+
{
84+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
85+
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
86+
}
87+
}
88+
89+
impl<T, A> Ord for VolatilePtr<'_, T, A>
90+
where
91+
T: ?Sized,
92+
{
93+
fn cmp(&self, other: &Self) -> Ordering {
94+
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
95+
}
96+
}
97+
98+
impl<T, A> hash::Hash for VolatilePtr<'_, T, A>
99+
where
100+
T: ?Sized,
101+
{
102+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
103+
self.pointer.as_ptr().hash(state);
104+
}
105+
}

src/volatile_ref.rs

+52-2
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,7 +242,7 @@ 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,
243246
{
244247
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245248
f.debug_struct("VolatileRef")
@@ -248,3 +251,50 @@ where
248251
.finish()
249252
}
250253
}
254+
255+
impl<T, A> fmt::Pointer for VolatileRef<'_, T, A>
256+
where
257+
T: ?Sized,
258+
{
259+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
261+
}
262+
}
263+
264+
impl<T, A> PartialEq for VolatileRef<'_, T, A>
265+
where
266+
T: ?Sized,
267+
{
268+
fn eq(&self, other: &Self) -> bool {
269+
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
270+
}
271+
}
272+
273+
impl<T, A> Eq for VolatileRef<'_, T, A> where T: ?Sized {}
274+
275+
impl<T, A> PartialOrd for VolatileRef<'_, T, A>
276+
where
277+
T: ?Sized,
278+
{
279+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
280+
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
281+
}
282+
}
283+
284+
impl<T, A> Ord for VolatileRef<'_, T, A>
285+
where
286+
T: ?Sized,
287+
{
288+
fn cmp(&self, other: &Self) -> Ordering {
289+
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
290+
}
291+
}
292+
293+
impl<T, A> hash::Hash for VolatileRef<'_, T, A>
294+
where
295+
T: ?Sized,
296+
{
297+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
298+
self.pointer.as_ptr().hash(state);
299+
}
300+
}

0 commit comments

Comments
 (0)