Skip to content

Commit

Permalink
Merge pull request #236 from kornelski/track_caller
Browse files Browse the repository at this point in the history
track_caller for capacity overflow panics
  • Loading branch information
bluss authored May 5, 2023
2 parents 0e131fd + dc33f75 commit 6712744
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/array_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl<const CAP: usize> ArrayString<CAP>
///
/// assert_eq!(&string[..], "ab");
/// ```
#[track_caller]
pub fn push(&mut self, c: char) {
self.try_push(c).unwrap();
}
Expand Down Expand Up @@ -252,6 +253,7 @@ impl<const CAP: usize> ArrayString<CAP>
///
/// assert_eq!(&string[..], "ad");
/// ```
#[track_caller]
pub fn push_str(&mut self, s: &str) {
self.try_push_str(s).unwrap()
}
Expand Down
8 changes: 8 additions & 0 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(&array[..], &[1, 2]);
/// assert_eq!(array.capacity(), 16);
/// ```
#[inline]
#[track_caller]
pub fn new() -> ArrayVec<T, CAP> {
assert_capacity_limit!(CAP);
unsafe {
Expand Down Expand Up @@ -172,6 +174,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
///
/// assert_eq!(&array[..], &[1, 2]);
/// ```
#[track_caller]
pub fn push(&mut self, element: T) {
ArrayVecImpl::push(self, element)
}
Expand Down Expand Up @@ -277,6 +280,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(&array[..], &["y", "x"]);
///
/// ```
#[track_caller]
pub fn insert(&mut self, index: usize, element: T) {
self.try_insert(index, element).unwrap()
}
Expand Down Expand Up @@ -748,6 +752,7 @@ impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> {
/// assert_eq!(array.capacity(), 3);
/// ```
impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> {
#[track_caller]
fn from(array: [T; CAP]) -> Self {
let array = ManuallyDrop::new(array);
let mut vec = <ArrayVec<T, CAP>>::new();
Expand Down Expand Up @@ -1011,6 +1016,7 @@ impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
/// Extend the `ArrayVec` with an iterator.
///
/// ***Panics*** if extending the vector exceeds its capacity.
#[track_caller]
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
unsafe {
self.extend_from_iter::<_, true>(iter)
Expand All @@ -1020,6 +1026,7 @@ impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {

#[inline(never)]
#[cold]
#[track_caller]
fn extend_panic() {
panic!("ArrayVec: capacity exceeded in extend/from_iter");
}
Expand All @@ -1031,6 +1038,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
///
/// Unsafe because if CHECK is false, the length of the input is not checked.
/// The caller must ensure the length of the input fits in the capacity.
#[track_caller]
pub(crate) unsafe fn extend_from_iter<I, const CHECK: bool>(&mut self, iterable: I)
where I: IntoIterator<Item = T>
{
Expand Down
1 change: 1 addition & 0 deletions src/arrayvec_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) trait ArrayVecImpl {
/// Return a raw mutable pointer to the vector's buffer.
fn as_mut_ptr(&mut self) -> *mut Self::Item;

#[track_caller]
fn push(&mut self, element: Self::Item) {
self.try_push(element).unwrap()
}
Expand Down

0 comments on commit 6712744

Please sign in to comment.