Skip to content

Commit

Permalink
Genericise as_(mut)view for all containers
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jul 3, 2024
1 parent 4102ceb commit 90cd4eb
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 82 deletions.
16 changes: 8 additions & 8 deletions src/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,22 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
pub fn into_vec(self) -> Vec<T, N> {
self.data
}
}

impl<T, K, S: Storage> BinaryHeapInner<T, K, S>
where
T: Ord,
K: Kind,
{
/// Get a reference to the `BinaryHeap`, erasing the `N` const-generic.
pub fn as_view(&self) -> &BinaryHeapView<T, K> {
self
S::as_binary_heap_view(self)
}
/// Get a mutable reference to the `BinaryHeap`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut BinaryHeapView<T, K> {
self
S::as_mut_binary_heap_view(self)
}
}

impl<T, K, S: Storage> BinaryHeapInner<T, K, S>
where
T: Ord,
K: Kind,
{
/* Public API */
/// Returns the capacity of the binary heap.
pub fn capacity(&self) -> usize {
Expand Down
9 changes: 4 additions & 5 deletions src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,18 @@ impl<T, const N: usize> Deque<T, N> {
self.back - self.front
}
}
}

impl<T, S: Storage> DequeInner<T, S> {
/// Get a reference to the `Deque`, erasing the `N` const-generic.
pub fn as_view(&self) -> &DequeView<T> {
self
S::as_deque_view(self)
}

/// Get a mutable reference to the `Deque`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut DequeView<T> {
self
S::as_mut_deque_view(self)
}
}

impl<T, S: Storage> DequeInner<T, S> {
/// Returns the maximum number of elements the deque can hold.
pub fn storage_capacity(&self) -> usize {
self.buffer.borrow().len()
Expand Down
9 changes: 9 additions & 0 deletions src/histbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ impl<T, S: Storage> HistoryBufferInner<T, S> {
}

impl<T, S: Storage> HistoryBufferInner<T, S> {
/// Get a reference to the `HistoryBuffer`, erasing the `N` const-generic.
pub fn as_view(&self) -> &HistoryBufferView<T> {
S::as_histbuf_view(self)
}
/// Get a mutable reference to the `HistoryBuffer`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut HistoryBufferView<T> {
S::as_mut_histbuf_view(self)
}

unsafe fn drop_contents(&mut self) {
unsafe {
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(
Expand Down
14 changes: 7 additions & 7 deletions src/linear_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
pub const fn new() -> Self {
Self { buffer: Vec::new() }
}
}

impl<K, V, S: Storage> LinearMapInner<K, V, S>
where
K: Eq,
{
/// Get a reference to the `LinearMap`, erasing the `N` const-generic.
pub fn as_view(&self) -> &LinearMapView<K, V> {
self
S::as_linear_map_view(self)
}

/// Get a mutable reference to the `LinearMap`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut LinearMapView<K, V> {
self
S::as_mut_linear_map_view(self)
}
}

impl<K, V, S: Storage> LinearMapInner<K, V, S>
where
K: Eq,
{
/// Returns the number of elements that the map can hold.
///
/// Computes in *O*(1) time.
Expand Down
11 changes: 6 additions & 5 deletions src/mpmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ impl<T, const N: usize> MpMcQueue<T, N> {
enqueue_pos: AtomicTargetSize::new(0),
}
}
}

impl<T, S: Storage> MpMcQueueInner<T, S> {
/// Get a reference to the `MpMcQueue`, erasing the `N` const-generic.
///
///
Expand All @@ -196,8 +199,8 @@ impl<T, const N: usize> MpMcQueue<T, N> {
/// let view: &MpMcQueueView<u8> = &queue;
/// ```
#[inline]
pub const fn as_view(&self) -> &MpMcQueueView<T> {
self
pub fn as_view(&self) -> &MpMcQueueView<T> {
S::as_mpmc_queue_view(self)
}

/// Get a mutable reference to the `MpMcQueue`, erasing the `N` const-generic.
Expand All @@ -217,11 +220,9 @@ impl<T, const N: usize> MpMcQueue<T, N> {
/// ```
#[inline]
pub fn as_mut_view(&mut self) -> &mut MpMcQueueView<T> {
self
S::as_mut_mpmc_queue_view(self)
}
}

impl<T, S: Storage> MpMcQueueInner<T, S> {
fn mask(&self) -> UintSize {
(S::len(self.buffer.get()) - 1) as _
}
Expand Down
14 changes: 4 additions & 10 deletions src/sorted_linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,20 @@ impl_index_and_const_new!(LinkedIndexU8, u8, new_u8, { u8::MAX as usize - 1 });
impl_index_and_const_new!(LinkedIndexU16, u16, new_u16, { u16::MAX as usize - 1 });
impl_index_and_const_new!(LinkedIndexUsize, usize, new_usize, { usize::MAX - 1 });

impl<T, Idx, K, const N: usize> SortedLinkedList<T, Idx, K, N>
impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
where
Idx: SortedLinkedListIndex,
S: Storage,
{
/// Get a reference to the `SortedLinkedList`, erasing the `N` const-generic.
pub fn as_view(&self) -> &SortedLinkedListView<T, Idx, K> {
self
S::as_sorted_linked_list_view(self)
}

/// Get a mutable reference to the `Vec`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, Idx, K> {
self
S::as_mut_sorted_linked_list_view(self)
}
}

impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
where
Idx: SortedLinkedListIndex,
S: Storage,
{
/// Internal access helper
#[inline(always)]
fn node_at(&self, index: usize) -> &Node<T, Idx> {
Expand Down
8 changes: 4 additions & 4 deletions src/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,19 @@ impl<T, const N: usize> Queue<T, N> {
pub const fn capacity(&self) -> usize {
N - 1
}
}

impl<T, S: Storage> QueueInner<T, S> {
/// Get a reference to the `Queue`, erasing the `N` const-generic.
pub fn as_view(&self) -> &QueueView<T> {
self
S::as_queue_view(self)
}

/// Get a mutable reference to the `Queue`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut QueueView<T> {
self
S::as_mut_queue_view(self)
}
}

impl<T, S: Storage> QueueInner<T, S> {
#[inline]
fn increment(&self, val: usize) -> usize {
(val + 1) % self.n()
Expand Down
Loading

0 comments on commit 90cd4eb

Please sign in to comment.