Skip to content

Commit 40a7fd3

Browse files
Re-introduce covariance for containers
1 parent 346525f commit 40a7fd3

File tree

10 files changed

+504
-199
lines changed

10 files changed

+504
-199
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,
@@ -539,7 +536,7 @@ pub struct PeekMutInner<'a, T, K, S>
539536
where
540537
T: Ord,
541538
K: Kind,
542-
S: Storage,
539+
S: VecStorage<T> + ?Sized,
543540
{
544541
heap: &'a mut BinaryHeapInner<T, K, S>,
545542
sift: bool,
@@ -550,20 +547,20 @@ where
550547
///
551548
/// This `struct` is created by [`BinaryHeap::peek_mut`].
552549
/// 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>>;
554551

555552
/// Structure wrapping a mutable reference to the greatest item on a
556553
/// `BinaryHeap`.
557554
///
558555
/// This `struct` is created by [`BinaryHeapView::peek_mut`].
559556
/// 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>>;
561558

562559
impl<T, K, S> Drop for PeekMutInner<'_, T, K, S>
563560
where
564561
T: Ord,
565562
K: Kind,
566-
S: Storage,
563+
S: VecStorage<T> + ?Sized,
567564
{
568565
fn drop(&mut self) {
569566
if self.sift {
@@ -576,7 +573,7 @@ impl<T, K, S> Deref for PeekMutInner<'_, T, K, S>
576573
where
577574
T: Ord,
578575
K: Kind,
579-
S: Storage,
576+
S: VecStorage<T> + ?Sized,
580577
{
581578
type Target = T;
582579
fn deref(&self) -> &T {
@@ -590,7 +587,7 @@ impl<T, K, S> DerefMut for PeekMutInner<'_, T, K, S>
590587
where
591588
T: Ord,
592589
K: Kind,
593-
S: Storage,
590+
S: VecStorage<T> + ?Sized,
594591
{
595592
fn deref_mut(&mut self) -> &mut T {
596593
debug_assert!(!self.heap.is_empty());
@@ -603,7 +600,7 @@ impl<'a, T, K, S> PeekMutInner<'a, T, K, S>
603600
where
604601
T: Ord,
605602
K: Kind,
606-
S: Storage,
603+
S: VecStorage<T> + ?Sized,
607604
{
608605
/// Removes the peeked value from the heap and returns it.
609606
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>
651648
where
652649
K: Kind,
653650
T: Ord + fmt::Debug,
654-
S: Storage,
651+
S: VecStorage<T> + ?Sized,
655652
{
656653
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
657654
f.debug_list().entries(self.iter()).finish()
@@ -662,7 +659,7 @@ impl<'a, T, K, S> IntoIterator for &'a BinaryHeapInner<T, K, S>
662659
where
663660
K: Kind,
664661
T: Ord,
665-
S: Storage,
662+
S: VecStorage<T> + ?Sized,
666663
{
667664
type Item = &'a T;
668665
type IntoIter = slice::Iter<'a, T>;
@@ -676,7 +673,7 @@ where
676673
mod tests {
677674
use static_assertions::assert_not_impl_any;
678675

679-
use super::{BinaryHeap, Max, Min};
676+
use super::{BinaryHeap, BinaryHeapView, Max, Min};
680677

681678
// Ensure a `BinaryHeap` containing `!Send` values stays `!Send` itself.
682679
assert_not_impl_any!(BinaryHeap<*const (), Max, 4>: Send);
@@ -849,4 +846,13 @@ mod tests {
849846
assert_eq!(heap.pop(), Some(1));
850847
assert_eq!(heap.pop(), None);
851848
}
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+
}
852858
}

src/defmt.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Defmt implementations for heapless types
22
3-
use crate::{storage::Storage, string::StringInner, vec::VecInner};
3+
use crate::{
4+
string::StringInner,
5+
vec::{VecInner, VecStorage},
6+
};
47
use defmt::Formatter;
58

6-
impl<T, S: Storage> defmt::Format for VecInner<T, S>
9+
impl<T, S: VecStorage<T> + ?Sized> defmt::Format for VecInner<T, S>
710
where
811
T: defmt::Format,
912
{
@@ -12,7 +15,7 @@ where
1215
}
1316
}
1417

15-
impl<S: Storage> defmt::Format for StringInner<S>
18+
impl<S: VecStorage<u8> + ?Sized> defmt::Format for StringInner<S>
1619
where
1720
u8: defmt::Format,
1821
{

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 [`DequeView`], generic over the [`Storage`].
44+
/// Base struct for [`Deque`] and [`DequeView`], generic over the [`VecStorage`].
4545
///
4646
/// In most cases you should use [`Deque`] or [`DequeView`] 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()
@@ -865,29 +870,29 @@ impl<T, const N: usize> Default for Deque<T, N> {
865870
}
866871
}
867872

868-
impl<T, S: Storage> Drop for DequeInner<T, S> {
873+
impl<T, S: VecStorage<T> + ?Sized> Drop for DequeInner<T, S> {
869874
fn drop(&mut self) {
870875
// safety: `self` is left in an inconsistent state but it doesn't matter since
871876
// it's getting dropped. Nothing should be able to observe `self` after drop.
872877
unsafe { self.drop_contents() }
873878
}
874879
}
875880

876-
impl<T: fmt::Debug, S: Storage> fmt::Debug for DequeInner<T, S> {
881+
impl<T: fmt::Debug, S: VecStorage<T> + ?Sized> fmt::Debug for DequeInner<T, S> {
877882
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
878883
f.debug_list().entries(self).finish()
879884
}
880885
}
881886

882887
/// As with the standard library's `VecDeque`, items are added via `push_back`.
883-
impl<T, S: Storage> Extend<T> for DequeInner<T, S> {
888+
impl<T, S: VecStorage<T> + ?Sized> Extend<T> for DequeInner<T, S> {
884889
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
885890
for item in iter {
886891
self.push_back(item).ok().unwrap();
887892
}
888893
}
889894
}
890-
impl<'a, T: 'a + Copy, S: Storage> Extend<&'a T> for DequeInner<T, S> {
895+
impl<'a, T: 'a + Copy, S: VecStorage<T> + ?Sized> Extend<&'a T> for DequeInner<T, S> {
891896
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
892897
self.extend(iter.into_iter().copied())
893898
}
@@ -917,7 +922,7 @@ impl<T, const N: usize> IntoIterator for Deque<T, N> {
917922
}
918923
}
919924

920-
impl<'a, T, S: Storage> IntoIterator for &'a DequeInner<T, S> {
925+
impl<'a, T, S: VecStorage<T> + ?Sized> IntoIterator for &'a DequeInner<T, S> {
921926
type Item = &'a T;
922927
type IntoIter = Iter<'a, T>;
923928

@@ -926,7 +931,7 @@ impl<'a, T, S: Storage> IntoIterator for &'a DequeInner<T, S> {
926931
}
927932
}
928933

929-
impl<'a, T, S: Storage> IntoIterator for &'a mut DequeInner<T, S> {
934+
impl<'a, T, S: VecStorage<T> + ?Sized> IntoIterator for &'a mut DequeInner<T, S> {
930935
type Item = &'a mut T;
931936
type IntoIter = IterMut<'a, T>;
932937

0 commit comments

Comments
 (0)