Skip to content

Commit

Permalink
Auto merge of #79233 - yoshuawuyts:alloc-doc-alias, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Add doc aliases for memory allocations

This patch adds doc aliases for various C allocation functions, making it possible to search for the C-equivalent of a function and finding the (safe) Rust counterpart:

- `Vec::with_capacity` / `Box::new` / `vec!` -> alloc + malloc, allocates memory
- `Box::new_zeroed` -> calloc, allocates zeroed-out memory
- `Vec::{reserve,reserve_exact,try_reserve_exact,shrink_to_fit,shrink_to}` -> realloc, reallocates a previously allocated slice of memory

It's worth noting that `Vec::new` does not allocate, so we don't link to it. Instead people are probably looking for `Vec::with_capacity` or `vec!`. I hope this will allow people comfortable with the system allocation APIs to make it easier to find what they may be looking for.

Thanks!
  • Loading branch information
bors committed Jan 22, 2021
2 parents 22ddcd1 + 7d10238 commit 34b3d41
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ impl<T> Box<T> {
/// ```
/// let five = Box::new(5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)]
#[doc(alias = "alloc")]
#[doc(alias = "malloc")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(x: T) -> Self {
box x
}
Expand Down Expand Up @@ -226,8 +228,9 @@ impl<T> Box<T> {
/// ```
///
/// [zeroed]: mem::MaybeUninit::zeroed
#[unstable(feature = "new_uninit", issue = "63291")]
#[inline]
#[doc(alias = "calloc")]
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
Self::new_zeroed_in(Global)
}
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
///
/// [`Vec`]: crate::vec::Vec
#[cfg(not(test))]
#[doc(alias = "alloc")]
#[doc(alias = "malloc")]
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable(box_syntax, liballoc_internals)]
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ impl String {
/// s.push('a');
/// ```
#[inline]
#[doc(alias = "alloc")]
#[doc(alias = "malloc")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(capacity: usize) -> String {
String { vec: Vec::with_capacity(capacity) }
Expand Down
7 changes: 7 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ impl<T> Vec<T> {
/// assert!(vec.capacity() >= 11);
/// ```
#[inline]
#[doc(alias = "malloc")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_in(capacity, Global)
Expand Down Expand Up @@ -766,6 +767,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// vec.reserve(10);
/// assert!(vec.capacity() >= 11);
/// ```
#[doc(alias = "realloc")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reserve(&mut self, additional: usize) {
self.buf.reserve(self.len, additional);
Expand All @@ -791,6 +793,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// vec.reserve_exact(10);
/// assert!(vec.capacity() >= 11);
/// ```
#[doc(alias = "realloc")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reserve_exact(&mut self, additional: usize) {
self.buf.reserve_exact(self.len, additional);
Expand Down Expand Up @@ -828,6 +831,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// }
/// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
/// ```
#[doc(alias = "realloc")]
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.buf.try_reserve(self.len, additional)
Expand Down Expand Up @@ -869,6 +873,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// }
/// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
/// ```
#[doc(alias = "realloc")]
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.buf.try_reserve_exact(self.len, additional)
Expand All @@ -888,6 +893,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// vec.shrink_to_fit();
/// assert!(vec.capacity() >= 3);
/// ```
#[doc(alias = "realloc")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn shrink_to_fit(&mut self) {
// The capacity is never less than the length, and there's nothing to do when
Expand Down Expand Up @@ -920,6 +926,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// vec.shrink_to(0);
/// assert!(vec.capacity() >= 3);
/// ```
#[doc(alias = "realloc")]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.buf.shrink_to_fit(cmp::max(self.len, min_capacity));
Expand Down

0 comments on commit 34b3d41

Please sign in to comment.