Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track_caller for capacity overflow panics #236

Merged
merged 1 commit into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/array_string.rs
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
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
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