Skip to content

Commit 6366631

Browse files
committed
auto merge of #20185 : csouth3/rust/dlist-deprecate, r=alexcrichton
This PR deprecates the `DList::ListInsertion` trait, in accordance with rust-lang/rfcs#509. The functions which were previously part of the ListInsertion impl for `DList::IterMut` have been moved to be inherent methods on the iterator itself, and appropriate doctests have been added.
2 parents 3e6b29f + 6bb59e4 commit 6366631

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/libcollections/dlist.rs

+41-4
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ impl<'a, A> DoubleEndedIterator<&'a mut A> for IterMut<'a, A> {
655655
impl<'a, A> ExactSizeIterator<&'a mut A> for IterMut<'a, A> {}
656656

657657
/// Allows mutating a `DList` while iterating.
658+
#[deprecated = "Trait is deprecated, use inherent methods on the iterator instead"]
658659
pub trait ListInsertion<A> {
659660
/// Inserts `elt` just after to the element most recently returned by
660661
/// `.next()`
@@ -689,14 +690,50 @@ impl<'a, A> IterMut<'a, A> {
689690
}
690691
}
691692

692-
impl<'a, A> ListInsertion<A> for IterMut<'a, A> {
693+
impl<'a, A> IterMut<'a, A> {
694+
/// Inserts `elt` just after the element most recently returned by `.next()`.
695+
/// The inserted element does not appear in the iteration.
696+
///
697+
/// # Examples
698+
///
699+
/// ```rust
700+
/// use std::collections::DList;
701+
///
702+
/// let mut list: DList<int> = vec![1, 3, 4].into_iter().collect();
703+
///
704+
/// {
705+
/// let mut it = list.iter_mut();
706+
/// assert_eq!(it.next().unwrap(), &1);
707+
/// // insert `2` after `1`
708+
/// it.insert_next(2);
709+
/// }
710+
/// {
711+
/// let vec: Vec<int> = list.into_iter().collect();
712+
/// assert_eq!(vec, vec![1i, 2, 3, 4]);
713+
/// }
714+
/// ```
693715
#[inline]
694-
fn insert_next(&mut self, elt: A) {
716+
pub fn insert_next(&mut self, elt: A) {
695717
self.insert_next_node(box Node::new(elt))
696718
}
697719

720+
/// Provides a reference to the next element, without changing the iterator.
721+
///
722+
/// # Examples
723+
///
724+
/// ```rust
725+
/// use std::collections::DList;
726+
///
727+
/// let mut list: DList<int> = vec![1, 2, 3].into_iter().collect();
728+
///
729+
/// let mut it = list.iter_mut();
730+
/// assert_eq!(it.next().unwrap(), &1);
731+
/// assert_eq!(it.peek_next().unwrap(), &2);
732+
/// // We just peeked at 2, so it was not consumed from the iterator.
733+
/// assert_eq!(it.next().unwrap(), &2);
734+
/// ```
698735
#[inline]
699-
fn peek_next(&mut self) -> Option<&mut A> {
736+
pub fn peek_next(&mut self) -> Option<&mut A> {
700737
if self.nelem == 0 {
701738
return None
702739
}
@@ -798,7 +835,7 @@ mod tests {
798835
use test::Bencher;
799836
use test;
800837

801-
use super::{DList, Node, ListInsertion};
838+
use super::{DList, Node};
802839

803840
pub fn check_links<T>(list: &DList<T>) {
804841
let mut len = 0u;

0 commit comments

Comments
 (0)