From ac17cc14659ea407ed90c5fafd859991585d9546 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 2 Jan 2021 15:23:11 -0500 Subject: [PATCH] Provide a From impl for KernelError This makes the try_alloc functions trivial, so we inline them. --- drivers/char/rust_example/src/lib.rs | 6 +++--- rust/kernel/src/error.rs | 8 ++++++++ rust/kernel/src/lib.rs | 14 -------------- rust/kernel/src/miscdev.rs | 2 +- rust/kernel/src/sysctl.rs | 3 +-- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/drivers/char/rust_example/src/lib.rs b/drivers/char/rust_example/src/lib.rs index 2c15dd163ac5ac..61aa3d98a4c84d 100644 --- a/drivers/char/rust_example/src/lib.rs +++ b/drivers/char/rust_example/src/lib.rs @@ -1,14 +1,14 @@ // SPDX-License-Identifier: GPL-2.0 #![no_std] -#![feature(global_asm)] +#![feature(allocator_api, global_asm)] extern crate alloc; use alloc::boxed::Box; use core::pin::Pin; use kernel::prelude::*; -use kernel::{cstr, file_operations::FileOperations, miscdev, try_alloc}; +use kernel::{cstr, file_operations::FileOperations, miscdev}; module! { type: RustExample, @@ -37,7 +37,7 @@ impl FileOperations for RustFile { fn open() -> KernelResult { println!("rust file was opened!"); - try_alloc(Self) + Ok(Box::try_new(Self)?) } } diff --git a/rust/kernel/src/error.rs b/rust/kernel/src/error.rs index 95e322e39a88e7..5599ab066ac0d4 100644 --- a/rust/kernel/src/error.rs +++ b/rust/kernel/src/error.rs @@ -2,6 +2,8 @@ use core::num::TryFromIntError; +use alloc::alloc::AllocError; + use crate::bindings; use crate::c_types; @@ -30,3 +32,9 @@ impl From for Error { } pub type KernelResult = Result; + +impl From for Error { + fn from(_: AllocError) -> Error { + Error::ENOMEM + } +} diff --git a/rust/kernel/src/lib.rs b/rust/kernel/src/lib.rs index e7a0dfa7f81062..74467ee9b1bf1e 100644 --- a/rust/kernel/src/lib.rs +++ b/rust/kernel/src/lib.rs @@ -12,9 +12,7 @@ compile_error!("Missing kernel configuration for conditional compilation"); extern crate alloc; -use alloc::boxed::Box; use core::panic::PanicInfo; -use core::pin::Pin; mod allocator; pub mod bindings; @@ -59,15 +57,3 @@ fn panic(_info: &PanicInfo) -> ! { #[global_allocator] static ALLOCATOR: allocator::KernelAllocator = allocator::KernelAllocator; - -/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is -/// moved into it and returned to the caller wrapped in a `Box`. -pub fn try_alloc(value: T) -> KernelResult> { - Box::try_new(value).map_err(|_| Error::ENOMEM) -} - -/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is -/// moved into it and returned to the caller wrapped in a pinned `Box`. -pub fn try_alloc_pinned(value: T) -> KernelResult>> { - Ok(Pin::from(try_alloc(value)?)) -} diff --git a/rust/kernel/src/miscdev.rs b/rust/kernel/src/miscdev.rs index 6d907972e785b2..632b24c67f864d 100644 --- a/rust/kernel/src/miscdev.rs +++ b/rust/kernel/src/miscdev.rs @@ -28,7 +28,7 @@ impl Registration { name: CStr<'static>, minor: Option, ) -> KernelResult>> { - let mut r = crate::try_alloc_pinned(Self::new())?; + let mut r = Pin::from(Box::try_new(Self::new())?); r.as_mut().register::(name, minor)?; Ok(r) } diff --git a/rust/kernel/src/sysctl.rs b/rust/kernel/src/sysctl.rs index 7b05d5e8066e2d..a4c6086270fcbe 100644 --- a/rust/kernel/src/sysctl.rs +++ b/rust/kernel/src/sysctl.rs @@ -9,7 +9,6 @@ use core::sync::atomic; use crate::bindings; use crate::c_types; use crate::error; -use crate::try_alloc; use crate::types; use crate::user_ptr::{UserSlicePtr, UserSlicePtrWriter}; @@ -130,7 +129,7 @@ impl Sysctl { return Err(error::Error::EINVAL); } - let storage = try_alloc(storage)?; + let storage = Box::try_new(storage)?; let mut table = vec![ bindings::ctl_table { procname: name.as_ptr() as *const i8,