Skip to content
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 @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Bump MSRV to 1.88 (requires by trybuild dev dependency).
- Fixed unsoundness in `Vec::IntoIter::drop` and `HistoryBuf::write`in the context of panicking drop implementations.
- Added `push_mut` to `Vec`.
- Implemented `FromIterator` for `Deque`.
- Fixed unsoundness in `IndexMap:insert`.
- Limited max size of `IndexMap` to u16::MAX + 1.
The implementation for sizes higher than u16::MAX were unsound anyway.
Expand Down
36 changes: 36 additions & 0 deletions src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,18 @@ impl<'a, T: 'a + Copy, S: VecStorage<T> + ?Sized> Extend<&'a T> for DequeInner<T
}
}

impl<T, const N: usize> FromIterator<T> for Deque<T, N> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
let mut deque = Self::new();
deque.extend(iter);

deque
}
}

/// An iterator that moves out of a [`Deque`].
///
/// This struct is created by calling the `into_iter` method.
Expand Down Expand Up @@ -1553,6 +1565,30 @@ mod tests {
v.extend(&[1, 2, 3, 4, 5]);
}

#[test]
fn from_iter() {
// Iterator is empty.
let deq: Deque<i32, 4> = core::iter::empty().collect();
assert!(deq.is_empty());

// Iterator is under limit.
let deq: Deque<i32, 4> = [1, 2, 3].into_iter().collect();
assert!(!deq.is_full());
assert_eq!(deq.as_slices(), (&[1, 2, 3][..], &[][..]));

// Iterator is at limit.
let deq: Deque<i32, 4> = [1, 2, 3, 4].into_iter().collect();
assert!(deq.is_full());
assert_eq!(deq.as_slices(), (&[1, 2, 3, 4][..], &[][..]));
}

#[test]
#[should_panic]
fn from_iter_panic() {
// Too many elements (4 vs 5)
let _: Deque<i32, 4> = [1, 2, 3, 4, 5].into_iter().collect();
}

#[test]
fn iter() {
let mut v: Deque<i32, 4> = Deque::new();
Expand Down
Loading