Re: [PATCH 3/3] rust: maple_tree: add MapleTreeAlloc
From: Gary Guo
Date: Sat Jul 26 2025 - 11:55:01 EST
On Sat, 26 Jul 2025 13:23:24 +0000
Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> To support allocation trees, we introduce a new type MapleTreeAlloc for
> the case where the tree is created using MT_FLAGS_ALLOC_RANGE. To ensure
> that you can only call mtree_alloc_range on an allocation tree, we
> restrict thta method to the new MapleTreeAlloc type. However, all
> methods on MapleTree remain accessible to MapleTreeAlloc as allocation
> trees can use the other methods without issues.
>
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> ---
> rust/kernel/maple_tree.rs | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 158 insertions(+)
>
> diff --git a/rust/kernel/maple_tree.rs b/rust/kernel/maple_tree.rs
> index c7ef504a9c78065b3d5752b4f5337fb6277182d1..8c025d2c395b6d57f1fb16214b4e87d4e7942d6f 100644
> --- a/rust/kernel/maple_tree.rs
> +++ b/rust/kernel/maple_tree.rs
>
> /// Error type for failure to insert a new value.
> pub struct InsertError<T> {
> /// The value that could not be inserted.
> @@ -378,3 +499,40 @@ fn from(insert_err: InsertError<T>) -> Error {
> Error::from(insert_err.cause)
> }
> }
> +
> +/// Error type for failure to insert a new value.
> +pub struct AllocError<T> {
> + /// The value that could not be inserted.
> + pub value: T,
> + /// The reason for the failure to insert.
> + pub cause: AllocErrorKind,
> +}
> +
> +/// The reason for the failure to insert.
> +#[derive(PartialEq, Eq, Copy, Clone)]
> +pub enum AllocErrorKind {
> + /// There is not enough space for the requested allocation.
> + Busy,
> + /// Failure to allocate memory.
> + Nomem,
> + /// The insertion request was invalid.
> + InvalidRequest,
> +}
> +
> +impl From<AllocErrorKind> for Error {
> + #[inline]
> + fn from(kind: AllocErrorKind) -> Error {
> + match kind {
> + AllocErrorKind::Busy => EBUSY,
> + AllocErrorKind::Nomem => ENOMEM,
> + AllocErrorKind::InvalidRequest => EINVAL,
> + }
> + }
> +}
> +
> +impl<T> From<AllocError<T>> for Error {
> + #[inline]
> + fn from(insert_err: AllocError<T>) -> Error {
> + Error::from(insert_err.cause)
> + }
> +}
>
This looks identical to `InsertError`?
Best,
Gary