Skip to content

Commit beb8ee8

Browse files
authored
Remove C validation (#83)
1 parent b383ea0 commit beb8ee8

File tree

4 files changed

+63
-205
lines changed

4 files changed

+63
-205
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- `no_drop` item-level option to `ZeroizeOnDrop` which does not implement
1212
`Drop` but instead only asserts that every field implements `ZeroizeOnDrop`.
1313

14+
### Changed
15+
- Remove unnecessary validation of the default discriminant type for enums.
16+
1417
### Fixed
1518
- Stop depending on unstable APIs for `Eq` for `ZeroizeOnDrop`.
1619

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@ supported.
278278

279279
Unions only support [`Clone`] and [`Copy`].
280280

281-
[`PartialOrd`] and [`Ord`] need to determine the discriminant type to
282-
function correctly. To protect against a potential future change to the
283-
default discriminant type, some compile-time validation is inserted to
284-
ascertain that the type remains `isize`.
285-
286281
### `no_std` support
287282

288283
`no_std` support is provided by default.

src/test/discriminant.rs

Lines changed: 36 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ fn default() -> Result<()> {
1515
#[cfg(not(feature = "nightly"))]
1616
let partial_ord = quote! {
1717
const fn __discriminant(__this: &Test) -> isize {
18-
const __VALIDATE_ISIZE_A: isize = 0;
19-
const __VALIDATE_ISIZE_B: isize = (0) + 1;
20-
const __VALIDATE_ISIZE_C: isize = (0) + 2;
21-
2218
match __this {
23-
Test::A => __VALIDATE_ISIZE_A,
24-
Test::B => __VALIDATE_ISIZE_B,
25-
Test::C => __VALIDATE_ISIZE_C
19+
Test::A => 0,
20+
Test::B => (0) + 1,
21+
Test::C => (0) + 2
2622
}
2723
}
2824

@@ -66,10 +62,6 @@ fn default_clone() -> Result<()> {
6662
};
6763
#[cfg(not(feature = "nightly"))]
6864
let partial_ord = quote! {
69-
const __VALIDATE_ISIZE_A: isize = 0;
70-
const __VALIDATE_ISIZE_B: isize = (0) + 1;
71-
const __VALIDATE_ISIZE_C: isize = (0) + 2;
72-
7365
::core::cmp::PartialOrd::partial_cmp(&(::core::clone::Clone::clone(self) as isize), &(::core::clone::Clone::clone(__other) as isize))
7466
};
7567

@@ -122,10 +114,6 @@ fn default_copy() -> Result<()> {
122114
};
123115
#[cfg(not(feature = "nightly"))]
124116
let partial_ord = quote! {
125-
const __VALIDATE_ISIZE_A: isize = 0;
126-
const __VALIDATE_ISIZE_B: isize = (0) + 1;
127-
const __VALIDATE_ISIZE_C: isize = (0) + 2;
128-
129117
::core::cmp::PartialOrd::partial_cmp(&(*self as isize), &(*__other as isize))
130118
};
131119

@@ -170,14 +158,10 @@ fn default_reverse() -> Result<()> {
170158
#[cfg(not(feature = "nightly"))]
171159
let partial_ord = quote! {
172160
const fn __discriminant(__this: &Test) -> isize {
173-
const __VALIDATE_ISIZE_A: isize = 2;
174-
const __VALIDATE_ISIZE_B: isize = 1;
175-
const __VALIDATE_ISIZE_C: isize = 0;
176-
177161
match __this {
178-
Test::A => __VALIDATE_ISIZE_A,
179-
Test::B => __VALIDATE_ISIZE_B,
180-
Test::C => __VALIDATE_ISIZE_C
162+
Test::A => 2,
163+
Test::B => 1,
164+
Test::C => 0
181165
}
182166
}
183167

@@ -222,16 +206,11 @@ fn default_mix() -> Result<()> {
222206
#[cfg(not(feature = "nightly"))]
223207
let partial_ord = quote! {
224208
const fn __discriminant(__this: &Test) -> isize {
225-
const __VALIDATE_ISIZE_A: isize = 1;
226-
const __VALIDATE_ISIZE_B: isize = 0;
227-
const __VALIDATE_ISIZE_C: isize = 2;
228-
const __VALIDATE_ISIZE_D: isize = (2) + 1;
229-
230209
match __this {
231-
Test::A => __VALIDATE_ISIZE_A,
232-
Test::B => __VALIDATE_ISIZE_B,
233-
Test::C => __VALIDATE_ISIZE_C,
234-
Test::D => __VALIDATE_ISIZE_D
210+
Test::A => 1,
211+
Test::B => 0,
212+
Test::C => 2,
213+
Test::D => (2) + 1
235214
}
236215
}
237216

@@ -277,18 +256,12 @@ fn default_skip() -> Result<()> {
277256
#[cfg(not(feature = "nightly"))]
278257
let partial_ord = quote! {
279258
const fn __discriminant(__this: &Test) -> isize {
280-
const __VALIDATE_ISIZE_A: isize = 0;
281-
const __VALIDATE_ISIZE_B: isize = 3;
282-
const __VALIDATE_ISIZE_C: isize = (3) + 1;
283-
const __VALIDATE_ISIZE_D: isize = (3) + 2;
284-
const __VALIDATE_ISIZE_E: isize = (3) + 3;
285-
286259
match __this {
287-
Test::A => __VALIDATE_ISIZE_A,
288-
Test::B => __VALIDATE_ISIZE_B,
289-
Test::C => __VALIDATE_ISIZE_C,
290-
Test::D => __VALIDATE_ISIZE_D,
291-
Test::E => __VALIDATE_ISIZE_E
260+
Test::A => 0,
261+
Test::B => 3,
262+
Test::C => (3) + 1,
263+
Test::D => (3) + 2,
264+
Test::E => (3) + 3
292265
}
293266
}
294267

@@ -335,14 +308,10 @@ fn default_expr() -> Result<()> {
335308
#[cfg(not(feature = "nightly"))]
336309
let partial_ord = quote! {
337310
const fn __discriminant(__this: &Test) -> isize {
338-
const __VALIDATE_ISIZE_A: isize = isize::MAX - 2;
339-
const __VALIDATE_ISIZE_B: isize = (isize::MAX - 2) + 1;
340-
const __VALIDATE_ISIZE_C: isize = (isize::MAX - 2) + 2;
341-
342311
match __this {
343-
Test::A => __VALIDATE_ISIZE_A,
344-
Test::B => __VALIDATE_ISIZE_B,
345-
Test::C => __VALIDATE_ISIZE_C
312+
Test::A => isize::MAX - 2,
313+
Test::B => (isize::MAX - 2) + 1,
314+
Test::C => (isize::MAX - 2) + 2
346315
}
347316
}
348317

@@ -387,14 +356,10 @@ fn repr_c() -> Result<()> {
387356
#[cfg(not(feature = "nightly"))]
388357
let partial_ord = quote! {
389358
const fn __discriminant(__this: &Test) -> isize {
390-
const __VALIDATE_ISIZE_A: isize = 0;
391-
const __VALIDATE_ISIZE_B: isize = (0) + 1;
392-
const __VALIDATE_ISIZE_C: isize = (0) + 2;
393-
394359
match __this {
395-
Test::A => __VALIDATE_ISIZE_A,
396-
Test::B => __VALIDATE_ISIZE_B,
397-
Test::C => __VALIDATE_ISIZE_C
360+
Test::A => 0,
361+
Test::B => (0) + 1,
362+
Test::C => (0) + 2
398363
}
399364
}
400365

@@ -488,10 +453,6 @@ fn repr_c_clone() -> Result<()> {
488453
};
489454
#[cfg(not(feature = "nightly"))]
490455
let partial_ord = quote! {
491-
const __VALIDATE_ISIZE_A: isize = 0;
492-
const __VALIDATE_ISIZE_B: isize = (0) + 1;
493-
const __VALIDATE_ISIZE_C: isize = (0) + 2;
494-
495456
::core::cmp::PartialOrd::partial_cmp(&(::core::clone::Clone::clone(self) as isize), &(::core::clone::Clone::clone(__other) as isize))
496457
};
497458

@@ -598,10 +559,6 @@ fn repr_c_copy() -> Result<()> {
598559
};
599560
#[cfg(not(feature = "nightly"))]
600561
let partial_ord = quote! {
601-
const __VALIDATE_ISIZE_A: isize = 0;
602-
const __VALIDATE_ISIZE_B: isize = (0) + 1;
603-
const __VALIDATE_ISIZE_C: isize = (0) + 2;
604-
605562
::core::cmp::PartialOrd::partial_cmp(&(*self as isize), &(*__other as isize))
606563
};
607564

@@ -691,14 +648,10 @@ fn repr_c_reverse() -> Result<()> {
691648
#[cfg(not(feature = "nightly"))]
692649
let partial_ord = quote! {
693650
const fn __discriminant(__this: &Test) -> isize {
694-
const __VALIDATE_ISIZE_A: isize = 2;
695-
const __VALIDATE_ISIZE_B: isize = 1;
696-
const __VALIDATE_ISIZE_C: isize = 0;
697-
698651
match __this {
699-
Test::A => __VALIDATE_ISIZE_A,
700-
Test::B => __VALIDATE_ISIZE_B,
701-
Test::C => __VALIDATE_ISIZE_C
652+
Test::A => 2,
653+
Test::B => 1,
654+
Test::C => 0
702655
}
703656
}
704657

@@ -744,16 +697,11 @@ fn repr_c_mix() -> Result<()> {
744697
#[cfg(not(feature = "nightly"))]
745698
let partial_ord = quote! {
746699
const fn __discriminant(__this: &Test) -> isize {
747-
const __VALIDATE_ISIZE_A: isize = 1;
748-
const __VALIDATE_ISIZE_B: isize = 0;
749-
const __VALIDATE_ISIZE_C: isize = 2;
750-
const __VALIDATE_ISIZE_D: isize = (2) + 1;
751-
752700
match __this {
753-
Test::A => __VALIDATE_ISIZE_A,
754-
Test::B => __VALIDATE_ISIZE_B,
755-
Test::C => __VALIDATE_ISIZE_C,
756-
Test::D => __VALIDATE_ISIZE_D
701+
Test::A => 1,
702+
Test::B => 0,
703+
Test::C => 2,
704+
Test::D => (2) + 1
757705
}
758706
}
759707

@@ -800,18 +748,12 @@ fn repr_c_skip() -> Result<()> {
800748
#[cfg(not(feature = "nightly"))]
801749
let partial_ord = quote! {
802750
const fn __discriminant(__this: &Test) -> isize {
803-
const __VALIDATE_ISIZE_A: isize = 0;
804-
const __VALIDATE_ISIZE_B: isize = 3;
805-
const __VALIDATE_ISIZE_C: isize = (3) + 1;
806-
const __VALIDATE_ISIZE_D: isize = (3) + 2;
807-
const __VALIDATE_ISIZE_E: isize = (3) + 3;
808-
809751
match __this {
810-
Test::A => __VALIDATE_ISIZE_A,
811-
Test::B => __VALIDATE_ISIZE_B,
812-
Test::C => __VALIDATE_ISIZE_C,
813-
Test::D => __VALIDATE_ISIZE_D,
814-
Test::E => __VALIDATE_ISIZE_E
752+
Test::A => 0,
753+
Test::B => 3,
754+
Test::C => (3) + 1,
755+
Test::D => (3) + 2,
756+
Test::E => (3) + 3
815757
}
816758
}
817759

@@ -859,14 +801,10 @@ fn repr_c_expr() -> Result<()> {
859801
#[cfg(not(feature = "nightly"))]
860802
let partial_ord = quote! {
861803
const fn __discriminant(__this: &Test) -> isize {
862-
const __VALIDATE_ISIZE_A: isize = isize::MAX - 2;
863-
const __VALIDATE_ISIZE_B: isize = (isize::MAX - 2) + 1;
864-
const __VALIDATE_ISIZE_C: isize = (isize::MAX - 2) + 2;
865-
866804
match __this {
867-
Test::A => __VALIDATE_ISIZE_A,
868-
Test::B => __VALIDATE_ISIZE_B,
869-
Test::C => __VALIDATE_ISIZE_C
805+
Test::A => isize::MAX - 2,
806+
Test::B => (isize::MAX - 2) + 1,
807+
Test::C => (isize::MAX - 2) + 2
870808
}
871809
}
872810

0 commit comments

Comments
 (0)