Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement some useful traits #41

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
/// - 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 @@ -218,7 +221,7 @@
T: ?Sized,
A: Access + Copyable,
{
fn clone(&self) -> Self {

Check warning on line 224 in src/volatile_ref.rs

View workflow job for this annotation

GitHub Actions / Lints

non-canonical implementation of `clone` on a `Copy` type
Self {
pointer: self.pointer,
reference: self.reference,
Expand All @@ -239,12 +242,56 @@

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);
}
}
Loading