@@ -17,10 +17,7 @@ use core::{
17
17
ptr, slice,
18
18
} ;
19
19
20
- use crate :: {
21
- storage:: { OwnedStorage , Storage , ViewStorage } ,
22
- vec:: { Vec , VecInner } ,
23
- } ;
20
+ use crate :: vec:: { OwnedVecStorage , Vec , VecInner , VecStorage , ViewVecStorage } ;
24
21
25
22
/// Min-heap
26
23
pub enum Min { }
@@ -54,11 +51,11 @@ mod private {
54
51
impl private:: Sealed for Max { }
55
52
impl private:: Sealed for Min { }
56
53
57
- /// Base struct for [`BinaryHeap`] and [`BinaryHeapView`], generic over the [`Storage `].
54
+ /// Base struct for [`BinaryHeap`] and [`BinaryHeapView`], generic over the [`VecStorage `].
58
55
///
59
56
/// In most cases you should use [`BinaryHeap`] or [`BinaryHeapView`] directly. Only use this
60
57
/// struct if you want to write code that's generic over both.
61
- pub struct BinaryHeapInner < T , K , S : Storage > {
58
+ pub struct BinaryHeapInner < T , K , S : VecStorage < T > + ? Sized > {
62
59
pub ( crate ) _kind : PhantomData < K > ,
63
60
pub ( crate ) data : VecInner < T , S > ,
64
61
}
@@ -109,7 +106,7 @@ pub struct BinaryHeapInner<T, K, S: Storage> {
109
106
/// // The heap should now be empty.
110
107
/// assert!(heap.is_empty())
111
108
/// ```
112
- pub type BinaryHeap < T , K , const N : usize > = BinaryHeapInner < T , K , OwnedStorage < N > > ;
109
+ pub type BinaryHeap < T , K , const N : usize > = BinaryHeapInner < T , K , OwnedVecStorage < T , N > > ;
113
110
114
111
/// A priority queue implemented with a binary heap.
115
112
///
@@ -158,7 +155,7 @@ pub type BinaryHeap<T, K, const N: usize> = BinaryHeapInner<T, K, OwnedStorage<N
158
155
/// // The heap should now be empty.
159
156
/// assert!(heap.is_empty())
160
157
/// ```
161
- pub type BinaryHeapView < T , K > = BinaryHeapInner < T , K , ViewStorage > ;
158
+ pub type BinaryHeapView < T , K > = BinaryHeapInner < T , K , ViewVecStorage < T > > ;
162
159
163
160
impl < T , K , const N : usize > BinaryHeap < T , K , N > {
164
161
/* Constructors */
@@ -198,7 +195,7 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
198
195
}
199
196
}
200
197
201
- impl < T , K , S : Storage > BinaryHeapInner < T , K , S >
198
+ impl < T , K , S : VecStorage < T > + ? Sized > BinaryHeapInner < T , K , S >
202
199
where
203
200
T : Ord ,
204
201
K : Kind ,
@@ -519,7 +516,7 @@ pub struct PeekMutInner<'a, T, K, S>
519
516
where
520
517
T : Ord ,
521
518
K : Kind ,
522
- S : Storage ,
519
+ S : VecStorage < T > + ? Sized ,
523
520
{
524
521
heap : & ' a mut BinaryHeapInner < T , K , S > ,
525
522
sift : bool ,
@@ -530,20 +527,20 @@ where
530
527
///
531
528
/// This `struct` is created by [`BinaryHeap::peek_mut`].
532
529
/// See its documentation for more.
533
- pub type PeekMut < ' a , T , K , const N : usize > = PeekMutInner < ' a , T , K , OwnedStorage < N > > ;
530
+ pub type PeekMut < ' a , T , K , const N : usize > = PeekMutInner < ' a , T , K , OwnedVecStorage < T , N > > ;
534
531
535
532
/// Structure wrapping a mutable reference to the greatest item on a
536
533
/// `BinaryHeap`.
537
534
///
538
535
/// This `struct` is created by [`BinaryHeapView::peek_mut`].
539
536
/// See its documentation for more.
540
- pub type PeekMutView < ' a , T , K > = PeekMutInner < ' a , T , K , ViewStorage > ;
537
+ pub type PeekMutView < ' a , T , K > = PeekMutInner < ' a , T , K , ViewVecStorage < T > > ;
541
538
542
539
impl < T , K , S > Drop for PeekMutInner < ' _ , T , K , S >
543
540
where
544
541
T : Ord ,
545
542
K : Kind ,
546
- S : Storage ,
543
+ S : VecStorage < T > + ? Sized ,
547
544
{
548
545
fn drop ( & mut self ) {
549
546
if self . sift {
@@ -556,7 +553,7 @@ impl<T, K, S> Deref for PeekMutInner<'_, T, K, S>
556
553
where
557
554
T : Ord ,
558
555
K : Kind ,
559
- S : Storage ,
556
+ S : VecStorage < T > + ? Sized ,
560
557
{
561
558
type Target = T ;
562
559
fn deref ( & self ) -> & T {
@@ -570,7 +567,7 @@ impl<T, K, S> DerefMut for PeekMutInner<'_, T, K, S>
570
567
where
571
568
T : Ord ,
572
569
K : Kind ,
573
- S : Storage ,
570
+ S : VecStorage < T > + ? Sized ,
574
571
{
575
572
fn deref_mut ( & mut self ) -> & mut T {
576
573
debug_assert ! ( !self . heap. is_empty( ) ) ;
@@ -583,7 +580,7 @@ impl<'a, T, K, S> PeekMutInner<'a, T, K, S>
583
580
where
584
581
T : Ord ,
585
582
K : Kind ,
586
- S : Storage ,
583
+ S : VecStorage < T > + ? Sized ,
587
584
{
588
585
/// Removes the peeked value from the heap and returns it.
589
586
pub fn pop ( mut this : PeekMutInner < ' a , T , K , S > ) -> T {
@@ -631,7 +628,7 @@ impl<T, K, S> fmt::Debug for BinaryHeapInner<T, K, S>
631
628
where
632
629
K : Kind ,
633
630
T : Ord + fmt:: Debug ,
634
- S : Storage ,
631
+ S : VecStorage < T > + ? Sized ,
635
632
{
636
633
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
637
634
f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
@@ -642,7 +639,7 @@ impl<'a, T, K, S> IntoIterator for &'a BinaryHeapInner<T, K, S>
642
639
where
643
640
K : Kind ,
644
641
T : Ord ,
645
- S : Storage ,
642
+ S : VecStorage < T > + ? Sized ,
646
643
{
647
644
type Item = & ' a T ;
648
645
type IntoIter = slice:: Iter < ' a , T > ;
@@ -656,7 +653,7 @@ where
656
653
mod tests {
657
654
use static_assertions:: assert_not_impl_any;
658
655
659
- use super :: { BinaryHeap , Max , Min } ;
656
+ use super :: { BinaryHeap , BinaryHeapView , Max , Min } ;
660
657
661
658
// Ensure a `BinaryHeap` containing `!Send` values stays `!Send` itself.
662
659
assert_not_impl_any ! ( BinaryHeap <* const ( ) , Max , 4 >: Send ) ;
@@ -829,4 +826,13 @@ mod tests {
829
826
assert_eq ! ( heap. pop( ) , Some ( 1 ) ) ;
830
827
assert_eq ! ( heap. pop( ) , None ) ;
831
828
}
829
+
830
+ fn _test_variance < ' a : ' b , ' b > ( x : BinaryHeap < & ' a ( ) , Max , 42 > ) -> BinaryHeap < & ' b ( ) , Max , 42 > {
831
+ x
832
+ }
833
+ fn _test_variance_view < ' a : ' b , ' b , ' c > (
834
+ x : & ' c BinaryHeapView < & ' a ( ) , Max > ,
835
+ ) -> & ' c BinaryHeapView < & ' b ( ) , Max > {
836
+ x
837
+ }
832
838
}
0 commit comments