Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented TyrFrom for Deque from slice. #524

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `SortedLinkedListView`, the `!Sized` version of `SortedLinkedList`.
- Added implementation of `Borrow` and `BorrowMut` for `String` and `Vec`.
- Added `Deque::{swap, swap_unchecked, swap_remove_front, swap_remove_back}`.
- Implemented `TyrFrom` for `Deque` from slice.

### Changed

Expand Down
64 changes: 63 additions & 1 deletion src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use core::borrow::{Borrow, BorrowMut};
use core::fmt;
use core::iter::FusedIterator;
use core::mem::MaybeUninit;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::{ptr, slice};

use crate::storage::{OwnedStorage, Storage, ViewStorage};
Expand Down Expand Up @@ -950,6 +950,50 @@ where
}
}

impl<T, const NS: usize, const ND: usize> TryFrom<[T; NS]> for Deque<T, ND> {
/// Converts a `[T; NS]` into a `Deque<T, ND>`.
///
/// ```
/// use heapless::Deque;
///
/// let deq1 = Deque::<u8, 4>::try_from([1, 2, 3, 4]).unwrap();
/// let mut deq2 = Deque::<u8, 4>::new();
/// deq2.push_back(1).unwrap();
/// deq2.push_back(2).unwrap();
/// deq2.push_back(3).unwrap();
/// deq2.push_back(4).unwrap();
///
/// // todo change to `assert_eq!(deq1, deq2);` when PR #521 is merged.
/// assert_eq!(deq1.len(), deq2.len());
/// for (i, e1) in deq1.iter().enumerate() {
/// assert_eq!(Some(e1), deq2.get(i));
/// }
/// ```
type Error = ();

fn try_from(value: [T; NS]) -> Result<Self, Self::Error> {
if NS > ND {
return Err(());
}

let mut deq = Self::default();
let value = ManuallyDrop::new(value);

if size_of::<T>() != 0 {
// SAFETY: We already ensured that value fits in deq.
unsafe {
ptr::copy_nonoverlapping(value.as_ptr(), deq.buffer.as_mut_ptr() as *mut T, NS);
}
}

deq.front = 0;
deq.back = NS;
deq.full = NS == ND;

Ok(deq)
}
}

#[cfg(test)]
mod tests {
use static_assertions::assert_not_impl_any;
Expand Down Expand Up @@ -1440,4 +1484,22 @@ mod tests {
q.pop_front().unwrap();
q.swap(0, 2);
}

#[test]
fn tyr_from_slice() {
assert!(Deque::<u8, 3>::try_from([1, 2, 3, 4]).is_err());

let deq1 = Deque::<u8, 8>::try_from([1, 2, 3, 4]).unwrap();
let mut deq2 = Deque::<u8, 8>::new();
deq2.push_back(1).unwrap();
deq2.push_back(2).unwrap();
deq2.push_back(3).unwrap();
deq2.push_back(4).unwrap();

// todo change to `assert_eq!(deq1, deq2);` when PR #521 is merged.
assert_eq!(deq1.len(), deq2.len());
for (i, e1) in deq1.iter().enumerate() {
assert_eq!(Some(e1), deq2.get(i));
}
}
}
Loading