1
- // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2
- #![ allow( static_mut_refs) ]
3
-
4
1
use core:: num:: NonZero ;
5
2
use std:: assert_matches:: assert_matches;
6
3
use std:: collections:: TryReserveErrorKind :: * ;
@@ -14,6 +11,7 @@ use Taggy::*;
14
11
use Taggypar :: * ;
15
12
16
13
use crate :: hash;
14
+ use crate :: testing:: macros:: struct_with_counted_drop;
17
15
18
16
#[ test]
19
17
fn test_simple ( ) {
@@ -719,15 +717,7 @@ fn test_show() {
719
717
720
718
#[ test]
721
719
fn test_drop ( ) {
722
- static mut DROPS : i32 = 0 ;
723
- struct Elem ;
724
- impl Drop for Elem {
725
- fn drop ( & mut self ) {
726
- unsafe {
727
- DROPS += 1 ;
728
- }
729
- }
730
- }
720
+ struct_with_counted_drop ! ( Elem , DROPS ) ;
731
721
732
722
let mut ring = VecDeque :: new ( ) ;
733
723
ring. push_back ( Elem ) ;
@@ -736,20 +726,12 @@ fn test_drop() {
736
726
ring. push_front ( Elem ) ;
737
727
drop ( ring) ;
738
728
739
- assert_eq ! ( unsafe { DROPS } , 4 ) ;
729
+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
740
730
}
741
731
742
732
#[ test]
743
733
fn test_drop_with_pop ( ) {
744
- static mut DROPS : i32 = 0 ;
745
- struct Elem ;
746
- impl Drop for Elem {
747
- fn drop ( & mut self ) {
748
- unsafe {
749
- DROPS += 1 ;
750
- }
751
- }
752
- }
734
+ struct_with_counted_drop ! ( Elem , DROPS ) ;
753
735
754
736
let mut ring = VecDeque :: new ( ) ;
755
737
ring. push_back ( Elem ) ;
@@ -759,54 +741,32 @@ fn test_drop_with_pop() {
759
741
760
742
drop ( ring. pop_back ( ) ) ;
761
743
drop ( ring. pop_front ( ) ) ;
762
- assert_eq ! ( unsafe { DROPS } , 2 ) ;
744
+ assert_eq ! ( DROPS . get ( ) , 2 ) ;
763
745
764
746
drop ( ring) ;
765
- assert_eq ! ( unsafe { DROPS } , 4 ) ;
747
+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
766
748
}
767
749
768
750
#[ test]
769
751
fn test_drop_clear ( ) {
770
- static mut DROPS : i32 = 0 ;
771
- struct Elem ;
772
- impl Drop for Elem {
773
- fn drop ( & mut self ) {
774
- unsafe {
775
- DROPS += 1 ;
776
- }
777
- }
778
- }
752
+ struct_with_counted_drop ! ( Elem , DROPS ) ;
779
753
780
754
let mut ring = VecDeque :: new ( ) ;
781
755
ring. push_back ( Elem ) ;
782
756
ring. push_front ( Elem ) ;
783
757
ring. push_back ( Elem ) ;
784
758
ring. push_front ( Elem ) ;
785
759
ring. clear ( ) ;
786
- assert_eq ! ( unsafe { DROPS } , 4 ) ;
760
+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
787
761
788
762
drop ( ring) ;
789
- assert_eq ! ( unsafe { DROPS } , 4 ) ;
763
+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
790
764
}
791
765
792
766
#[ test]
793
767
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
794
768
fn test_drop_panic ( ) {
795
- static mut DROPS : i32 = 0 ;
796
-
797
- struct D ( bool ) ;
798
-
799
- impl Drop for D {
800
- fn drop ( & mut self ) {
801
- unsafe {
802
- DROPS += 1 ;
803
- }
804
-
805
- if self . 0 {
806
- panic ! ( "panic in `drop`" ) ;
807
- }
808
- }
809
- }
769
+ struct_with_counted_drop ! ( D ( bool ) , DROPS => |this: & D | if this. 0 { panic!( "panic in `drop`" ) ; } ) ;
810
770
811
771
let mut q = VecDeque :: new ( ) ;
812
772
q. push_back ( D ( false ) ) ;
@@ -820,7 +780,7 @@ fn test_drop_panic() {
820
780
821
781
catch_unwind ( move || drop ( q) ) . ok ( ) ;
822
782
823
- assert_eq ! ( unsafe { DROPS } , 8 ) ;
783
+ assert_eq ! ( DROPS . get ( ) , 8 ) ;
824
784
}
825
785
826
786
#[ test]
@@ -1655,21 +1615,7 @@ fn test_try_rfold_moves_iter() {
1655
1615
#[ test]
1656
1616
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
1657
1617
fn truncate_leak ( ) {
1658
- static mut DROPS : i32 = 0 ;
1659
-
1660
- struct D ( bool ) ;
1661
-
1662
- impl Drop for D {
1663
- fn drop ( & mut self ) {
1664
- unsafe {
1665
- DROPS += 1 ;
1666
- }
1667
-
1668
- if self . 0 {
1669
- panic ! ( "panic in `drop`" ) ;
1670
- }
1671
- }
1672
- }
1618
+ struct_with_counted_drop ! ( D ( bool ) , DROPS => |this: & D | if this. 0 { panic!( "panic in `drop`" ) ; } ) ;
1673
1619
1674
1620
let mut q = VecDeque :: new ( ) ;
1675
1621
q. push_back ( D ( false ) ) ;
@@ -1683,27 +1629,13 @@ fn truncate_leak() {
1683
1629
1684
1630
catch_unwind ( AssertUnwindSafe ( || q. truncate ( 1 ) ) ) . ok ( ) ;
1685
1631
1686
- assert_eq ! ( unsafe { DROPS } , 7 ) ;
1632
+ assert_eq ! ( DROPS . get ( ) , 7 ) ;
1687
1633
}
1688
1634
1689
1635
#[ test]
1690
1636
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
1691
1637
fn truncate_front_leak ( ) {
1692
- static mut DROPS : i32 = 0 ;
1693
-
1694
- struct D ( bool ) ;
1695
-
1696
- impl Drop for D {
1697
- fn drop ( & mut self ) {
1698
- unsafe {
1699
- DROPS += 1 ;
1700
- }
1701
-
1702
- if self . 0 {
1703
- panic ! ( "panic in `drop`" ) ;
1704
- }
1705
- }
1706
- }
1638
+ struct_with_counted_drop ! ( D ( bool ) , DROPS => |this: & D | if this. 0 { panic!( "panic in `drop`" ) ; } ) ;
1707
1639
1708
1640
let mut q = VecDeque :: new ( ) ;
1709
1641
q. push_back ( D ( false ) ) ;
@@ -1717,28 +1649,13 @@ fn truncate_front_leak() {
1717
1649
1718
1650
catch_unwind ( AssertUnwindSafe ( || q. truncate_front ( 1 ) ) ) . ok ( ) ;
1719
1651
1720
- assert_eq ! ( unsafe { DROPS } , 7 ) ;
1652
+ assert_eq ! ( DROPS . get ( ) , 7 ) ;
1721
1653
}
1722
1654
1723
1655
#[ test]
1724
1656
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
1725
1657
fn test_drain_leak ( ) {
1726
- static mut DROPS : i32 = 0 ;
1727
-
1728
- #[ derive( Debug , PartialEq ) ]
1729
- struct D ( u32 , bool ) ;
1730
-
1731
- impl Drop for D {
1732
- fn drop ( & mut self ) {
1733
- unsafe {
1734
- DROPS += 1 ;
1735
- }
1736
-
1737
- if self . 1 {
1738
- panic ! ( "panic in `drop`" ) ;
1739
- }
1740
- }
1741
- }
1658
+ struct_with_counted_drop ! ( D ( u32 , bool ) , DROPS => |this: & D | if this. 1 { panic!( "panic in `drop`" ) ; } ) ;
1742
1659
1743
1660
let mut v = VecDeque :: new ( ) ;
1744
1661
v. push_back ( D ( 4 , false ) ) ;
@@ -1754,10 +1671,10 @@ fn test_drain_leak() {
1754
1671
} ) )
1755
1672
. ok ( ) ;
1756
1673
1757
- assert_eq ! ( unsafe { DROPS } , 4 ) ;
1674
+ assert_eq ! ( DROPS . get ( ) , 4 ) ;
1758
1675
assert_eq ! ( v. len( ) , 3 ) ;
1759
1676
drop ( v) ;
1760
- assert_eq ! ( unsafe { DROPS } , 7 ) ;
1677
+ assert_eq ! ( DROPS . get ( ) , 7 ) ;
1761
1678
}
1762
1679
1763
1680
#[ test]
0 commit comments