Skip to content

Commit bac8caf

Browse files
committed
Merge branch 'main' into pr/remove-sized-requirement-for-send-sync-volatile-ref
2 parents f87dd65 + 595ccf3 commit bac8caf

File tree

5 files changed

+110
-19
lines changed

5 files changed

+110
-19
lines changed

Changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Unreleased
22

3+
- Add implementations for `fmt::Pointer`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`.
4+
- Update `very_unstable` feature to latest nightly
5+
36
# 0.5.1 – 2023-06-24
47

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

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![cfg_attr(feature = "very_unstable", feature(const_trait_impl))]
3939
#![cfg_attr(feature = "very_unstable", feature(unboxed_closures))]
4040
#![cfg_attr(feature = "very_unstable", feature(fn_traits))]
41+
#![cfg_attr(feature = "very_unstable", feature(effects))]
4142
#![warn(missing_docs)]
4243
#![deny(unsafe_op_in_unsafe_fn)]
4344

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_ptr/very_unstable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616
/// The safety requirements of [`Self::map`] apply to this method too.
1717
pub const unsafe fn map_const<F, U>(self, f: F) -> VolatilePtr<'a, U, A>
1818
where
19-
F: ~const FnOnce(NonNull<T>) -> NonNull<U>,
19+
F: FnOnce(NonNull<T>) -> NonNull<U>,
2020
U: ?Sized,
2121
{
2222
unsafe { VolatilePtr::new_generic(f(self.pointer)) }
@@ -36,7 +36,7 @@ impl<'a, T, A> VolatilePtr<'a, [T], A> {
3636
struct Mapper {
3737
index: usize,
3838
}
39-
impl<T> const FnOnce<(NonNull<[T]>,)> for Mapper {
39+
impl<T> FnOnce<(NonNull<[T]>,)> for Mapper {
4040
type Output = NonNull<T>;
4141

4242
extern "rust-call" fn call_once(self, (slice,): (NonNull<[T]>,)) -> Self::Output {

src/volatile_ref.rs

+54-11
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.
@@ -219,11 +222,7 @@ where
219222
A: Access + Copyable,
220223
{
221224
fn clone(&self) -> Self {
222-
Self {
223-
pointer: self.pointer,
224-
reference: self.reference,
225-
access: self.access,
226-
}
225+
*self
227226
}
228227
}
229228

@@ -239,12 +238,56 @@ unsafe impl<T, A> Sync for VolatileRef<'_, T, A> where T: Sync + ?Sized {}
239238

240239
impl<T, A> fmt::Debug for VolatileRef<'_, T, A>
241240
where
242-
T: Copy + fmt::Debug + ?Sized,
241+
T: ?Sized,
242+
{
243+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
245+
}
246+
}
247+
248+
impl<T, A> fmt::Pointer for VolatileRef<'_, T, A>
249+
where
250+
T: ?Sized,
243251
{
244252
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()
253+
fmt::Pointer::fmt(&self.pointer.as_ptr(), f)
254+
}
255+
}
256+
257+
impl<T, A> PartialEq for VolatileRef<'_, T, A>
258+
where
259+
T: ?Sized,
260+
{
261+
fn eq(&self, other: &Self) -> bool {
262+
core::ptr::eq(self.pointer.as_ptr(), other.pointer.as_ptr())
263+
}
264+
}
265+
266+
impl<T, A> Eq for VolatileRef<'_, T, A> where T: ?Sized {}
267+
268+
impl<T, A> PartialOrd for VolatileRef<'_, T, A>
269+
where
270+
T: ?Sized,
271+
{
272+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
273+
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))
274+
}
275+
}
276+
277+
impl<T, A> Ord for VolatileRef<'_, T, A>
278+
where
279+
T: ?Sized,
280+
{
281+
fn cmp(&self, other: &Self) -> Ordering {
282+
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())
283+
}
284+
}
285+
286+
impl<T, A> hash::Hash for VolatileRef<'_, T, A>
287+
where
288+
T: ?Sized,
289+
{
290+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
291+
self.pointer.as_ptr().hash(state);
249292
}
250293
}

0 commit comments

Comments
 (0)