@@ -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 ,
@@ -539,7 +536,7 @@ pub struct PeekMutInner<'a, T, K, S>
539
536
where
540
537
T : Ord ,
541
538
K : Kind ,
542
- S : Storage ,
539
+ S : VecStorage < T > + ? Sized ,
543
540
{
544
541
heap : & ' a mut BinaryHeapInner < T , K , S > ,
545
542
sift : bool ,
@@ -550,20 +547,20 @@ where
550
547
///
551
548
/// This `struct` is created by [`BinaryHeap::peek_mut`].
552
549
/// See its documentation for more.
553
- pub type PeekMut < ' a , T , K , const N : usize > = PeekMutInner < ' a , T , K , OwnedStorage < N > > ;
550
+ pub type PeekMut < ' a , T , K , const N : usize > = PeekMutInner < ' a , T , K , OwnedVecStorage < T , N > > ;
554
551
555
552
/// Structure wrapping a mutable reference to the greatest item on a
556
553
/// `BinaryHeap`.
557
554
///
558
555
/// This `struct` is created by [`BinaryHeapView::peek_mut`].
559
556
/// See its documentation for more.
560
- pub type PeekMutView < ' a , T , K > = PeekMutInner < ' a , T , K , ViewStorage > ;
557
+ pub type PeekMutView < ' a , T , K > = PeekMutInner < ' a , T , K , ViewVecStorage < T > > ;
561
558
562
559
impl < T , K , S > Drop for PeekMutInner < ' _ , T , K , S >
563
560
where
564
561
T : Ord ,
565
562
K : Kind ,
566
- S : Storage ,
563
+ S : VecStorage < T > + ? Sized ,
567
564
{
568
565
fn drop ( & mut self ) {
569
566
if self . sift {
@@ -576,7 +573,7 @@ impl<T, K, S> Deref for PeekMutInner<'_, T, K, S>
576
573
where
577
574
T : Ord ,
578
575
K : Kind ,
579
- S : Storage ,
576
+ S : VecStorage < T > + ? Sized ,
580
577
{
581
578
type Target = T ;
582
579
fn deref ( & self ) -> & T {
@@ -590,7 +587,7 @@ impl<T, K, S> DerefMut for PeekMutInner<'_, T, K, S>
590
587
where
591
588
T : Ord ,
592
589
K : Kind ,
593
- S : Storage ,
590
+ S : VecStorage < T > + ? Sized ,
594
591
{
595
592
fn deref_mut ( & mut self ) -> & mut T {
596
593
debug_assert ! ( !self . heap. is_empty( ) ) ;
@@ -603,7 +600,7 @@ impl<'a, T, K, S> PeekMutInner<'a, T, K, S>
603
600
where
604
601
T : Ord ,
605
602
K : Kind ,
606
- S : Storage ,
603
+ S : VecStorage < T > + ? Sized ,
607
604
{
608
605
/// Removes the peeked value from the heap and returns it.
609
606
pub fn pop ( mut this : PeekMutInner < ' a , T , K , S > ) -> T {
@@ -651,7 +648,7 @@ impl<T, K, S> fmt::Debug for BinaryHeapInner<T, K, S>
651
648
where
652
649
K : Kind ,
653
650
T : Ord + fmt:: Debug ,
654
- S : Storage ,
651
+ S : VecStorage < T > + ? Sized ,
655
652
{
656
653
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
657
654
f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
@@ -662,7 +659,7 @@ impl<'a, T, K, S> IntoIterator for &'a BinaryHeapInner<T, K, S>
662
659
where
663
660
K : Kind ,
664
661
T : Ord ,
665
- S : Storage ,
662
+ S : VecStorage < T > + ? Sized ,
666
663
{
667
664
type Item = & ' a T ;
668
665
type IntoIter = slice:: Iter < ' a , T > ;
@@ -676,7 +673,7 @@ where
676
673
mod tests {
677
674
use static_assertions:: assert_not_impl_any;
678
675
679
- use super :: { BinaryHeap , Max , Min } ;
676
+ use super :: { BinaryHeap , BinaryHeapView , Max , Min } ;
680
677
681
678
// Ensure a `BinaryHeap` containing `!Send` values stays `!Send` itself.
682
679
assert_not_impl_any ! ( BinaryHeap <* const ( ) , Max , 4 >: Send ) ;
@@ -849,4 +846,13 @@ mod tests {
849
846
assert_eq ! ( heap. pop( ) , Some ( 1 ) ) ;
850
847
assert_eq ! ( heap. pop( ) , None ) ;
851
848
}
849
+
850
+ fn _test_variance < ' a : ' b , ' b > ( x : BinaryHeap < & ' a ( ) , Max , 42 > ) -> BinaryHeap < & ' b ( ) , Max , 42 > {
851
+ x
852
+ }
853
+ fn _test_variance_view < ' a : ' b , ' b , ' c > (
854
+ x : & ' c BinaryHeapView < & ' a ( ) , Max > ,
855
+ ) -> & ' c BinaryHeapView < & ' b ( ) , Max > {
856
+ x
857
+ }
852
858
}
0 commit comments