[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <Z2LEqxaRxqJbl9KL@pollux>
Date: Wed, 18 Dec 2024 13:48:43 +0100
From: Danilo Krummrich <dakr@...nel.org>
To: Jimmy Ostler <jtostler1@...il.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Wedson Almeida Filho <walmeida@...rosoft.com>,
Filipe Xavier <felipe_life@...e.com>,
Valentin Obst <kernel@...entinobst.de>,
Daniel Sedlak <daniel@...lak.dev>,
Alex Mantel <alexmantel93@...lbox.org>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3 3/3] rust: alloc: Add doctest for `ArrayLayout`
On Tue, Dec 17, 2024 at 04:23:12PM -0800, Jimmy Ostler wrote:
> 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@...il.com>
> Link: https://github.com/Rust-for-Linux/linux/issues/1131
> Signed-off-by: Jimmy Ostler <jtostler1@...il.com>
Reviewed-by: Danilo Krummrich <dakr@...nel.org>
> ---
> rust/kernel/alloc/layout.rs | 19 +++++++++++++++++++
> 1 file changed, 19 insertions(+)
>
> diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
> index 4b3cd7fdc816..0e053dcc7941 100644
> --- a/rust/kernel/alloc/layout.rs
> +++ b/rust/kernel/alloc/layout.rs
> @@ -43,6 +43,25 @@ pub const fn empty() -> Self {
> /// # 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 => {
> --
> 2.47.1
>
Powered by blists - more mailing lists