Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,7 @@ impl<T, A: Allocator> Vec<T, A> {
///
/// # Panics
///
/// Panics if the number of elements in the vector overflows a `usize`.
/// Panics if the new capacity exceeds `isize::MAX` bytes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the wording here (and in Vec::push) is confusing because it uses the term capacity which in this case is not related to the Vec::capacity method since for a ZST, Vec::capacity can be bigger than isize::MAX but no actual memory will be allocated:

fn main() {
        let mut vec = vec![(); usize::MAX];
        assert_eq!(vec.len(), usize::MAX);
        assert_eq!(vec.capacity(), usize::MAX);
        assert!(vec.capacity() > usize::try_from(isize::MAX).unwrap());
        vec.append(&mut vec![]);
        assert!(vec.capacity() > usize::try_from(isize::MAX).unwrap());
}

https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=b75b2d890ed0f530ca925ff424f4246c

I would rather change both panic notes to use a different terminology like allocated memory with something for ZST that says no memory will be allocated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: It's also not directly related to the Vec::capacity method for other types, it's just for 1-byte large types where it corresponds 1-to-1. E.g. a 2-byte type can at most have isize::MAX / 2 capacity.

What replacement would you suggest?

Panics if the memory to be allocated would exceed isize::MAX bytes.

It seems to me that this would introduce another terminology and would thus make the note harder to understand.

///
/// # Examples
///
Expand Down