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

vec: add try_* methods and a try_vec! macro to make Vec usable in without infallible allocation methods #95051

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions library/alloc/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ impl TryReserveError {
pub fn kind(&self) -> TryReserveErrorKind {
self.kind.clone()
}

/// Create a new [`TryReserveError`] indicating that there was an allocation failure.
#[must_use]
#[unstable(feature = "more_fallible_allocation_methods", issue = "86942")]
pub fn alloc_error(layout: Layout) -> Self {
Self { kind: TryReserveErrorKind::AllocError { layout, non_exhaustive: () } }
}
}

/// Details of the allocation that caused a `TryReserveError`
Expand Down
85 changes: 85 additions & 0 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,70 @@ macro_rules! vec {
);
}

/// Creates a [`Vec`] containing the arguments.
///
/// `try_vec!` allows `Vec`s to be defined with the same syntax as array expressions.
/// There are two forms of this macro:
///
/// - Create a [`Vec`] containing a given list of elements:
///
/// ```
/// #![feature(more_fallible_allocation_methods)]
/// # #[macro_use] extern crate alloc;
/// let v = try_vec![1, 2, 3]?;
/// assert_eq!(v[0], 1);
/// assert_eq!(v[1], 2);
/// assert_eq!(v[2], 3);
/// # Ok::<(), alloc::collections::TryReserveError>(())
/// ```
///
/// - Create a [`Vec`] from a given element and size:
///
/// ```
/// #![feature(more_fallible_allocation_methods)]
/// # #[macro_use] extern crate alloc;
/// let v = try_vec![1; 3]?;
/// assert_eq!(v, [1, 1, 1]);
/// # Ok::<(), alloc::collections::TryReserveError>(())
/// ```
///
/// Note that unlike array expressions this syntax supports all elements
/// which implement [`Clone`] and the number of elements doesn't have to be
/// a constant.
///
/// This will use `clone` to duplicate an expression, so one should be careful
/// using this with types having a nonstandard `Clone` implementation. For
/// example, `try_vec![Rc::new(1); 5]` will create a vector of five references
/// to the same boxed integer value, not five references pointing to independently
/// boxed integers.
///
/// Also, note that `try_vec![expr; 0]` is allowed, and produces an empty vector.
/// This will still evaluate `expr`, however, and immediately drop the resulting value, so
/// be mindful of side effects.
///
/// [`Vec`]: crate::vec::Vec
#[cfg(not(test))]
#[macro_export]
#[allow_internal_unstable(allocator_api, liballoc_internals)]
#[unstable(feature = "more_fallible_allocation_methods", issue = "86942")]
macro_rules! try_vec {
() => (
$crate::__rust_force_expr!(core::result::Result::Ok($crate::vec::Vec::new()))
);
($elem:expr; $n:expr) => (
$crate::__rust_force_expr!($crate::vec::try_from_elem($elem, $n))
);
($($x:expr),+ $(,)?) => (
$crate::__rust_force_expr!({
let values = [$($x),+];
let layout = $crate::alloc::Layout::for_value(&values);
dpaoliello marked this conversation as resolved.
Show resolved Hide resolved
$crate::boxed::Box::try_new(values)
.map(|b| <[_]>::into_vec(b))
.map_err(|_| $crate::collections::TryReserveError::alloc_error(layout))
})
);
}

// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
// required for this macro definition, is not available. Instead use the
// `slice::into_vec` function which is only available with cfg(test)
Expand All @@ -73,6 +137,27 @@ macro_rules! vec {
($($x:expr,)*) => (vec![$($x),*])
}

#[cfg(test)]
#[allow(unused_macros)]
macro_rules! try_vec {
() => (
core::result::Result::Ok($crate::vec::Vec::new())
);
($elem:expr; $n:expr) => (
$crate::vec::try_from_elem($elem, $n)
);
($($x:expr),*) => (
{
let values = [$($x),+];
let layout = $crate::alloc::Layout::for_value(&values);
$crate::boxed::Box::try_new(values)
.map(|b| <[_]>::into_vec(b))
.map_err(|_| $crate::collections::TryReserveError::alloc_error(layout))
}
);
($($x:expr,)*) => (try_vec![$($x),*])
}

/// Creates a `String` using interpolation of runtime expressions.
///
/// The first argument `format!` receives is a format string. This must be a string
Expand Down
Loading