File tree 2 files changed +13
-2
lines changed
src/collections/vec_deque
2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -2793,8 +2793,12 @@ impl<T> From<Vec<T>> for VecDeque<T> {
2793
2793
let len = other. len ( ) ;
2794
2794
2795
2795
// We need to extend the buf if it's not a power of two, too small
2796
- // or doesn't have at least one free space
2797
- if !buf. capacity ( ) . is_power_of_two ( )
2796
+ // or doesn't have at least one free space.
2797
+ // We check if `T` is a ZST in the first condition,
2798
+ // because `usize::MAX` (the capacity returned by `capacity()` for ZST)
2799
+ // is not a power of zero and thus it'll always try
2800
+ // to reserve more memory which will panic for ZST (rust-lang/rust#78532)
2801
+ if ( !buf. capacity ( ) . is_power_of_two ( ) && mem:: size_of :: < T > ( ) != 0 )
2798
2802
|| ( buf. capacity ( ) < ( MINIMUM_CAPACITY + 1 ) )
2799
2803
|| ( buf. capacity ( ) == len)
2800
2804
{
Original file line number Diff line number Diff line change @@ -1728,3 +1728,10 @@ fn test_zero_sized_push() {
1728
1728
}
1729
1729
}
1730
1730
}
1731
+
1732
+ #[ test]
1733
+ fn test_from_zero_sized_vec ( ) {
1734
+ let v = vec ! [ ( ) ; 100 ] ;
1735
+ let queue = VecDeque :: from ( v) ;
1736
+ assert ! ( queue. len( ) , 100 ) ;
1737
+ }
You can’t perform that action at this time.
0 commit comments