From 4c2c523a0546480fe2ee1895802d80cfdcf4ad64 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Fri, 30 Nov 2018 23:38:05 -0800 Subject: [PATCH] Move VecDeque::resize_with out of the impl block --- src/liballoc/collections/vec_deque.rs | 60 ++++++++++++--------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/liballoc/collections/vec_deque.rs b/src/liballoc/collections/vec_deque.rs index cbf104a8fcde9..b52e93f85d94f 100644 --- a/src/liballoc/collections/vec_deque.rs +++ b/src/liballoc/collections/vec_deque.rs @@ -19,7 +19,7 @@ use core::cmp::Ordering; use core::fmt; -use core::iter::{repeat, repeat_with, FromIterator, FusedIterator}; +use core::iter::{repeat_with, FromIterator, FusedIterator}; use core::mem; use core::ops::Bound::{Excluded, Included, Unbounded}; use core::ops::{Index, IndexMut, RangeBounds}; @@ -1886,16 +1886,16 @@ impl VecDeque { debug_assert!(!self.is_full()); } } -} -impl VecDeque { - /// Modifies the `VecDeque` in-place so that `len()` is equal to new_len, - /// either by removing excess elements from the back or by appending clones of `value` - /// to the back. + /// Modifies the `VecDeque` in-place so that `len()` is equal to `new_len`, + /// either by removing excess elements from the back or by appending + /// elements generated by calling `generator` to the back. /// /// # Examples /// /// ``` + /// #![feature(vec_resize_with)] + /// /// use std::collections::VecDeque; /// /// let mut buf = VecDeque::new(); @@ -1904,32 +1904,36 @@ impl VecDeque { /// buf.push_back(15); /// assert_eq!(buf, [5, 10, 15]); /// - /// buf.resize(2, 0); + /// buf.resize_with(5, Default::default); + /// assert_eq!(buf, [5, 10, 15, 0, 0]); + /// + /// buf.resize_with(2, || unreachable!()); /// assert_eq!(buf, [5, 10]); /// - /// buf.resize(5, 20); - /// assert_eq!(buf, [5, 10, 20, 20, 20]); + /// let mut state = 100; + /// buf.resize_with(5, || { state += 1; state }); + /// assert_eq!(buf, [5, 10, 101, 102, 103]); /// ``` - #[stable(feature = "deque_extras", since = "1.16.0")] - pub fn resize(&mut self, new_len: usize, value: T) { + #[unstable(feature = "vec_resize_with", issue = "41758")] + pub fn resize_with(&mut self, new_len: usize, generator: impl FnMut()->T) { let len = self.len(); if new_len > len { - self.extend(repeat(value).take(new_len - len)) + self.extend(repeat_with(generator).take(new_len - len)) } else { self.truncate(new_len); } } +} - /// Modifies the `VecDeque` in-place so that `len()` is equal to `new_len`, - /// either by removing excess elements from the back or by appending - /// elements generated by calling `generator` to the back. +impl VecDeque { + /// Modifies the `VecDeque` in-place so that `len()` is equal to new_len, + /// either by removing excess elements from the back or by appending clones of `value` + /// to the back. /// /// # Examples /// /// ``` - /// #![feature(vec_resize_with)] - /// /// use std::collections::VecDeque; /// /// let mut buf = VecDeque::new(); @@ -1938,25 +1942,15 @@ impl VecDeque { /// buf.push_back(15); /// assert_eq!(buf, [5, 10, 15]); /// - /// buf.resize_with(5, Default::default); - /// assert_eq!(buf, [5, 10, 15, 0, 0]); - /// - /// buf.resize_with(2, || unreachable!()); + /// buf.resize(2, 0); /// assert_eq!(buf, [5, 10]); /// - /// let mut state = 100; - /// buf.resize_with(5, || { state += 1; state }); - /// assert_eq!(buf, [5, 10, 101, 102, 103]); + /// buf.resize(5, 20); + /// assert_eq!(buf, [5, 10, 20, 20, 20]); /// ``` - #[unstable(feature = "vec_resize_with", issue = "41758")] - pub fn resize_with(&mut self, new_len: usize, generator: impl FnMut()->T) { - let len = self.len(); - - if new_len > len { - self.extend(repeat_with(generator).take(new_len - len)) - } else { - self.truncate(new_len); - } + #[stable(feature = "deque_extras", since = "1.16.0")] + pub fn resize(&mut self, new_len: usize, value: T) { + self.resize_with(new_len, || value.clone()); } }