Skip to content

Commit b17927e

Browse files
Re-introduce covariance for containers
1 parent 1c47ffc commit b17927e

File tree

7 files changed

+484
-184
lines changed

7 files changed

+484
-184
lines changed

src/binary_heap.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ use core::{
1717
ptr, slice,
1818
};
1919

20-
use crate::{
21-
storage::{OwnedStorage, Storage, ViewStorage},
22-
vec::{Vec, VecInner},
23-
};
20+
use crate::vec::{OwnedVecStorage, Vec, VecInner, VecStorage, ViewVecStorage};
2421

2522
/// Min-heap
2623
pub enum Min {}
@@ -54,11 +51,11 @@ mod private {
5451
impl private::Sealed for Max {}
5552
impl private::Sealed for Min {}
5653

57-
/// Base struct for [`BinaryHeap`] and [`BinaryHeapView`], generic over the [`Storage`].
54+
/// Base struct for [`BinaryHeap`] and [`BinaryHeapView`], generic over the [`VecStorage`].
5855
///
5956
/// In most cases you should use [`BinaryHeap`] or [`BinaryHeapView`] directly. Only use this
6057
/// 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> {
6259
pub(crate) _kind: PhantomData<K>,
6360
pub(crate) data: VecInner<T, S>,
6461
}
@@ -109,7 +106,7 @@ pub struct BinaryHeapInner<T, K, S: Storage> {
109106
/// // The heap should now be empty.
110107
/// assert!(heap.is_empty())
111108
/// ```
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>>;
113110

114111
/// A priority queue implemented with a binary heap.
115112
///
@@ -158,7 +155,7 @@ pub type BinaryHeap<T, K, const N: usize> = BinaryHeapInner<T, K, OwnedStorage<N
158155
/// // The heap should now be empty.
159156
/// assert!(heap.is_empty())
160157
/// ```
161-
pub type BinaryHeapView<T, K> = BinaryHeapInner<T, K, ViewStorage>;
158+
pub type BinaryHeapView<T, K> = BinaryHeapInner<T, K, ViewVecStorage<T>>;
162159

163160
impl<T, K, const N: usize> BinaryHeap<T, K, N> {
164161
/* Constructors */
@@ -198,7 +195,7 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
198195
}
199196
}
200197

201-
impl<T, K, S: Storage> BinaryHeapInner<T, K, S>
198+
impl<T, K, S: VecStorage<T> + ?Sized> BinaryHeapInner<T, K, S>
202199
where
203200
T: Ord,
204201
K: Kind,
@@ -519,7 +516,7 @@ pub struct PeekMutInner<'a, T, K, S>
519516
where
520517
T: Ord,
521518
K: Kind,
522-
S: Storage,
519+
S: VecStorage<T> + ?Sized,
523520
{
524521
heap: &'a mut BinaryHeapInner<T, K, S>,
525522
sift: bool,
@@ -530,20 +527,20 @@ where
530527
///
531528
/// This `struct` is created by [`BinaryHeap::peek_mut`].
532529
/// 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>>;
534531

535532
/// Structure wrapping a mutable reference to the greatest item on a
536533
/// `BinaryHeap`.
537534
///
538535
/// This `struct` is created by [`BinaryHeapView::peek_mut`].
539536
/// 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>>;
541538

542539
impl<T, K, S> Drop for PeekMutInner<'_, T, K, S>
543540
where
544541
T: Ord,
545542
K: Kind,
546-
S: Storage,
543+
S: VecStorage<T> + ?Sized,
547544
{
548545
fn drop(&mut self) {
549546
if self.sift {
@@ -556,7 +553,7 @@ impl<T, K, S> Deref for PeekMutInner<'_, T, K, S>
556553
where
557554
T: Ord,
558555
K: Kind,
559-
S: Storage,
556+
S: VecStorage<T> + ?Sized,
560557
{
561558
type Target = T;
562559
fn deref(&self) -> &T {
@@ -570,7 +567,7 @@ impl<T, K, S> DerefMut for PeekMutInner<'_, T, K, S>
570567
where
571568
T: Ord,
572569
K: Kind,
573-
S: Storage,
570+
S: VecStorage<T> + ?Sized,
574571
{
575572
fn deref_mut(&mut self) -> &mut T {
576573
debug_assert!(!self.heap.is_empty());
@@ -583,7 +580,7 @@ impl<'a, T, K, S> PeekMutInner<'a, T, K, S>
583580
where
584581
T: Ord,
585582
K: Kind,
586-
S: Storage,
583+
S: VecStorage<T> + ?Sized,
587584
{
588585
/// Removes the peeked value from the heap and returns it.
589586
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>
631628
where
632629
K: Kind,
633630
T: Ord + fmt::Debug,
634-
S: Storage,
631+
S: VecStorage<T> + ?Sized,
635632
{
636633
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
637634
f.debug_list().entries(self.iter()).finish()
@@ -642,7 +639,7 @@ impl<'a, T, K, S> IntoIterator for &'a BinaryHeapInner<T, K, S>
642639
where
643640
K: Kind,
644641
T: Ord,
645-
S: Storage,
642+
S: VecStorage<T> + ?Sized,
646643
{
647644
type Item = &'a T;
648645
type IntoIter = slice::Iter<'a, T>;
@@ -656,7 +653,7 @@ where
656653
mod tests {
657654
use static_assertions::assert_not_impl_any;
658655

659-
use super::{BinaryHeap, Max, Min};
656+
use super::{BinaryHeap, BinaryHeapView, Max, Min};
660657

661658
// Ensure a `BinaryHeap` containing `!Send` values stays `!Send` itself.
662659
assert_not_impl_any!(BinaryHeap<*const (), Max, 4>: Send);
@@ -829,4 +826,13 @@ mod tests {
829826
assert_eq!(heap.pop(), Some(1));
830827
assert_eq!(heap.pop(), None);
831828
}
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+
}
832838
}

src/deque.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@
3333
//! }
3434
//! ```
3535
36-
use core::borrow::{Borrow, BorrowMut};
3736
use core::fmt;
3837
use core::iter::FusedIterator;
38+
use core::marker::PhantomData;
3939
use core::mem::MaybeUninit;
4040
use core::{ptr, slice};
4141

42-
use crate::storage::{OwnedStorage, Storage, ViewStorage};
42+
use crate::vec::{OwnedVecStorage, VecStorage, VecStorageInner, ViewVecStorage};
4343

44-
/// Base struct for [`Deque`] and [`Deque`], generic over the [`Storage`].
44+
/// Base struct for [`Deque`] and [`Deque`], generic over the [`VecStorage`].
4545
///
4646
/// In most cases you should use [`Deque`] or [`Deque`] directly. Only use this
4747
/// struct if you want to write code that's generic over both.
48-
pub struct DequeInner<T, S: Storage> {
48+
pub struct DequeInner<T, S: VecStorage<T> + ?Sized> {
49+
// This phantomdata is required because otherwise rustc thinks that `T` is not used
50+
phantom: PhantomData<T>,
4951
/// Front index. Always 0..=(N-1)
5052
front: usize,
5153
/// Back index. Always 0..=(N-1).
@@ -54,7 +56,7 @@ pub struct DequeInner<T, S: Storage> {
5456
/// Used to distinguish "empty" and "full" cases when `front == back`.
5557
/// May only be `true` if `front == back`, always `false` otherwise.
5658
full: bool,
57-
buffer: S::Buffer<MaybeUninit<T>>,
59+
buffer: S,
5860
}
5961

6062
/// A fixed capacity double-ended queue.
@@ -91,7 +93,7 @@ pub struct DequeInner<T, S: Storage> {
9193
/// println!("{}", x);
9294
/// }
9395
/// ```
94-
pub type Deque<T, const N: usize> = DequeInner<T, OwnedStorage<N>>;
96+
pub type Deque<T, const N: usize> = DequeInner<T, OwnedVecStorage<T, N>>;
9597

9698
/// A double-ended queue with dynamic capacity.
9799
///
@@ -130,7 +132,7 @@ pub type Deque<T, const N: usize> = DequeInner<T, OwnedStorage<N>>;
130132
/// println!("{}", x);
131133
/// }
132134
/// ```
133-
pub type DequeView<T> = DequeInner<T, ViewStorage>;
135+
pub type DequeView<T> = DequeInner<T, ViewVecStorage<T>>;
134136

135137
impl<T, const N: usize> Deque<T, N> {
136138
const INIT: MaybeUninit<T> = MaybeUninit::uninit();
@@ -153,7 +155,10 @@ impl<T, const N: usize> Deque<T, N> {
153155
crate::sealed::greater_than_0::<N>();
154156

155157
Self {
156-
buffer: [Self::INIT; N],
158+
phantom: PhantomData,
159+
buffer: VecStorageInner {
160+
buffer: [Self::INIT; N],
161+
},
157162
front: 0,
158163
back: 0,
159164
full: false,
@@ -191,7 +196,7 @@ impl<T, const N: usize> Deque<T, N> {
191196
}
192197
}
193198

194-
impl<T, S: Storage> DequeInner<T, S> {
199+
impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
195200
/// Returns the maximum number of elements the deque can hold.
196201
pub fn storage_capacity(&self) -> usize {
197202
self.buffer.borrow().len()
@@ -794,29 +799,29 @@ impl<T, const N: usize> Default for Deque<T, N> {
794799
}
795800
}
796801

797-
impl<T, S: Storage> Drop for DequeInner<T, S> {
802+
impl<T, S: VecStorage<T> + ?Sized> Drop for DequeInner<T, S> {
798803
fn drop(&mut self) {
799804
// safety: `self` is left in an inconsistent state but it doesn't matter since
800805
// it's getting dropped. Nothing should be able to observe `self` after drop.
801806
unsafe { self.drop_contents() }
802807
}
803808
}
804809

805-
impl<T: fmt::Debug, S: Storage> fmt::Debug for DequeInner<T, S> {
810+
impl<T: fmt::Debug, S: VecStorage<T> + ?Sized> fmt::Debug for DequeInner<T, S> {
806811
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
807812
f.debug_list().entries(self).finish()
808813
}
809814
}
810815

811816
/// As with the standard library's `VecDeque`, items are added via `push_back`.
812-
impl<T, S: Storage> Extend<T> for DequeInner<T, S> {
817+
impl<T, S: VecStorage<T> + ?Sized> Extend<T> for DequeInner<T, S> {
813818
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
814819
for item in iter {
815820
self.push_back(item).ok().unwrap();
816821
}
817822
}
818823
}
819-
impl<'a, T: 'a + Copy, S: Storage> Extend<&'a T> for DequeInner<T, S> {
824+
impl<'a, T: 'a + Copy, S: VecStorage<T> + ?Sized> Extend<&'a T> for DequeInner<T, S> {
820825
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
821826
self.extend(iter.into_iter().copied())
822827
}
@@ -846,7 +851,7 @@ impl<T, const N: usize> IntoIterator for Deque<T, N> {
846851
}
847852
}
848853

849-
impl<'a, T, S: Storage> IntoIterator for &'a DequeInner<T, S> {
854+
impl<'a, T, S: VecStorage<T> + ?Sized> IntoIterator for &'a DequeInner<T, S> {
850855
type Item = &'a T;
851856
type IntoIter = Iter<'a, T>;
852857

@@ -855,7 +860,7 @@ impl<'a, T, S: Storage> IntoIterator for &'a DequeInner<T, S> {
855860
}
856861
}
857862

858-
impl<'a, T, S: Storage> IntoIterator for &'a mut DequeInner<T, S> {
863+
impl<'a, T, S: VecStorage<T> + ?Sized> IntoIterator for &'a mut DequeInner<T, S> {
859864
type Item = &'a mut T;
860865
type IntoIter = IterMut<'a, T>;
861866

0 commit comments

Comments
 (0)