forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#105453 - scottmcm:vecdeque_from_iter, r=the…
…8472 Make `VecDeque::from_iter` O(1) from `vec(_deque)::IntoIter` As suggested in rust-lang#105046 (comment) by r? ``@the8472`` `Vec` & `VecDeque`'s `IntoIter`s own the allocations, and even if advanced can be turned into `VecDeque`s in O(1). This is just a specialization, not an API or doc commitment, so I don't think it needs an FCP.
- Loading branch information
Showing
5 changed files
with
139 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use super::{IntoIter, VecDeque}; | ||
|
||
/// Specialization trait used for `VecDeque::from_iter` | ||
pub(super) trait SpecFromIter<T, I> { | ||
fn spec_from_iter(iter: I) -> Self; | ||
} | ||
|
||
impl<T, I> SpecFromIter<T, I> for VecDeque<T> | ||
where | ||
I: Iterator<Item = T>, | ||
{ | ||
default fn spec_from_iter(iterator: I) -> Self { | ||
// Since converting is O(1) now, just re-use the `Vec` logic for | ||
// anything where we can't do something extra-special for `VecDeque`, | ||
// especially as that could save us some monomorphiziation work | ||
// if one uses the same iterators (like slice ones) with both. | ||
crate::vec::Vec::from_iter(iterator).into() | ||
} | ||
} | ||
|
||
impl<T> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T> { | ||
#[inline] | ||
fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self { | ||
iterator.into_vecdeque() | ||
} | ||
} | ||
|
||
impl<T> SpecFromIter<T, IntoIter<T>> for VecDeque<T> { | ||
#[inline] | ||
fn spec_from_iter(iterator: IntoIter<T>) -> Self { | ||
iterator.into_vecdeque() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters