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

Add excess capacity description for Vec::leak #79257

Closed
Closed
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
14 changes: 14 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,20 @@ impl<T, A: Allocator> Vec<T, A> {
/// static_ref[0] += 1;
/// assert_eq!(static_ref, &[2, 2, 3]);
/// ```
///
/// Drop excess capacity:
///
/// ```
/// let mut v = Vec::with_capacity(10);
/// v.extend([1, 2, 3].iter().cloned());
/// let slice = v.clone().into_boxed_slice();
///
/// unsafe {
/// let p = slice.as_ptr();
/// let rebuilt = std::slice::from_raw_parts(p, slice.len());
/// assert_eq!(rebuilt.len(), 3);
/// }
Comment on lines +1790 to +1798
Copy link
Member

Choose a reason for hiding this comment

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

This example doesn't demonstrate any leaking. .into_boxed_slice() returns a Box, which the slice variable will own.

The issue this PR was originally trying to fix is that the documentation doesn't promise this function drops excess capacity. However, I don't think that's something we want to fix. Instead, I think it'd be better if leak explicitly does not drop the excess capacity. But for that, there's an RFC which has been standing still for a while: rust-lang/rfcs#2969

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@m-ou-se Thank you for your support and help. I've been stucking and would like to close my PR.

/// ```
#[stable(feature = "vec_leak", since = "1.47.0")]
#[inline]
pub fn leak<'a>(self) -> &'a mut [T]
Expand Down