Skip to content

Commit 98c50eb

Browse files
Change the placement of two functions.
Right now, the order is as follows: `pop_front()` `push_front()` `push_back()` `pop_back()` `swap_remove_back()` `swap_remove_front()` I believe it would be more natural, and easier to follow, if we place `pop_back()` right after the `pop_front()`, and `swap_remove_back()` after the `swap_remove_front()` like this: `pop_front()` `pop_back()` `push_front()` `push_back()` `swap_remove_front()` `swap_remove_back()` The rest of the documentation (at least in this module) adheres to the same logic, where the 'front' function always precedes its 'back' equivalent.
1 parent c43753f commit 98c50eb

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

src/liballoc/collections/vec_deque.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,31 @@ impl<T> VecDeque<T> {
11971197
}
11981198
}
11991199

1200+
/// Removes the last element from the `VecDeque` and returns it, or `None` if
1201+
/// it is empty.
1202+
///
1203+
/// # Examples
1204+
///
1205+
/// ```
1206+
/// use std::collections::VecDeque;
1207+
///
1208+
/// let mut buf = VecDeque::new();
1209+
/// assert_eq!(buf.pop_back(), None);
1210+
/// buf.push_back(1);
1211+
/// buf.push_back(3);
1212+
/// assert_eq!(buf.pop_back(), Some(3));
1213+
/// ```
1214+
#[stable(feature = "rust1", since = "1.0.0")]
1215+
pub fn pop_back(&mut self) -> Option<T> {
1216+
if self.is_empty() {
1217+
None
1218+
} else {
1219+
self.head = self.wrap_sub(self.head, 1);
1220+
let head = self.head;
1221+
unsafe { Some(self.buffer_read(head)) }
1222+
}
1223+
}
1224+
12001225
/// Prepends an element to the `VecDeque`.
12011226
///
12021227
/// # Examples
@@ -1241,38 +1266,13 @@ impl<T> VecDeque<T> {
12411266
unsafe { self.buffer_write(head, value) }
12421267
}
12431268

1244-
/// Removes the last element from the `VecDeque` and returns it, or `None` if
1245-
/// it is empty.
1246-
///
1247-
/// # Examples
1248-
///
1249-
/// ```
1250-
/// use std::collections::VecDeque;
1251-
///
1252-
/// let mut buf = VecDeque::new();
1253-
/// assert_eq!(buf.pop_back(), None);
1254-
/// buf.push_back(1);
1255-
/// buf.push_back(3);
1256-
/// assert_eq!(buf.pop_back(), Some(3));
1257-
/// ```
1258-
#[stable(feature = "rust1", since = "1.0.0")]
1259-
pub fn pop_back(&mut self) -> Option<T> {
1260-
if self.is_empty() {
1261-
None
1262-
} else {
1263-
self.head = self.wrap_sub(self.head, 1);
1264-
let head = self.head;
1265-
unsafe { Some(self.buffer_read(head)) }
1266-
}
1267-
}
1268-
12691269
#[inline]
12701270
fn is_contiguous(&self) -> bool {
12711271
self.tail <= self.head
12721272
}
12731273

1274-
/// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
1275-
/// last element.
1274+
/// Removes an element from anywhere in the `VecDeque` and returns it,
1275+
/// replacing it with the first element.
12761276
///
12771277
/// This does not preserve ordering, but is O(1).
12781278
///
@@ -1286,28 +1286,28 @@ impl<T> VecDeque<T> {
12861286
/// use std::collections::VecDeque;
12871287
///
12881288
/// let mut buf = VecDeque::new();
1289-
/// assert_eq!(buf.swap_remove_back(0), None);
1289+
/// assert_eq!(buf.swap_remove_front(0), None);
12901290
/// buf.push_back(1);
12911291
/// buf.push_back(2);
12921292
/// buf.push_back(3);
12931293
/// assert_eq!(buf, [1, 2, 3]);
12941294
///
1295-
/// assert_eq!(buf.swap_remove_back(0), Some(1));
1296-
/// assert_eq!(buf, [3, 2]);
1295+
/// assert_eq!(buf.swap_remove_front(2), Some(3));
1296+
/// assert_eq!(buf, [2, 1]);
12971297
/// ```
12981298
#[stable(feature = "deque_extras_15", since = "1.5.0")]
1299-
pub fn swap_remove_back(&mut self, index: usize) -> Option<T> {
1299+
pub fn swap_remove_front(&mut self, index: usize) -> Option<T> {
13001300
let length = self.len();
1301-
if length > 0 && index < length - 1 {
1302-
self.swap(index, length - 1);
1301+
if length > 0 && index < length && index != 0 {
1302+
self.swap(index, 0);
13031303
} else if index >= length {
13041304
return None;
13051305
}
1306-
self.pop_back()
1306+
self.pop_front()
13071307
}
13081308

1309-
/// Removes an element from anywhere in the `VecDeque` and returns it,
1310-
/// replacing it with the first element.
1309+
/// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
1310+
/// last element.
13111311
///
13121312
/// This does not preserve ordering, but is O(1).
13131313
///
@@ -1321,24 +1321,24 @@ impl<T> VecDeque<T> {
13211321
/// use std::collections::VecDeque;
13221322
///
13231323
/// let mut buf = VecDeque::new();
1324-
/// assert_eq!(buf.swap_remove_front(0), None);
1324+
/// assert_eq!(buf.swap_remove_back(0), None);
13251325
/// buf.push_back(1);
13261326
/// buf.push_back(2);
13271327
/// buf.push_back(3);
13281328
/// assert_eq!(buf, [1, 2, 3]);
13291329
///
1330-
/// assert_eq!(buf.swap_remove_front(2), Some(3));
1331-
/// assert_eq!(buf, [2, 1]);
1330+
/// assert_eq!(buf.swap_remove_back(0), Some(1));
1331+
/// assert_eq!(buf, [3, 2]);
13321332
/// ```
13331333
#[stable(feature = "deque_extras_15", since = "1.5.0")]
1334-
pub fn swap_remove_front(&mut self, index: usize) -> Option<T> {
1334+
pub fn swap_remove_back(&mut self, index: usize) -> Option<T> {
13351335
let length = self.len();
1336-
if length > 0 && index < length && index != 0 {
1337-
self.swap(index, 0);
1336+
if length > 0 && index < length - 1 {
1337+
self.swap(index, length - 1);
13381338
} else if index >= length {
13391339
return None;
13401340
}
1341-
self.pop_front()
1341+
self.pop_back()
13421342
}
13431343

13441344
/// Inserts an element at `index` within the `VecDeque`, shifting all elements with indices

0 commit comments

Comments
 (0)