summaryrefslogtreecommitdiff
path: root/rust/kernel/alloc/layout.rs
diff options
context:
space:
mode:
authorDmitry Torokhov <dmitry.torokhov@gmail.com>2025-04-04 23:04:35 -0700
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2025-04-04 23:04:35 -0700
commit946661e3bef8efa11ba8079d4ebafe6fc3b0aaad (patch)
treea90605abb7bb65503a2d3f93a79e19a01aaa5e89 /rust/kernel/alloc/layout.rs
parentfd10709e28d2fa9015667aee56d92099fc97aa0d (diff)
parent4d395cb071a343196ca524d3694790f06978fe91 (diff)
Merge branch 'next' into for-linus
Prepare input updates for 6.15 merge window.
Diffstat (limited to 'rust/kernel/alloc/layout.rs')
-rw-r--r--rust/kernel/alloc/layout.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
index 4b3cd7fdc816c..93ed514f7cc7e 100644
--- a/rust/kernel/alloc/layout.rs
+++ b/rust/kernel/alloc/layout.rs
@@ -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 => {