Skip to content

Commit

Permalink
Merge pull request #1693 from dtolnay/get
Browse files Browse the repository at this point in the history
Add Punctuated::get and get_mut
  • Loading branch information
dtolnay authored Jul 6, 2024
2 parents f0dfdbd + a443857 commit 06f34fc
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ impl<T, P> Punctuated<T, P> {
self.iter_mut().next_back()
}

/// Borrows the element at the given index.
pub fn get(&mut self, index: usize) -> Option<&T> {
if let Some((value, _punct)) = self.inner.get(index) {
Some(value)
} else if index == self.inner.len() {
self.last.as_deref()
} else {
None
}
}

/// Mutably borrows the element at the given index.
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
let inner_len = self.inner.len();
if let Some((value, _punct)) = self.inner.get_mut(index) {
Some(value)
} else if index == inner_len {
self.last.as_deref_mut()
} else {
None
}
}

/// Returns an iterator over borrowed syntax tree nodes of type `&T`.
pub fn iter(&self) -> Iter<T> {
Iter {
Expand Down

0 comments on commit 06f34fc

Please sign in to comment.