Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/slice_deque.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A double-ended queue with fixed capacity, backed by a slice.

use core::mem;
use core::{mem, num::NonZeroUsize};

use crate::{
meta::{Meta, MetaLayout},
Expand All @@ -26,6 +26,15 @@ impl SliceMeta {
layout: MetaLayout::Empty,
}
}
pub fn full(capacity: usize) -> SliceMeta {
SliceMeta {
capacity,
layout: MetaLayout::Linear {
first: 0,
len: NonZeroUsize::new(capacity).unwrap(),
},
}
}
}

impl Meta for SliceMeta {
Expand All @@ -48,6 +57,21 @@ impl Meta for SliceMeta {
/// A double-ended queue with fixed capacity, backed by a slice.
///
/// The capacity of the deque is determined by the length of the slice.
///
/// A `SliceDeque` can be initialized with the elements in the slice.
///
/// # Example
///
/// ```
/// # use holodeque::SliceDeque;
/// # fn main() {
/// let mut slice = ["these", "values", "will", "stay"];
/// let mut deque = SliceDeque::from(&mut slice);
///
/// assert!(deque.is_full());
/// assert_eq!(deque.len(), 4);
/// # }
/// ```
#[derive(Debug)]
pub struct SliceDeque<'a, T>
where
Expand Down Expand Up @@ -99,6 +123,7 @@ where
/// value of `T`.
///
/// # Example
///
/// ```
/// # use holodeque::SliceDeque;
/// # fn main() {
Expand All @@ -125,6 +150,7 @@ where
/// This is the length of the backing slice.
///
/// # Example
///
/// ```
/// # use holodeque::SliceDeque;
/// # fn main() {
Expand Down Expand Up @@ -718,6 +744,26 @@ where
}
}

impl<'a, T> From<&'a mut [T]> for SliceDeque<'a, T>
where
T: Default,
{
fn from(slice: &'a mut [T]) -> Self {
let meta = SliceMeta::full(slice.len());

SliceDeque { meta, items: slice }
}
}

impl<'a, T, const N: usize> From<&'a mut [T; N]> for SliceDeque<'a, T>
where
T: Default,
{
fn from(slice: &'a mut [T; N]) -> Self {
Self::from(slice.as_mut_slice())
}
}

#[cfg(feature = "serde")]
impl<'a, 'de, T> SliceDeque<'a, T>
where
Expand Down