[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLgg8PW0P2OKC14Tm_0DTVff9ew+MKajMq4Urecz51Zvc7A@mail.gmail.com>
Date: Tue, 3 Dec 2024 09:40:35 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: jtostler1@...il.com
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: alloc: Add doctest for `ArrayLayout`
On Tue, Dec 3, 2024 at 6:19 AM <jtostler1@...il.com> wrote:
>
> From: Jimmy Ostler <jtostler1@...il.com>
>
> Added a rustdoc example and Kunit test to the `ArrayLayout` struct's
> `ArrayLayout::new()` function.
>
> Kunit tests ran using `./tools/testing/kunit/kunit.py run \
> --make_options LLVM=1 \
> --kconfig_add CONFIG_RUST=y` passed.
>
> Generated documentation looked as expected.
>
> Signed-off-by: Jimmy Ostler <jtostler1@...il.com>
> Suggested-by: Boqun Feng <boqun.feng@...il.com>
> Link: https://github.com/Rust-for-Linux/linux/issues/1131
> ---
> rust/kernel/alloc/layout.rs | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
> index 4b3cd7fdc816..4265f92f8af0 100644
> --- a/rust/kernel/alloc/layout.rs
> +++ b/rust/kernel/alloc/layout.rs
> @@ -7,6 +7,7 @@
> use core::{alloc::Layout, marker::PhantomData};
>
> /// Error when constructing an [`ArrayLayout`].
> +#[derive(Debug)]
> pub struct LayoutError;
>
> /// A layout for an array `[T; n]`.
> @@ -43,6 +44,19 @@ pub const fn empty() -> Self {
> /// # Errors
> ///
> /// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
> + ///
> + ///
> + /// # Examples
There should only be one empty line before # Examples, not two.
> + ///
> + /// ```rust
> + /// use kernel::alloc::layout::ArrayLayout;
> + ///
> + /// let layout = ArrayLayout::<i32>::new(15);
> + /// assert_eq!(layout.len(), 15);
I think it's less confusing to move this expect() to the line before.
let layout = ArrayLayout::<i32>::new(15).expect(...);
> + ///
> + /// let layout = ArrayLayout::<i32>::new(isize::MAX as usize);
> + /// assert!(layout.is_err());
> + /// ```
> pub const fn new(len: usize) -> Result<Self, LayoutError> {
> match len.checked_mul(core::mem::size_of::<T>()) {
> Some(size) if size <= ISIZE_MAX => {
>
> base-commit: 1dc707e647bc919834eff9636c8d00b78c782545
> --
> 2.47.1
>
>
Powered by blists - more mailing lists