Skip to content

Commit 04a213e

Browse files
committed
Make errors Clone.
Manually implement PartialEq for types using SendSyncPhantomData. This is necessary because the default derive imposes an additional PartialEq bound on the phantom type.
1 parent 846c36e commit 04a213e

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

src/error.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ use crate::{FromBytes, Ref};
149149
/// - [`CastError`]: the error type of reference conversions
150150
/// - [`TryCastError`]: the error type of fallible reference conversions
151151
/// - [`TryReadError`]: the error type of fallible read conversions
152-
#[derive(PartialEq, Eq)]
152+
#[derive(PartialEq, Eq, Clone)]
153153
pub enum ConvertError<A, S, V> {
154154
/// The conversion source was improperly aligned.
155155
Alignment(A),
@@ -242,15 +242,15 @@ where
242242
}
243243

244244
/// The error emitted if the conversion source is improperly aligned.
245-
#[derive(PartialEq, Eq)]
245+
#[derive(Eq, Clone)]
246246
pub struct AlignmentError<Src, Dst: ?Sized> {
247247
/// The source value involved in the conversion.
248248
src: Src,
249249
/// The inner destination type involved in the conversion.
250250
///
251251
/// INVARIANT: An `AlignmentError` may only be constructed if `Dst`'s
252252
/// alignment requirement is greater than one.
253-
dst: SendSyncPhantomData<Dst>,
253+
_dst: SendSyncPhantomData<Dst>,
254254
}
255255

256256
impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
@@ -261,7 +261,7 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
261261
pub(crate) unsafe fn new_unchecked(src: Src) -> Self {
262262
// INVARIANT: The caller guarantees that `Dst`'s alignment requirement
263263
// is greater than one.
264-
Self { src, dst: SendSyncPhantomData::default() }
264+
Self { src, _dst: SendSyncPhantomData::default() }
265265
}
266266

267267
/// Produces the source underlying the failed conversion.
@@ -274,7 +274,7 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
274274
// INVARIANT: `with_src` doesn't change the type of `Dst`, so the
275275
// invariant that `Dst`'s alignment requirement is greater than one is
276276
// preserved.
277-
AlignmentError { src: new_src, dst: SendSyncPhantomData::default() }
277+
AlignmentError { src: new_src, _dst: SendSyncPhantomData::default() }
278278
}
279279

280280
/// Maps the source value associated with the conversion error.
@@ -299,7 +299,7 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
299299
/// ```
300300
#[inline]
301301
pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> AlignmentError<NewSrc, Dst> {
302-
AlignmentError { src: f(self.src), dst: SendSyncPhantomData::default() }
302+
AlignmentError { src: f(self.src), _dst: SendSyncPhantomData::default() }
303303
}
304304

305305
pub(crate) fn into<S, V>(self) -> ConvertError<Self, S, V> {
@@ -337,6 +337,13 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
337337
}
338338
}
339339

340+
impl<Src: PartialEq, Dst: ?Sized> PartialEq for AlignmentError<Src, Dst> {
341+
#[inline]
342+
fn eq(&self, other: &Self) -> bool {
343+
self.src == other.src
344+
}
345+
}
346+
340347
impl<Src, Dst: ?Sized + Unaligned> From<AlignmentError<Src, Dst>> for Infallible {
341348
#[inline(always)]
342349
fn from(_: AlignmentError<Src, Dst>) -> Infallible {
@@ -408,17 +415,17 @@ impl<Src, Dst: ?Sized, S, V> From<AlignmentError<Src, Dst>>
408415
}
409416

410417
/// The error emitted if the conversion source is of incorrect size.
411-
#[derive(PartialEq, Eq)]
418+
#[derive(Eq, Clone)]
412419
pub struct SizeError<Src, Dst: ?Sized> {
413420
/// The source value involved in the conversion.
414421
src: Src,
415422
/// The inner destination type involved in the conversion.
416-
dst: SendSyncPhantomData<Dst>,
423+
_dst: SendSyncPhantomData<Dst>,
417424
}
418425

419426
impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
420427
pub(crate) fn new(src: Src) -> Self {
421-
Self { src, dst: SendSyncPhantomData::default() }
428+
Self { src, _dst: SendSyncPhantomData::default() }
422429
}
423430

424431
/// Produces the source underlying the failed conversion.
@@ -429,7 +436,7 @@ impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
429436

430437
/// Sets the source value associated with the conversion error.
431438
pub(crate) fn with_src<NewSrc>(self, new_src: NewSrc) -> SizeError<NewSrc, Dst> {
432-
SizeError { src: new_src, dst: SendSyncPhantomData::default() }
439+
SizeError { src: new_src, _dst: SendSyncPhantomData::default() }
433440
}
434441

435442
/// Maps the source value associated with the conversion error.
@@ -455,12 +462,12 @@ impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
455462
/// ```
456463
#[inline]
457464
pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> SizeError<NewSrc, Dst> {
458-
SizeError { src: f(self.src), dst: SendSyncPhantomData::default() }
465+
SizeError { src: f(self.src), _dst: SendSyncPhantomData::default() }
459466
}
460467

461468
/// Sets the destination type associated with the conversion error.
462469
pub(crate) fn with_dst<NewDst: ?Sized>(self) -> SizeError<Src, NewDst> {
463-
SizeError { src: self.src, dst: SendSyncPhantomData::default() }
470+
SizeError { src: self.src, _dst: SendSyncPhantomData::default() }
464471
}
465472

466473
/// Converts the error into a general [`ConvertError`].
@@ -507,6 +514,13 @@ impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
507514
}
508515
}
509516

517+
impl<Src: PartialEq, Dst: ?Sized> PartialEq for SizeError<Src, Dst> {
518+
#[inline]
519+
fn eq(&self, other: &Self) -> bool {
520+
self.src == other.src
521+
}
522+
}
523+
510524
impl<Src, Dst: ?Sized> fmt::Debug for SizeError<Src, Dst> {
511525
#[inline]
512526
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -552,17 +566,17 @@ impl<Src, Dst: ?Sized, A, V> From<SizeError<Src, Dst>> for ConvertError<A, SizeE
552566
}
553567

554568
/// The error emitted if the conversion source contains invalid data.
555-
#[derive(PartialEq, Eq)]
569+
#[derive(Eq)]
556570
pub struct ValidityError<Src, Dst: ?Sized + TryFromBytes> {
557571
/// The source value involved in the conversion.
558572
pub(crate) src: Src,
559573
/// The inner destination type involved in the conversion.
560-
dst: SendSyncPhantomData<Dst>,
574+
_dst: SendSyncPhantomData<Dst>,
561575
}
562576

563577
impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
564578
pub(crate) fn new(src: Src) -> Self {
565-
Self { src, dst: SendSyncPhantomData::default() }
579+
Self { src, _dst: SendSyncPhantomData::default() }
566580
}
567581

568582
/// Produces the source underlying the failed conversion.
@@ -593,7 +607,7 @@ impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
593607
/// ```
594608
#[inline]
595609
pub fn map_src<NewSrc>(self, f: impl FnOnce(Src) -> NewSrc) -> ValidityError<NewSrc, Dst> {
596-
ValidityError { src: f(self.src), dst: SendSyncPhantomData::default() }
610+
ValidityError { src: f(self.src), _dst: SendSyncPhantomData::default() }
597611
}
598612

599613
/// Converts the error into a general [`ConvertError`].
@@ -614,6 +628,13 @@ impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
614628
}
615629
}
616630

631+
impl<Src: PartialEq, Dst: ?Sized + TryFromBytes> PartialEq for ValidityError<Src, Dst> {
632+
#[inline]
633+
fn eq(&self, other: &Self) -> bool {
634+
self.src == other.src
635+
}
636+
}
637+
617638
impl<Src, Dst: ?Sized + TryFromBytes> fmt::Debug for ValidityError<Src, Dst> {
618639
#[inline]
619640
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

src/util/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ impl<T: ?Sized> Default for SendSyncPhantomData<T> {
3939
}
4040

4141
impl<T: ?Sized> PartialEq for SendSyncPhantomData<T> {
42-
fn eq(&self, other: &Self) -> bool {
43-
self.0.eq(&other.0)
42+
fn eq(&self, _other: &Self) -> bool {
43+
true
4444
}
4545
}
4646

4747
impl<T: ?Sized> Eq for SendSyncPhantomData<T> {}
4848

49+
impl<T: ?Sized> Clone for SendSyncPhantomData<T> {
50+
fn clone(&self) -> Self {
51+
SendSyncPhantomData(PhantomData)
52+
}
53+
}
54+
4955
pub(crate) trait AsAddress {
5056
fn addr(self) -> usize;
5157
}

0 commit comments

Comments
 (0)