Skip to content

Commit b48ad8c

Browse files
authored
Update mod.rs
1 parent b243d13 commit b48ad8c

File tree

1 file changed

+32
-31
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+32
-31
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,11 @@ impl<T, A: Allocator> VecDeque<T, A> {
183183
}
184184

185185
/// Writes an element into the buffer, moving it and returning a pointer to it.
186+
/// # Safety
187+
///
188+
/// May only be called if `off < self.capacity()`.
186189
#[inline]
187-
unsafe fn buffer_write(&mut self, off: usize, value: T) -> *mut T {
190+
unsafe fn buffer_write(&mut self, off: usize, value: T) -> &mut T {
188191
unsafe {
189192
let ptr = self.ptr().add(off);
190193
ptr::write(ptr, value);
@@ -1901,59 +1904,57 @@ impl<T, A: Allocator> VecDeque<T, A> {
19011904
self.buffer_write(self.head, value);
19021905
}
19031906
}
1904-
1905-
/// Appends an element to the back of the deque.
1907+
1908+
/// Prepends an element to the deque, returning a reference to it.
19061909
///
19071910
/// # Examples
19081911
///
19091912
/// ```
1913+
/// #![feature(push_mut)]
19101914
/// use std::collections::VecDeque;
19111915
///
1912-
/// let mut buf = VecDeque::new();
1913-
/// buf.push_back(1);
1914-
/// buf.push_back(3);
1915-
/// assert_eq!(3, *buf.back().unwrap());
1916+
/// let mut d = VecDeque::from([1, 2, 3]);
1917+
/// let x = d.push_front_mut(8);
1918+
/// *x -= 1;
1919+
/// assert_eq!(d.front(), Some(&7));
19161920
/// ```
1917-
#[stable(feature = "rust1", since = "1.0.0")]
1918-
#[rustc_confusables("push", "put", "append")]
1921+
#[unstable(feature = "push_mut", issue = "135974")]
19191922
#[track_caller]
1920-
pub fn push_back(&mut self, value: T) {
1923+
#[must_use = "if you don't need a reference to the value, use VecDeque::push_front instead"]
1924+
pub fn push_front_mut(&mut self, value: T) -> &mut T {
19211925
if self.is_full() {
19221926
self.grow();
19231927
}
19241928

1925-
unsafe { self.buffer_write(self.to_physical_idx(self.len), value) };
1929+
self.head = self.wrap_sub(self.head, 1);
19261930
self.len += 1;
1931+
// SAFETY: We know that self.head is within range of the deque.
1932+
unsafe { self.buffer_write(self.head, value) }
19271933
}
19281934

1929-
/// Prepends an element to the deque, returning a reference to it.
1935+
/// Appends an element to the back of the deque.
19301936
///
19311937
/// # Examples
19321938
///
19331939
/// ```
1934-
/// #![feature(push_mut)]
19351940
/// use std::collections::VecDeque;
19361941
///
1937-
/// let mut d = VecDeque::from([1, 2, 3]);
1938-
/// let x = d.push_front_mut(1);
1939-
/// *x -= 1;
1940-
/// assert_eq!(d.front(), Some(&0));
1942+
/// let mut buf = VecDeque::new();
1943+
/// buf.push_back(1);
1944+
/// buf.push_back(3);
1945+
/// assert_eq!(3, *buf.back().unwrap());
19411946
/// ```
1942-
#[unstable(feature = "push_mut", issue = "135974")]
1947+
#[stable(feature = "rust1", since = "1.0.0")]
1948+
#[rustc_confusables("push", "put", "append")]
19431949
#[track_caller]
1944-
#[must_use = "if you don't need a reference to the value, use VecDeque::push_front instead"]
1945-
pub fn push_front_mut(&mut self, value: T) -> &mut T {
1950+
pub fn push_back(&mut self, value: T) {
19461951
if self.is_full() {
19471952
self.grow();
19481953
}
1949-
1950-
self.head = self.wrap_sub(self.head, 1);
1954+
1955+
// SAFETY: We know that self.head is within range of the deque.
1956+
unsafe { self.buffer_write(self.to_physical_idx(self.len), value) };
19511957
self.len += 1;
1952-
1953-
unsafe {
1954-
let ptr = self.buffer_write(self.head, value);
1955-
&mut *ptr
1956-
}
19571958
}
19581959

19591960
/// Appends an element to the back of the deque, returning a reference to it.
@@ -1965,9 +1966,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
19651966
/// use std::collections::VecDeque;
19661967
///
19671968
/// let mut d = VecDeque::from([1, 2, 3]);
1968-
/// let x = d.push_back_mut(3);
1969+
/// let x = d.push_back_mut(9);
19691970
/// *x += 1;
1970-
/// assert_eq!(d.back(), Some(&4));
1971+
/// assert_eq!(d.back(), Some(&10));
19711972
/// ```
19721973
#[unstable(feature = "push_mut", issue = "135974")]
19731974
#[track_caller]
@@ -2158,7 +2159,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
21582159
self.wrap_copy(self.to_physical_idx(index), self.to_physical_idx(index + 1), k);
21592160
let ptr = self.buffer_write(self.to_physical_idx(index), value);
21602161
self.len += 1;
2161-
Some(&mut *ptr)
2162+
Some(ptr)
21622163
}
21632164
} else {
21642165
let old_head = self.head;
@@ -2167,7 +2168,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
21672168
self.wrap_copy(old_head, self.head, index);
21682169
let ptr = self.buffer_write(self.to_physical_idx(index), value);
21692170
self.len += 1;
2170-
Some(&mut *ptr)
2171+
Some(ptr)
21712172
}
21722173
}
21732174
}

0 commit comments

Comments
 (0)