-
Notifications
You must be signed in to change notification settings - Fork 210
Implements TryFrom for Deque from array #524
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
98afd95
954acee
1c58cf1
d345c12
4df1839
88860f8
8907839
5cc1d4b
cb9a595
eacb197
aca4aa9
2dfeabe
5706415
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.#* | ||
Cargo.lock | ||
target/ | ||
.idea | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ use core::cmp::Ordering; | |
use core::fmt; | ||
use core::iter::FusedIterator; | ||
use core::marker::PhantomData; | ||
use core::mem::MaybeUninit; | ||
use core::mem::{ManuallyDrop, MaybeUninit}; | ||
use core::{ptr, slice}; | ||
|
||
use crate::vec::{OwnedVecStorage, VecStorage, VecStorageInner, ViewVecStorage}; | ||
|
@@ -998,6 +998,50 @@ impl<T: PartialEq, const N: usize> PartialEq for Deque<T, N> { | |
|
||
impl<T: Eq, const N: usize> Eq for Deque<T, N> {} | ||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might make more sense to use different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this what you mean? 2dfeabe |
||
/// 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(); | ||
/// | ||
/// assert_eq!(deq1, deq2); | ||
/// ``` | ||
type Error = (); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed here aca4aa9 |
||
|
||
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.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; | ||
|
@@ -1544,4 +1588,23 @@ mod tests { | |
|
||
assert_eq!(a, b); | ||
} | ||
|
||
#[test] | ||
fn try_from_array() { | ||
// Array is too big error. | ||
assert!(Deque::<u8, 3>::try_from([1, 2, 3, 4]).is_err()); | ||
|
||
// Array is at limit. | ||
assert!(Deque::<u8, 3>::try_from([1, 2, 3]).unwrap().is_full()); | ||
|
||
// Array is under limit. | ||
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(); | ||
|
||
assert_eq!(deq1, deq2); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed aca4aa9