mirror of
https://github.com/Dasharo/linux.git
synced 2026-03-06 15:25:10 -08:00
rust: alloc: add doctest for ArrayLayout::new()
Add a rustdoc example and Kunit test to the `ArrayLayout` struct's `ArrayLayout::new()` function. This patch depends on the first patch in this series in order for the KUnit test to compile. Suggested-by: Boqun Feng <boqun.feng@gmail.com> Link: https://github.com/Rust-for-Linux/linux/issues/1131 Reviewed-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Jimmy Ostler <jtostler1@gmail.com> Link: https://lore.kernel.org/r/f1564da5bcaa6be87aee312767a1d1694a03d1b7.1734674670.git.jtostler1@gmail.com [ Added periods to example comments. Reworded title. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
committed by
Miguel Ojeda
parent
59d5846594
commit
91da5a2414
@@ -43,6 +43,25 @@ impl<T> ArrayLayout<T> {
|
||||
/// # Errors
|
||||
///
|
||||
/// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::alloc::layout::{ArrayLayout, LayoutError};
|
||||
/// let layout = ArrayLayout::<i32>::new(15)?;
|
||||
/// assert_eq!(layout.len(), 15);
|
||||
///
|
||||
/// // Errors because `len * size_of::<T>()` overflows.
|
||||
/// let layout = ArrayLayout::<i32>::new(isize::MAX as usize);
|
||||
/// assert!(layout.is_err());
|
||||
///
|
||||
/// // Errors because `len * size_of::<i32>() > isize::MAX`,
|
||||
/// // even though `len < isize::MAX`.
|
||||
/// let layout = ArrayLayout::<i32>::new(isize::MAX as usize / 2);
|
||||
/// assert!(layout.is_err());
|
||||
///
|
||||
/// # Ok::<(), Error>(())
|
||||
/// ```
|
||||
pub const fn new(len: usize) -> Result<Self, LayoutError> {
|
||||
match len.checked_mul(core::mem::size_of::<T>()) {
|
||||
Some(size) if size <= ISIZE_MAX => {
|
||||
|
||||
Reference in New Issue
Block a user