@@ -386,6 +386,54 @@ fn repr_c() -> Result<()> {
386
386
)
387
387
}
388
388
389
+ #[ test]
390
+ fn repr_c_without_discriminant ( ) -> Result < ( ) > {
391
+ #[ cfg( feature = "nightly" ) ]
392
+ let partial_ord = quote ! {
393
+ :: core:: cmp:: PartialOrd :: partial_cmp(
394
+ & :: core:: intrinsics:: discriminant_value( self ) ,
395
+ & :: core:: intrinsics:: discriminant_value( __other) ,
396
+ )
397
+ } ;
398
+ #[ cfg( not( feature = "nightly" ) ) ]
399
+ let partial_ord = quote ! {
400
+ fn __discriminant( __this: & Test ) -> isize {
401
+ match __this {
402
+ Test :: A => 0 ,
403
+ Test :: B => 1 ,
404
+ Test :: C => 2
405
+ }
406
+ }
407
+
408
+ :: core:: cmp:: PartialOrd :: partial_cmp( & __discriminant( self ) , & __discriminant( __other) )
409
+ } ;
410
+
411
+ test_derive (
412
+ quote ! {
413
+ #[ derive_where( PartialOrd ) ]
414
+ #[ repr( C ) ]
415
+ enum Test {
416
+ A ,
417
+ B ,
418
+ #[ derive_where( incomparable) ]
419
+ C ,
420
+ }
421
+ } ,
422
+ quote ! {
423
+ impl :: core:: cmp:: PartialOrd for Test {
424
+ #[ inline]
425
+ fn partial_cmp( & self , __other: & Self ) -> :: core:: option:: Option <:: core:: cmp:: Ordering > {
426
+ if :: core:: matches!( self , Test :: C ) || :: core:: matches!( __other, Test :: C ) {
427
+ return :: core:: option:: Option :: None ;
428
+ }
429
+
430
+ #partial_ord
431
+ }
432
+ }
433
+ } ,
434
+ )
435
+ }
436
+
389
437
#[ test]
390
438
fn repr_c_clone ( ) -> Result < ( ) > {
391
439
#[ cfg( feature = "nightly" ) ]
@@ -440,6 +488,57 @@ fn repr_c_clone() -> Result<()> {
440
488
)
441
489
}
442
490
491
+ #[ test]
492
+ fn repr_c_clone_without_discriminant ( ) -> Result < ( ) > {
493
+ #[ cfg( feature = "nightly" ) ]
494
+ let partial_ord = quote ! {
495
+ :: core:: cmp:: PartialOrd :: partial_cmp(
496
+ & :: core:: intrinsics:: discriminant_value( self ) ,
497
+ & :: core:: intrinsics:: discriminant_value( __other) ,
498
+ )
499
+ } ;
500
+ #[ cfg( not( feature = "nightly" ) ) ]
501
+ let partial_ord = quote ! {
502
+ :: core:: cmp:: PartialOrd :: partial_cmp( & ( :: core:: clone:: Clone :: clone( self ) as isize ) , & ( :: core:: clone:: Clone :: clone( __other) as isize ) )
503
+ } ;
504
+
505
+ test_derive (
506
+ quote ! {
507
+ #[ derive_where( Clone , PartialOrd ) ]
508
+ #[ repr( C ) ]
509
+ enum Test {
510
+ A ,
511
+ B ,
512
+ #[ derive_where( incomparable) ]
513
+ C ,
514
+ }
515
+ } ,
516
+ quote ! {
517
+ impl :: core:: clone:: Clone for Test {
518
+ #[ inline]
519
+ fn clone( & self ) -> Self {
520
+ match self {
521
+ Test :: A => Test :: A ,
522
+ Test :: B => Test :: B ,
523
+ Test :: C => Test :: C ,
524
+ }
525
+ }
526
+ }
527
+
528
+ impl :: core:: cmp:: PartialOrd for Test {
529
+ #[ inline]
530
+ fn partial_cmp( & self , __other: & Self ) -> :: core:: option:: Option <:: core:: cmp:: Ordering > {
531
+ if :: core:: matches!( self , Test :: C ) || :: core:: matches!( __other, Test :: C ) {
532
+ return :: core:: option:: Option :: None ;
533
+ }
534
+
535
+ #partial_ord
536
+ }
537
+ }
538
+ } ,
539
+ )
540
+ }
541
+
443
542
#[ test]
444
543
fn repr_c_copy ( ) -> Result < ( ) > {
445
544
#[ cfg( feature = "nightly" ) ]
@@ -485,6 +584,48 @@ fn repr_c_copy() -> Result<()> {
485
584
)
486
585
}
487
586
587
+ #[ test]
588
+ fn repr_c_copy_without_discriminant ( ) -> Result < ( ) > {
589
+ #[ cfg( feature = "nightly" ) ]
590
+ let partial_ord = quote ! {
591
+ :: core:: cmp:: PartialOrd :: partial_cmp(
592
+ & :: core:: intrinsics:: discriminant_value( self ) ,
593
+ & :: core:: intrinsics:: discriminant_value( __other) ,
594
+ )
595
+ } ;
596
+ #[ cfg( not( feature = "nightly" ) ) ]
597
+ let partial_ord = quote ! {
598
+ :: core:: cmp:: PartialOrd :: partial_cmp( & ( * self as isize ) , & ( * __other as isize ) )
599
+ } ;
600
+
601
+ test_derive (
602
+ quote ! {
603
+ #[ derive_where( Copy , PartialOrd ) ]
604
+ #[ repr( C ) ]
605
+ enum Test {
606
+ A ,
607
+ B ,
608
+ #[ derive_where( incomparable) ]
609
+ C ,
610
+ }
611
+ } ,
612
+ quote ! {
613
+ impl :: core:: marker:: Copy for Test { }
614
+
615
+ impl :: core:: cmp:: PartialOrd for Test {
616
+ #[ inline]
617
+ fn partial_cmp( & self , __other: & Self ) -> :: core:: option:: Option <:: core:: cmp:: Ordering > {
618
+ if :: core:: matches!( self , Test :: C ) || :: core:: matches!( __other, Test :: C ) {
619
+ return :: core:: option:: Option :: None ;
620
+ }
621
+
622
+ #partial_ord
623
+ }
624
+ }
625
+ } ,
626
+ )
627
+ }
628
+
488
629
#[ test]
489
630
fn repr_c_reverse ( ) -> Result < ( ) > {
490
631
#[ cfg( feature = "nightly" ) ]
@@ -1111,14 +1252,7 @@ fn repr_clone() -> Result<()> {
1111
1252
& :: core:: intrinsics:: discriminant_value( __other) ,
1112
1253
)
1113
1254
} ;
1114
- #[ cfg( not( any( feature = "nightly" , feature = "safe" ) ) ) ]
1115
- let partial_ord = quote ! {
1116
- :: core:: cmp:: PartialOrd :: partial_cmp(
1117
- & unsafe { * <* const _>:: from( self ) . cast:: <u64 >( ) } ,
1118
- & unsafe { * <* const _>:: from( __other) . cast:: <u64 >( ) } ,
1119
- )
1120
- } ;
1121
- #[ cfg( all( not( feature = "nightly" ) , feature = "safe" ) ) ]
1255
+ #[ cfg( not( feature = "nightly" ) ) ]
1122
1256
let partial_ord = quote ! {
1123
1257
:: core:: cmp:: PartialOrd :: partial_cmp( & ( :: core:: clone:: Clone :: clone( self ) as u64 ) , & ( :: core:: clone:: Clone :: clone( __other) as u64 ) )
1124
1258
} ;
@@ -1169,14 +1303,7 @@ fn repr_copy() -> Result<()> {
1169
1303
& :: core:: intrinsics:: discriminant_value( __other) ,
1170
1304
)
1171
1305
} ;
1172
- #[ cfg( not( any( feature = "nightly" , feature = "safe" ) ) ) ]
1173
- let partial_ord = quote ! {
1174
- :: core:: cmp:: PartialOrd :: partial_cmp(
1175
- & unsafe { * <* const _>:: from( self ) . cast:: <u64 >( ) } ,
1176
- & unsafe { * <* const _>:: from( __other) . cast:: <u64 >( ) } ,
1177
- )
1178
- } ;
1179
- #[ cfg( all( not( feature = "nightly" ) , feature = "safe" ) ) ]
1306
+ #[ cfg( not( feature = "nightly" ) ) ]
1180
1307
let partial_ord = quote ! {
1181
1308
:: core:: cmp:: PartialOrd :: partial_cmp( & ( * self as u64 ) , & ( * __other as u64 ) )
1182
1309
} ;
0 commit comments