Skip to content

Commit 04c49f0

Browse files
committed
Plumb Allocator generic into std::vec::PeekMut
1 parent 9d82de1 commit 04c49f0

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -760,33 +760,6 @@ impl<T> Vec<T> {
760760
unsafe { Self::from_parts_in(ptr, length, capacity, Global) }
761761
}
762762

763-
/// Returns a mutable reference to the last item in the vector, or
764-
/// `None` if it is empty.
765-
///
766-
/// # Examples
767-
///
768-
/// Basic usage:
769-
///
770-
/// ```
771-
/// #![feature(vec_peek_mut)]
772-
/// let mut vec = Vec::new();
773-
/// assert!(vec.peek_mut().is_none());
774-
///
775-
/// vec.push(1);
776-
/// vec.push(5);
777-
/// vec.push(2);
778-
/// assert_eq!(vec.last(), Some(&2));
779-
/// if let Some(mut val) = vec.peek_mut() {
780-
/// *val = 0;
781-
/// }
782-
/// assert_eq!(vec.last(), Some(&0));
783-
/// ```
784-
#[inline]
785-
#[unstable(feature = "vec_peek_mut", issue = "122742")]
786-
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
787-
PeekMut::new(self)
788-
}
789-
790763
/// Decomposes a `Vec<T>` into its raw components: `(pointer, length, capacity)`.
791764
///
792765
/// Returns the raw pointer to the underlying data, the length of
@@ -2747,6 +2720,33 @@ impl<T, A: Allocator> Vec<T, A> {
27472720
if predicate(last) { self.pop() } else { None }
27482721
}
27492722

2723+
/// Returns a mutable reference to the last item in the vector, or
2724+
/// `None` if it is empty.
2725+
///
2726+
/// # Examples
2727+
///
2728+
/// Basic usage:
2729+
///
2730+
/// ```
2731+
/// #![feature(vec_peek_mut)]
2732+
/// let mut vec = Vec::new();
2733+
/// assert!(vec.peek_mut().is_none());
2734+
///
2735+
/// vec.push(1);
2736+
/// vec.push(5);
2737+
/// vec.push(2);
2738+
/// assert_eq!(vec.last(), Some(&2));
2739+
/// if let Some(mut val) = vec.peek_mut() {
2740+
/// *val = 0;
2741+
/// }
2742+
/// assert_eq!(vec.last(), Some(&0));
2743+
/// ```
2744+
#[inline]
2745+
#[unstable(feature = "vec_peek_mut", issue = "122742")]
2746+
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T, A>> {
2747+
PeekMut::new(self)
2748+
}
2749+
27502750
/// Moves all the elements of `other` into `self`, leaving `other` empty.
27512751
///
27522752
/// # Panics

library/alloc/src/vec/peek_mut.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::ops::{Deref, DerefMut};
22

33
use super::Vec;
4+
use crate::alloc::Allocator;
45
use crate::fmt;
56

67
/// Structure wrapping a mutable reference to the last item in a
@@ -11,19 +12,19 @@ use crate::fmt;
1112
///
1213
/// [`peek_mut`]: Vec::peek_mut
1314
#[unstable(feature = "vec_peek_mut", issue = "122742")]
14-
pub struct PeekMut<'a, T> {
15-
vec: &'a mut Vec<T>,
15+
pub struct PeekMut<'a, T, A: Allocator> {
16+
vec: &'a mut Vec<T, A>,
1617
}
1718

1819
#[unstable(feature = "vec_peek_mut", issue = "122742")]
19-
impl<T: fmt::Debug> fmt::Debug for PeekMut<'_, T> {
20+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for PeekMut<'_, T, A> {
2021
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2122
f.debug_tuple("PeekMut").field(self.deref()).finish()
2223
}
2324
}
2425

25-
impl<'a, T> PeekMut<'a, T> {
26-
pub(crate) fn new(vec: &'a mut Vec<T>) -> Option<Self> {
26+
impl<'a, T, A: Allocator> PeekMut<'a, T, A> {
27+
pub(super) fn new(vec: &'a mut Vec<T, A>) -> Option<Self> {
2728
if vec.is_empty() { None } else { Some(Self { vec }) }
2829
}
2930

@@ -36,17 +37,18 @@ impl<'a, T> PeekMut<'a, T> {
3637
}
3738

3839
#[unstable(feature = "vec_peek_mut", issue = "122742")]
39-
impl<'a, T> Deref for PeekMut<'a, T> {
40+
impl<'a, T, A: Allocator> Deref for PeekMut<'a, T, A> {
4041
type Target = T;
4142

4243
fn deref(&self) -> &Self::Target {
44+
let idx = self.vec.len() - 1;
4345
// SAFETY: PeekMut is only constructed if the vec is non-empty
44-
unsafe { self.vec.get_unchecked(self.vec.len() - 1) }
46+
unsafe { self.vec.get_unchecked(idx) }
4547
}
4648
}
4749

4850
#[unstable(feature = "vec_peek_mut", issue = "122742")]
49-
impl<'a, T> DerefMut for PeekMut<'a, T> {
51+
impl<'a, T, A: Allocator> DerefMut for PeekMut<'a, T, A> {
5052
fn deref_mut(&mut self) -> &mut Self::Target {
5153
let idx = self.vec.len() - 1;
5254
// SAFETY: PeekMut is only constructed if the vec is non-empty

0 commit comments

Comments
 (0)