Skip to content

Commit

Permalink
Merge pull request #41 from kadiwa4/impl_traits
Browse files Browse the repository at this point in the history
Implement some useful traits
  • Loading branch information
phil-opp committed Mar 22, 2024
2 parents 0d09121 + 56dc7f9 commit ef6eb83
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Add implementations for `fmt::Pointer`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`.

# 0.5.1 – 2023-06-24

- Fix: Add missing documentation of the `map` macro
Expand Down
56 changes: 50 additions & 6 deletions src/volatile_ptr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{fmt, marker::PhantomData, ptr::NonNull};
use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};

use crate::access::ReadWrite;

Expand Down Expand Up @@ -47,12 +47,56 @@ where

impl<T, A> fmt::Debug for VolatilePtr<'_, T, A>
where
T: Copy + fmt::Debug + ?Sized,
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VolatilePtr")
.field("pointer", &self.pointer)
.field("access", &self.access)
.finish()
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
}
}

impl<T, A> fmt::Pointer for VolatilePtr<'_, T, A>
where
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
}
}

impl<T, A> PartialEq for VolatilePtr<'_, T, A>
where
T: ?Sized,
{
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
}
}

impl<T, A> Eq for VolatilePtr<'_, T, A> where T: ?Sized {}

impl<T, A> PartialOrd for VolatilePtr<'_, T, A>
where
T: ?Sized,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
}
}

impl<T, A> Ord for VolatilePtr<'_, T, A>
where
T: ?Sized,
{
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
}
}

impl<T, A> hash::Hash for VolatilePtr<'_, T, A>
where
T: ?Sized,
{
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.pointer.as_ptr().hash(state);
}
}
59 changes: 53 additions & 6 deletions src/volatile_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
access::{Access, Copyable, ReadOnly, ReadWrite, WriteOnly},
volatile_ptr::VolatilePtr,
};
use core::{fmt, marker::PhantomData, ptr::NonNull};
use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};

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

impl<T, A> fmt::Debug for VolatileRef<'_, T, A>
where
T: Copy + fmt::Debug + ?Sized,
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
}
}

impl<T, A> fmt::Pointer for VolatileRef<'_, T, A>
where
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VolatileRef")
.field("pointer", &self.pointer)
.field("access", &self.access)
.finish()
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
}
}

impl<T, A> PartialEq for VolatileRef<'_, T, A>
where
T: ?Sized,
{
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
}
}

impl<T, A> Eq for VolatileRef<'_, T, A> where T: ?Sized {}

impl<T, A> PartialOrd for VolatileRef<'_, T, A>
where
T: ?Sized,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
}
}

impl<T, A> Ord for VolatileRef<'_, T, A>
where
T: ?Sized,
{
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
}
}

impl<T, A> hash::Hash for VolatileRef<'_, T, A>
where
T: ?Sized,
{
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.pointer.as_ptr().hash(state);
}
}

0 comments on commit ef6eb83

Please sign in to comment.