Skip to content

Commit 21dd498

Browse files
Re-introduce covariance for containers
1 parent 2e71c18 commit 21dd498

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,20 +33,22 @@
3333
//! }
3434
//! ```
3535
36-
use core::borrow::{Borrow, BorrowMut};
3736
use core::cmp::Ordering;
3837
use core::fmt;
3938
use core::iter::FusedIterator;
39+
use core::marker::PhantomData;
4040
use core::mem::MaybeUninit;
4141
use core::{ptr, slice};
4242

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

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

6163
/// A fixed capacity double-ended queue.
@@ -92,7 +94,7 @@ pub struct DequeInner<T, S: Storage> {
9294
/// println!("{}", x);
9395
/// }
9496
/// ```
95-
pub type Deque<T, const N: usize> = DequeInner<T, OwnedStorage<N>>;
97+
pub type Deque<T, const N: usize> = DequeInner<T, OwnedVecStorage<T, N>>;
9698

9799
/// A double-ended queue with dynamic capacity.
98100
///
@@ -131,7 +133,7 @@ pub type Deque<T, const N: usize> = DequeInner<T, OwnedStorage<N>>;
131133
/// println!("{}", x);
132134
/// }
133135
/// ```
134-
pub type DequeView<T> = DequeInner<T, ViewStorage>;
136+
pub type DequeView<T> = DequeInner<T, ViewVecStorage<T>>;
135137

136138
impl<T, const N: usize> Deque<T, N> {
137139
const INIT: MaybeUninit<T> = MaybeUninit::uninit();
@@ -154,7 +156,10 @@ impl<T, const N: usize> Deque<T, N> {
154156
crate::sealed::greater_than_0::<N>();
155157

156158
Self {
157-
buffer: [Self::INIT; N],
159+
phantom: PhantomData,
160+
buffer: VecStorageInner {
161+
buffer: [Self::INIT; N],
162+
},
158163
front: 0,
159164
back: 0,
160165
full: false,
@@ -192,7 +197,7 @@ impl<T, const N: usize> Deque<T, N> {
192197
}
193198
}
194199

195-
impl<T, S: Storage> DequeInner<T, S> {
200+
impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
196201
/// Returns the maximum number of elements the deque can hold.
197202
pub fn storage_capacity(&self) -> usize {
198203
self.buffer.borrow().len()
@@ -866,29 +871,29 @@ impl<T, const N: usize> Default for Deque<T, N> {
866871
}
867872
}
868873

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

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

883888
/// As with the standard library's `VecDeque`, items are added via `push_back`.
884-
impl<T, S: Storage> Extend<T> for DequeInner<T, S> {
889+
impl<T, S: VecStorage<T> + ?Sized> Extend<T> for DequeInner<T, S> {
885890
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
886891
for item in iter {
887892
self.push_back(item).ok().unwrap();
888893
}
889894
}
890895
}
891-
impl<'a, T: 'a + Copy, S: Storage> Extend<&'a T> for DequeInner<T, S> {
896+
impl<'a, T: 'a + Copy, S: VecStorage<T> + ?Sized> Extend<&'a T> for DequeInner<T, S> {
892897
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
893898
self.extend(iter.into_iter().copied())
894899
}
@@ -918,7 +923,7 @@ impl<T, const N: usize> IntoIterator for Deque<T, N> {
918923
}
919924
}
920925

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

@@ -927,7 +932,7 @@ impl<'a, T, S: Storage> IntoIterator for &'a DequeInner<T, S> {
927932
}
928933
}
929934

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

0 commit comments

Comments
 (0)