Skip to content

Commit 300842a

Browse files
committed
Stop using Eq::assert_receiver_is_total_eq()
1 parent f37bb41 commit 300842a

File tree

5 files changed

+120
-52
lines changed

5 files changed

+120
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
`Drop` but instead only asserts that every field implements `ZeroizeOnDrop`.
1313

1414
### Fixed
15-
- Stop depending on unstable APIs of `zeroize` to implement `ZeroizeOnDrop`.
15+
- Stop depending on unstable APIs for `Eq` for `ZeroizeOnDrop`.
1616

1717
## [1.4.0] - 2025-05-01
1818

src/test/basic.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,23 @@ fn struct_() -> Result<()> {
4545
}
4646
}
4747

48-
#[automatically_derived]
49-
impl<T> ::core::cmp::Eq for Test<T> {
50-
#[inline]
51-
fn assert_receiver_is_total_eq(&self) {
52-
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
48+
const _: () = {
49+
trait DeriveWhereAssertEq {
50+
fn assert(&self);
51+
}
5352

54-
// For some reason the comparison fails without the extra space at the end.
55-
let _: __AssertEq<std::marker::PhantomData<T> >;
53+
impl<T> DeriveWhereAssertEq for Test<T> {
54+
fn assert(&self) {
55+
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
56+
57+
// For some reason the comparison fails without the extra space at the end.
58+
let _: __AssertEq<std::marker::PhantomData<T> >;
59+
}
5660
}
57-
}
61+
};
62+
63+
#[automatically_derived]
64+
impl<T> ::core::cmp::Eq for Test<T> { }
5865

5966
#[automatically_derived]
6067
impl<T> ::core::hash::Hash for Test<T> {
@@ -141,16 +148,23 @@ fn tuple() -> Result<()> {
141148
}
142149
}
143150

144-
#[automatically_derived]
145-
impl<T> ::core::cmp::Eq for Test<T> {
146-
#[inline]
147-
fn assert_receiver_is_total_eq(&self) {
148-
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
151+
const _: () = {
152+
trait DeriveWhereAssertEq {
153+
fn assert(&self);
154+
}
155+
156+
impl<T> DeriveWhereAssertEq for Test<T> {
157+
fn assert(&self) {
158+
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
149159

150-
// For some reason the comparison fails without the extra space at the end.
151-
let _: __AssertEq<std::marker::PhantomData<T> >;
160+
// For some reason the comparison fails without the extra space at the end.
161+
let _: __AssertEq<std::marker::PhantomData<T> >;
162+
}
152163
}
153-
}
164+
};
165+
166+
#[automatically_derived]
167+
impl<T> ::core::cmp::Eq for Test<T> { }
154168

155169
#[automatically_derived]
156170
impl<T> ::core::hash::Hash for Test<T> {
@@ -287,17 +301,24 @@ fn enum_() -> Result<()> {
287301
}
288302
}
289303

290-
#[automatically_derived]
291-
impl<T> ::core::cmp::Eq for Test<T> {
292-
#[inline]
293-
fn assert_receiver_is_total_eq(&self) {
294-
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
304+
const _: () = {
305+
trait DeriveWhereAssertEq {
306+
fn assert(&self);
307+
}
308+
309+
impl<T> DeriveWhereAssertEq for Test<T> {
310+
fn assert(&self) {
311+
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
295312

296-
// For some reason the comparison fails without the extra space at the end.
297-
let _: __AssertEq<std::marker::PhantomData<T> >;
298-
let _: __AssertEq<std::marker::PhantomData<T> >;
313+
// For some reason the comparison fails without the extra space at the end.
314+
let _: __AssertEq<std::marker::PhantomData<T> >;
315+
let _: __AssertEq<std::marker::PhantomData<T> >;
316+
}
299317
}
300-
}
318+
};
319+
320+
#[automatically_derived]
321+
impl<T> ::core::cmp::Eq for Test<T> { }
301322

302323
#[automatically_derived]
303324
impl<T> ::core::hash::Hash for Test<T> {

src/test/bound.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,28 @@ fn check_trait_bounds() -> Result<()> {
235235
}
236236
}
237237

238+
const _: () = {
239+
trait DeriveWhereAssertEq {
240+
fn assert(&self);
241+
}
242+
243+
impl<T, U> DeriveWhereAssertEq for Test<T, U>
244+
where T: ::core::cmp::Eq
245+
{
246+
fn assert(&self) {
247+
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
248+
249+
// For some reason the comparison fails without the extra space at the end.
250+
let _: __AssertEq<T >;
251+
let _: __AssertEq<std::marker::PhantomData<U> >;
252+
}
253+
}
254+
};
255+
238256
#[automatically_derived]
239257
impl<T, U> ::core::cmp::Eq for Test<T, U>
240258
where T: ::core::cmp::Eq
241-
{
242-
#[inline]
243-
fn assert_receiver_is_total_eq(&self) {
244-
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
245-
246-
// For some reason the comparison fails without the extra space at the end.
247-
let _: __AssertEq<T >;
248-
let _: __AssertEq<std::marker::PhantomData<U> >;
249-
}
250-
}
259+
{ }
251260

252261
#[automatically_derived]
253262
impl<T, U> ::core::hash::Hash for Test<T, U>
@@ -377,21 +386,32 @@ fn check_multiple_trait_bounds() -> Result<()> {
377386
}
378387
}
379388

389+
const _: () = {
390+
trait DeriveWhereAssertEq {
391+
fn assert(&self);
392+
}
393+
394+
impl<T, U, V> DeriveWhereAssertEq for Test<T, U, V>
395+
where
396+
T: ::core::cmp::Eq,
397+
U: ::core::cmp::Eq
398+
{
399+
fn assert(&self) {
400+
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
401+
402+
// For some reason the comparison fails without the extra space at the end.
403+
let _: __AssertEq<T >;
404+
let _: __AssertEq<std::marker::PhantomData<(U, V)> >;
405+
}
406+
}
407+
};
408+
380409
#[automatically_derived]
381410
impl<T, U, V> ::core::cmp::Eq for Test<T, U, V>
382411
where
383412
T: ::core::cmp::Eq,
384413
U: ::core::cmp::Eq
385-
{
386-
#[inline]
387-
fn assert_receiver_is_total_eq(&self) {
388-
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
389-
390-
// For some reason the comparison fails without the extra space at the end.
391-
let _: __AssertEq<T >;
392-
let _: __AssertEq<std::marker::PhantomData<(U, V)> >;
393-
}
394-
}
414+
{ }
395415

396416
#[automatically_derived]
397417
impl<T, U, V> ::core::hash::Hash for Test<T, U, V>

src/trait_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ pub trait TraitImpl: Deref<Target = Trait> {
292292
None
293293
}
294294

295-
/// Trait to implement. Only used for [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html)
296-
/// because it implements [`Drop`] and not itself.
295+
/// Trait to implement. Only used by [`Eq`] and
296+
/// [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html).
297297
fn impl_item(
298298
&self,
299299
imp: &ImplGenerics<'_>,

src/trait_/eq.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! [`Eq`](trait@std::cmp::Eq) implementation.
22
3-
use std::ops::Deref;
3+
use std::{borrow::Cow, ops::Deref};
44

55
use proc_macro2::TokenStream;
66
use quote::quote;
7+
use syn::{Ident, ImplGenerics, Path, TypeGenerics, WhereClause};
78

89
use crate::{util, Data, DeriveTrait, DeriveWhere, Item, SplitGenerics, Trait, TraitImpl};
910

@@ -19,10 +20,37 @@ impl TraitImpl for Eq {
1920
DeriveTrait::Eq
2021
}
2122

22-
fn path(&self) -> syn::Path {
23+
fn path(&self) -> Path {
2324
util::path_from_strs(&["core", "cmp", "Eq"])
2425
}
2526

27+
fn additional_impl(&self) -> Option<(Path, TokenStream)> {
28+
Some((self.path(), quote! {}))
29+
}
30+
31+
fn impl_item(
32+
&self,
33+
imp: &ImplGenerics<'_>,
34+
ident: &Ident,
35+
ty: &TypeGenerics<'_>,
36+
where_clause: &Option<Cow<'_, WhereClause>>,
37+
body: TokenStream,
38+
) -> TokenStream {
39+
quote! {
40+
const _: () = {
41+
trait DeriveWhereAssertEq {
42+
fn assert(&self);
43+
}
44+
45+
impl #imp DeriveWhereAssertEq for #ident #ty
46+
#where_clause
47+
{
48+
#body
49+
}
50+
};
51+
}
52+
}
53+
2654
fn build_signature(
2755
&self,
2856
_derive_where: &DeriveWhere,
@@ -31,8 +59,7 @@ impl TraitImpl for Eq {
3159
body: &TokenStream,
3260
) -> TokenStream {
3361
quote! {
34-
#[inline]
35-
fn assert_receiver_is_total_eq(&self) {
62+
fn assert(&self) {
3663
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
3764

3865
#body

0 commit comments

Comments
 (0)