[PATCH v6 5/7] rust: id_pool: take a NonZero capacity in with_capacity

From: Eliot Courtney

Date: Thu Aug 13 2026 - 03:37:03 EST


There is no good reason to allocate an IdPool with zero capacity.
Reflect this in IdPool::with_capacity.

Signed-off-by: Eliot Courtney <ecourtney@xxxxxxxxxx>
---
rust/kernel/id_pool.rs | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/rust/kernel/id_pool.rs b/rust/kernel/id_pool.rs
index a4a2ff417fe8..1423a5fbca69 100644
--- a/rust/kernel/id_pool.rs
+++ b/rust/kernel/id_pool.rs
@@ -29,10 +29,11 @@
/// Basic usage
///
/// ```
+/// use core::num::NonZero;
/// use kernel::alloc::AllocError;
/// use kernel::id_pool::{IdPool, UnusedId};
///
-/// let mut pool = IdPool::with_capacity(64, GFP_KERNEL)?;
+/// let mut pool = IdPool::with_capacity(NonZero::new(64).unwrap(), GFP_KERNEL)?;
/// for i in 0..64 {
/// assert_eq!(i, pool.find_unused_id(i).ok_or(ENOSPC)?.acquire());
/// }
@@ -117,8 +118,8 @@ pub fn new() -> Self {
///
/// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
#[inline]
- pub fn with_capacity(num_ids: usize, flags: Flags) -> Result<Self, AllocError> {
- let num_ids = usize::max(num_ids, BitmapVec::MAX_INLINE_LEN);
+ pub fn with_capacity(num_ids: NonZero<usize>, flags: Flags) -> Result<Self, AllocError> {
+ let num_ids = usize::max(num_ids.get(), BitmapVec::MAX_INLINE_LEN);
let map = BitmapVec::new(num_ids, flags)?;
Ok(Self { map })
}
@@ -138,6 +139,7 @@ pub fn capacity(&self) -> usize {
/// # Examples
///
/// ```
+ /// use core::num::NonZero;
/// use kernel::{
/// alloc::AllocError,
/// bitmap::BitmapVec,
@@ -147,7 +149,7 @@ pub fn capacity(&self) -> usize {
/// },
/// };
///
- /// let mut pool = IdPool::with_capacity(1024, GFP_KERNEL)?;
+ /// let mut pool = IdPool::with_capacity(NonZero::new(1024).unwrap(), GFP_KERNEL)?;
/// let alloc_request = pool.shrink_request().ok_or(AllocError)?;
/// let resizer = alloc_request.realloc(GFP_KERNEL)?;
/// pool.shrink(resizer);

--
2.55.0