Merge pull request #236 from kornelski/track_caller

track_caller for capacity overflow panics
This commit is contained in:
bluss
2023-05-05 21:24:17 +02:00
committed by GitHub
3 changed files with 11 additions and 0 deletions
+2
View File
@@ -201,6 +201,7 @@ impl<const CAP: usize> ArrayString<CAP>
/// ///
/// assert_eq!(&string[..], "ab"); /// assert_eq!(&string[..], "ab");
/// ``` /// ```
#[track_caller]
pub fn push(&mut self, c: char) { pub fn push(&mut self, c: char) {
self.try_push(c).unwrap(); self.try_push(c).unwrap();
} }
@@ -252,6 +253,7 @@ impl<const CAP: usize> ArrayString<CAP>
/// ///
/// assert_eq!(&string[..], "ad"); /// assert_eq!(&string[..], "ad");
/// ``` /// ```
#[track_caller]
pub fn push_str(&mut self, s: &str) { pub fn push_str(&mut self, s: &str) {
self.try_push_str(s).unwrap() self.try_push_str(s).unwrap()
} }
+8
View File
@@ -77,6 +77,8 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(&array[..], &[1, 2]); /// assert_eq!(&array[..], &[1, 2]);
/// assert_eq!(array.capacity(), 16); /// assert_eq!(array.capacity(), 16);
/// ``` /// ```
#[inline]
#[track_caller]
pub fn new() -> ArrayVec<T, CAP> { pub fn new() -> ArrayVec<T, CAP> {
assert_capacity_limit!(CAP); assert_capacity_limit!(CAP);
unsafe { unsafe {
@@ -172,6 +174,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ///
/// assert_eq!(&array[..], &[1, 2]); /// assert_eq!(&array[..], &[1, 2]);
/// ``` /// ```
#[track_caller]
pub fn push(&mut self, element: T) { pub fn push(&mut self, element: T) {
ArrayVecImpl::push(self, element) ArrayVecImpl::push(self, element)
} }
@@ -277,6 +280,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(&array[..], &["y", "x"]); /// assert_eq!(&array[..], &["y", "x"]);
/// ///
/// ``` /// ```
#[track_caller]
pub fn insert(&mut self, index: usize, element: T) { pub fn insert(&mut self, index: usize, element: T) {
self.try_insert(index, element).unwrap() self.try_insert(index, element).unwrap()
} }
@@ -748,6 +752,7 @@ impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> {
/// assert_eq!(array.capacity(), 3); /// assert_eq!(array.capacity(), 3);
/// ``` /// ```
impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> { impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> {
#[track_caller]
fn from(array: [T; CAP]) -> Self { fn from(array: [T; CAP]) -> Self {
let array = ManuallyDrop::new(array); let array = ManuallyDrop::new(array);
let mut vec = <ArrayVec<T, CAP>>::new(); let mut vec = <ArrayVec<T, CAP>>::new();
@@ -1011,6 +1016,7 @@ impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
/// Extend the `ArrayVec` with an iterator. /// Extend the `ArrayVec` with an iterator.
/// ///
/// ***Panics*** if extending the vector exceeds its capacity. /// ***Panics*** if extending the vector exceeds its capacity.
#[track_caller]
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) { fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
unsafe { unsafe {
self.extend_from_iter::<_, true>(iter) self.extend_from_iter::<_, true>(iter)
@@ -1020,6 +1026,7 @@ impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
#[inline(never)] #[inline(never)]
#[cold] #[cold]
#[track_caller]
fn extend_panic() { fn extend_panic() {
panic!("ArrayVec: capacity exceeded in extend/from_iter"); panic!("ArrayVec: capacity exceeded in extend/from_iter");
} }
@@ -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. /// 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. /// 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) pub(crate) unsafe fn extend_from_iter<I, const CHECK: bool>(&mut self, iterable: I)
where I: IntoIterator<Item = T> where I: IntoIterator<Item = T>
{ {
+1
View File
@@ -35,6 +35,7 @@ pub(crate) trait ArrayVecImpl {
/// Return a raw mutable pointer to the vector's buffer. /// Return a raw mutable pointer to the vector's buffer.
fn as_mut_ptr(&mut self) -> *mut Self::Item; fn as_mut_ptr(&mut self) -> *mut Self::Item;
#[track_caller]
fn push(&mut self, element: Self::Item) { fn push(&mut self, element: Self::Item) {
self.try_push(element).unwrap() self.try_push(element).unwrap()
} }