From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 494DC150980; Wed, 18 Dec 2024 12:48:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734526130; cv=none; b=PHuROVkm8gRuYyS6YHDshS8VxmjxrXlBXWkqWEmwDW0tFYJhwkhrozdJ3tA6T2fj/IuvTv36Mab30j0yIqddzSy+2Kml2EtNfH+FhHcHywrQ2NiV2fJTP+FvETXPyVffhpFhkt2uqw3ey/49uS7hyilHI2cUPG/pSH5rB5zsQFU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734526130; c=relaxed/simple; bh=n6yFccAHwNfpCkLoJEcdsxlok9C4rHzofc/QyPqniEo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JBzoSiYuGZh23k75CikKsEnV0TUj10zRC19MfAjKGIJncnpESEMK5E0q3XngHzsqemm8lNSmRfo87rOLbMSGAl+oj05sYxMg/t1k+iKfqfZOUbTC5D/TN6aKAneGG1FwcguN1bOIxox5W+VYvz9D4Adt13S3xRh1GlCH/RpnGQY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HtyBxp8w; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HtyBxp8w" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A764C4CECE; Wed, 18 Dec 2024 12:48:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734526129; bh=n6yFccAHwNfpCkLoJEcdsxlok9C4rHzofc/QyPqniEo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=HtyBxp8wvqegBA+pJr+rwCHqwe0ldfjDsQLyjewLsWj7B88y/LB+D5fuxCOHX8A+0 iWOWRrokKIxoEgGSKkGh2w6NvB8WQcwWcOmsmMszuZ27ZkiR6NLzX9B7ooQHmb7npp cijvZCfAyeJ28bWZ6hHX6/rvwt3q+vhtSqJ6vHbIHqBNQf6CnuSKetBGlHzamZK6dk DO3w63dDjhf9cbHonu3nM9WFdxYKs5n5z36MiWyf/dVNWW55F1S2xL4k9fYUq/Kw8/ Cf5r/Ejt5RP/CZMWCq+X109vSaGA6CwfExB1NNMAeiqZYQzk1Wec28vxH0roGhNjGY tr/CPlztrEFBA== Date: Wed, 18 Dec 2024 13:48:43 +0100 From: Danilo Krummrich To: Jimmy Ostler Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Wedson Almeida Filho , Filipe Xavier , Valentin Obst , Daniel Sedlak , Alex Mantel , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 3/3] rust: alloc: Add doctest for `ArrayLayout` Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 > Link: https://github.com/Rust-for-Linux/linux/issues/1131 > Signed-off-by: Jimmy Ostler Reviewed-by: Danilo Krummrich > --- > 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::()` overflows or when `len * size_of::() > isize::MAX`. > + /// > + /// # Examples > + /// > + /// ``` > + /// # use kernel::alloc::layout::{ArrayLayout, LayoutError}; > + /// let layout = ArrayLayout::::new(15)?; > + /// assert_eq!(layout.len(), 15); > + /// > + /// // Errors because `len * size_of::()` overflows > + /// let layout = ArrayLayout::::new(isize::MAX as usize); > + /// assert!(layout.is_err()); > + /// > + /// // Errors because `len * size_of::() > isize::MAX`, > + /// // even though `len < isize::MAX` > + /// let layout = ArrayLayout::::new(isize::MAX as usize / 2); > + /// assert!(layout.is_err()); > + /// > + /// # Ok::<(), Error>(()) > + /// ``` > pub const fn new(len: usize) -> Result { > match len.checked_mul(core::mem::size_of::()) { > Some(size) if size <= ISIZE_MAX => { > -- > 2.47.1 >