From 6cea03b871650042fb2e81520d2fd4d76686930a Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Mon, 17 Apr 2023 16:11:36 +0800 Subject: [PATCH] Simplify current codes with TryFromNum --- Cargo.lock | 69 +++++++++++-------- services/comps/virtio/Cargo.toml | 1 + .../comps/virtio/src/device/block/device.rs | 12 ++-- services/comps/virtio/src/device/block/mod.rs | 11 +-- services/libs/int-to-c-enum/derive/src/lib.rs | 4 +- services/libs/int-to-c-enum/src/lib.rs | 4 +- services/libs/jinux-std/Cargo.toml | 1 + services/libs/jinux-std/src/error.rs | 6 ++ services/libs/jinux-std/src/fs/utils/fcntl.rs | 32 ++------- services/libs/jinux-std/src/fs/utils/ioctl.rs | 30 ++------ services/libs/jinux-std/src/prelude.rs | 1 + services/libs/jinux-std/src/process/rlimit.rs | 28 +------- .../libs/jinux-std/src/syscall/arch_prctl.rs | 17 +---- .../libs/jinux-std/src/syscall/madvise.rs | 18 +---- .../jinux-std/src/syscall/rt_sigprocmask.rs | 16 +---- services/libs/jinux-std/src/time/mod.rs | 20 +----- 16 files changed, 82 insertions(+), 188 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88e48ddbc..3382080c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,7 +128,7 @@ dependencies = [ "json", "proc-macro2", "quote", - "syn", + "syn 1.0.109", "toml", ] @@ -138,7 +138,7 @@ version = "0.1.0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -160,7 +160,7 @@ version = "0.1.25" source = "git+https://github.com/sdww0/rust-ctor#a6ee5e7a69cb368f00e7df00108028635fdf3fbd" dependencies = [ "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -189,7 +189,7 @@ checksum = "41973d4c45f7a35af8753ba3457cc99d406d863941fd7f52663cff54a5ab99b3" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -217,6 +217,22 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "int-to-c-enum" +version = "0.1.0" +dependencies = [ + "int-to-c-enum-derive", +] + +[[package]] +name = "int-to-c-enum-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "intrusive-collections" version = "0.9.5" @@ -349,7 +365,7 @@ version = "0.1.0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -361,6 +377,7 @@ dependencies = [ "bitflags", "controlled", "cpio-decoder", + "int-to-c-enum", "intrusive-collections", "jinux-block", "jinux-frame", @@ -407,6 +424,7 @@ version = "0.1.0" dependencies = [ "bitflags", "component", + "int-to-c-enum", "jinux-frame", "jinux-pci", "jinux-util", @@ -513,23 +531,23 @@ source = "git+https://github.com/jinzhao-dev/pod?rev=7fa2ed2#7fa2ed2bb8344856f98 dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -639,6 +657,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tap" version = "1.0.1" @@ -662,7 +691,7 @@ checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -733,7 +762,7 @@ dependencies = [ "itertools", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -758,22 +787,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79" -[[package]] -name = "value-enum" -version = "0.1.0" -dependencies = [ - "value-enum-derive", -] - -[[package]] -name = "value-enum-derive" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "version_check" version = "0.9.4" diff --git a/services/comps/virtio/Cargo.toml b/services/comps/virtio/Cargo.toml index 11546d501..67d76fb85 100644 --- a/services/comps/virtio/Cargo.toml +++ b/services/comps/virtio/Cargo.toml @@ -14,5 +14,6 @@ jinux-util = { path = "../../libs/jinux-util" } pod = { git = "https://github.com/jinzhao-dev/pod", rev = "7fa2ed2" } component = { path = "../../libs/comp-sys/component" } log = "0.4" +int-to-c-enum = { path = "../../libs/int-to-c-enum" } [features] diff --git a/services/comps/virtio/src/device/block/device.rs b/services/comps/virtio/src/device/block/device.rs index d81a171e6..64648436f 100644 --- a/services/comps/virtio/src/device/block/device.rs +++ b/services/comps/virtio/src/device/block/device.rs @@ -60,7 +60,7 @@ impl BLKDevice { pub fn read_block(&mut self, block_id: usize, buf: &mut [u8]) { assert_eq!(buf.len(), BLK_SIZE); let req = BlkReq { - type_: ReqType::In, + type_: ReqType::In as _, reserved: 0, sector: block_id as u64, }; @@ -76,7 +76,7 @@ impl BLKDevice { self.queue .pop_used_with_token(token) .expect("pop used failed"); - match resp.status { + match RespStatus::try_from(resp.status).unwrap() { RespStatus::Ok => {} _ => panic!("io error in block device"), }; @@ -85,7 +85,7 @@ impl BLKDevice { pub fn write_block(&mut self, block_id: usize, buf: &[u8]) { assert_eq!(buf.len(), BLK_SIZE); let req = BlkReq { - type_: ReqType::Out, + type_: ReqType::Out as _, reserved: 0, sector: block_id as u64, }; @@ -102,7 +102,7 @@ impl BLKDevice { .pop_used_with_token(token) .expect("pop used failed"); let st = resp.status; - match st { + match RespStatus::try_from(st).unwrap() { RespStatus::Ok => {} _ => panic!("io error in block device:{:?}", st), }; @@ -127,7 +127,7 @@ impl BLKDevice { ) -> u16 { assert_eq!(buf.len(), BLK_SIZE); *req = BlkReq { - type_: ReqType::In, + type_: ReqType::In as _, reserved: 0, sector: block_id as u64, }; @@ -150,7 +150,7 @@ impl BLKDevice { ) -> u16 { assert_eq!(buf.len(), BLK_SIZE); *req = BlkReq { - type_: ReqType::Out, + type_: ReqType::Out as _, reserved: 0, sector: block_id as u64, }; diff --git a/services/comps/virtio/src/device/block/mod.rs b/services/comps/virtio/src/device/block/mod.rs index 11475aafc..957527862 100644 --- a/services/comps/virtio/src/device/block/mod.rs +++ b/services/comps/virtio/src/device/block/mod.rs @@ -1,6 +1,7 @@ pub mod device; use bitflags::bitflags; +use int_to_c_enum::TryFromInt; use jinux_pci::capability::vendor::virtio::CapabilityVirtioData; use jinux_pci::util::BAR; use jinux_util::frame_ptr::InFramePtr; @@ -29,7 +30,7 @@ bitflags! { #[repr(C)] #[derive(Debug, Copy, Clone, Pod)] pub struct BlkReq { - pub type_: ReqType, + pub type_: u32, pub reserved: u32, pub sector: u64, } @@ -38,19 +39,19 @@ pub struct BlkReq { #[repr(C)] #[derive(Debug, Copy, Clone, Pod)] pub struct BlkResp { - pub status: RespStatus, + pub status: u8, } impl Default for BlkResp { fn default() -> Self { BlkResp { - status: RespStatus::_NotReady, + status: RespStatus::_NotReady as _, } } } #[repr(u32)] -#[derive(Debug, Copy, Clone, Pod)] +#[derive(Debug, Copy, Clone, TryFromInt)] pub enum ReqType { In = 0, Out = 1, @@ -60,7 +61,7 @@ pub enum ReqType { } #[repr(u8)] -#[derive(Debug, Eq, PartialEq, Copy, Clone, Pod)] +#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromInt)] pub enum RespStatus { /// Ok. Ok = 0, diff --git a/services/libs/int-to-c-enum/derive/src/lib.rs b/services/libs/int-to-c-enum/derive/src/lib.rs index 317f7dfb3..2d9393840 100644 --- a/services/libs/int-to-c-enum/derive/src/lib.rs +++ b/services/libs/int-to-c-enum/derive/src/lib.rs @@ -71,7 +71,9 @@ fn fn_body_tokens(value_name: &str, data_enum: &DataEnum, ident: Ident) -> Token let statement = quote!(#value => ::core::result::Result::Ok(#ident::#vairant_ident),); match_bodys.append_all(statement); } - match_bodys.append_all(quote!(_ => core::result::Result::Err(::int_to_c_enum::TryFromIntError::InvalidValue),)); + match_bodys.append_all( + quote!(_ => core::result::Result::Err(::int_to_c_enum::TryFromIntError::InvalidValue),), + ); let param = format_ident!("{}", value_name); quote!(match #param { #match_bodys diff --git a/services/libs/int-to-c-enum/src/lib.rs b/services/libs/int-to-c-enum/src/lib.rs index 58f86f2a8..11de5dda6 100644 --- a/services/libs/int-to-c-enum/src/lib.rs +++ b/services/libs/int-to-c-enum/src/lib.rs @@ -33,7 +33,7 @@ //! } //! } //! ``` -//! +//! #![no_std] @@ -44,4 +44,4 @@ pub enum TryFromIntError { } #[cfg(feature = "derive")] -pub use int_to_c_enum_derive::TryFromInt; \ No newline at end of file +pub use int_to_c_enum_derive::TryFromInt; diff --git a/services/libs/jinux-std/Cargo.toml b/services/libs/jinux-std/Cargo.toml index 56cb2f487..677ef209d 100644 --- a/services/libs/jinux-std/Cargo.toml +++ b/services/libs/jinux-std/Cargo.toml @@ -17,6 +17,7 @@ typeflags = { path = "../typeflags" } typeflags-util = { path = "../typeflags-util" } jinux-rights-proc = { path = "../jinux-rights-proc" } jinux-util = { path = "../jinux-util" } +int-to-c-enum = { path = "../../libs/int-to-c-enum" } cpio-decoder = { path = "../cpio-decoder" } virtio-input-decoder = "0.1.4" ascii = { version = "1.1", default-features = false, features = ["alloc"] } diff --git a/services/libs/jinux-std/src/error.rs b/services/libs/jinux-std/src/error.rs index 06516b5cc..f06461595 100644 --- a/services/libs/jinux-std/src/error.rs +++ b/services/libs/jinux-std/src/error.rs @@ -256,6 +256,12 @@ impl From for Error { } } +impl From for Error { + fn from(_: int_to_c_enum::TryFromIntError) -> Self { + Error::with_message(Errno::EINVAL, "Invalid enum value") + } +} + #[macro_export] macro_rules! return_errno { ($errno: expr) => { diff --git a/services/libs/jinux-std/src/fs/utils/fcntl.rs b/services/libs/jinux-std/src/fs/utils/fcntl.rs index 3c94a4115..b7e80f3a6 100644 --- a/services/libs/jinux-std/src/fs/utils/fcntl.rs +++ b/services/libs/jinux-std/src/fs/utils/fcntl.rs @@ -1,33 +1,11 @@ use crate::prelude::*; -macro_rules! define_fcntl_cmd { - ($($name: ident = $value: expr),*) => { - #[repr(i32)] - #[derive(Debug, Clone, Copy)] - #[allow(non_camel_case_types)] - pub enum FcntlCmd { - $($name = $value,)* - } - - $( - pub const $name: i32 = $value; - )* - - impl TryFrom for FcntlCmd { - type Error = Error; - fn try_from(value: i32) -> Result { - match value { - $($name => Ok(FcntlCmd::$name),)* - _ => return_errno_with_message!(Errno::EINVAL, "Unknown fcntl cmd"), - } - } - } - } -} - -define_fcntl_cmd! { +#[repr(i32)] +#[derive(Debug, Clone, Copy, TryFromInt)] +#[allow(non_camel_case_types)] +pub enum FcntlCmd { F_DUPFD = 0, F_GETFD = 1, F_SETFD = 2, - F_DUPFD_CLOEXEC = 1030 + F_DUPFD_CLOEXEC = 1030, } diff --git a/services/libs/jinux-std/src/fs/utils/ioctl.rs b/services/libs/jinux-std/src/fs/utils/ioctl.rs index f74aea6f9..5489dd98d 100644 --- a/services/libs/jinux-std/src/fs/utils/ioctl.rs +++ b/services/libs/jinux-std/src/fs/utils/ioctl.rs @@ -1,30 +1,8 @@ use crate::prelude::*; -macro_rules! define_ioctl_cmd { - ($($name: ident = $value: expr),*) => { - #[repr(u32)] - #[derive(Debug, Clone, Copy)] - pub enum IoctlCmd { - $($name = $value,)* - } - - $( - pub const $name: u32 = $value; - )* - - impl TryFrom for IoctlCmd { - type Error = Error; - fn try_from(value: u32) -> Result { - match value { - $($name => Ok(IoctlCmd::$name),)* - _ => return_errno!(Errno::EINVAL), - } - } - } - } -} - -define_ioctl_cmd! { +#[repr(u32)] +#[derive(Debug, Clone, Copy, TryFromInt)] +pub enum IoctlCmd { // Get terminal attributes TCGETS = 0x5401, TCSETS = 0x5402, @@ -34,5 +12,5 @@ define_ioctl_cmd! { TIOCSPGRP = 0x5410, // Set window size TIOCGWINSZ = 0x5413, - TIOCSWINSZ = 0x5414 + TIOCSWINSZ = 0x5414, } diff --git a/services/libs/jinux-std/src/prelude.rs b/services/libs/jinux-std/src/prelude.rs index 6ca7eb737..aa3851c89 100644 --- a/services/libs/jinux-std/src/prelude.rs +++ b/services/libs/jinux-std/src/prelude.rs @@ -15,6 +15,7 @@ pub(crate) use alloc::vec::Vec; pub(crate) use bitflags::bitflags; pub(crate) use core::any::Any; pub(crate) use core::ffi::CStr; +pub(crate) use int_to_c_enum::TryFromInt; pub(crate) use jinux_frame::config::PAGE_SIZE; pub(crate) use jinux_frame::sync::{Mutex, MutexGuard}; pub(crate) use jinux_frame::vm::Vaddr; diff --git a/services/libs/jinux-std/src/process/rlimit.rs b/services/libs/jinux-std/src/process/rlimit.rs index 4760d6633..3c8b0f7d7 100644 --- a/services/libs/jinux-std/src/process/rlimit.rs +++ b/services/libs/jinux-std/src/process/rlimit.rs @@ -37,7 +37,7 @@ impl Default for ResourceLimits { } #[repr(u32)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, TryFromInt)] pub enum ResourceType { RLIMIT_CPU = 0, RLIMIT_FSIZE = 1, @@ -57,32 +57,6 @@ pub enum ResourceType { RLIMIT_RTTIME = 15, } -impl TryFrom for ResourceType { - type Error = Error; - - fn try_from(value: u32) -> Result { - match value { - 0 => Ok(ResourceType::RLIMIT_CPU), - 1 => Ok(ResourceType::RLIMIT_FSIZE), - 2 => Ok(ResourceType::RLIMIT_DATA), - 3 => Ok(ResourceType::RLIMIT_STACK), - 4 => Ok(ResourceType::RLIMIT_CORE), - 5 => Ok(ResourceType::RLIMIT_RSS), - 6 => Ok(ResourceType::RLIMIT_NPROC), - 7 => Ok(ResourceType::RLIMIT_NOFILE), - 8 => Ok(ResourceType::RLIMIT_MEMLOCK), - 9 => Ok(ResourceType::RLIMIT_AS), - 10 => Ok(ResourceType::RLIMIT_LOCKS), - 11 => Ok(ResourceType::RLIMIT_SIGPENDING), - 12 => Ok(ResourceType::RLIMIT_MSGQUEUE), - 13 => Ok(ResourceType::RLIMIT_NICE), - 14 => Ok(ResourceType::RLIMIT_RTPRIO), - 15 => Ok(ResourceType::RLIMIT_RTTIME), - _ => return_errno_with_message!(Errno::EINVAL, "invalid resource type"), - } - } -} - pub const RLIMIT_COUNT: usize = 16; #[derive(Debug, Clone, Copy, Pod)] diff --git a/services/libs/jinux-std/src/syscall/arch_prctl.rs b/services/libs/jinux-std/src/syscall/arch_prctl.rs index c1b962733..9e9f8c119 100644 --- a/services/libs/jinux-std/src/syscall/arch_prctl.rs +++ b/services/libs/jinux-std/src/syscall/arch_prctl.rs @@ -6,7 +6,8 @@ use crate::{log_syscall_entry, prelude::*}; use super::SyscallReturn; #[allow(non_camel_case_types)] -#[derive(Debug)] +#[repr(u64)] +#[derive(Debug, TryFromInt)] pub enum ArchPrctlCode { ARCH_SET_GS = 0x1001, ARCH_SET_FS = 0x1002, @@ -14,20 +15,6 @@ pub enum ArchPrctlCode { ARCH_GET_GS = 0x1004, } -impl TryFrom for ArchPrctlCode { - type Error = Error; - - fn try_from(value: u64) -> Result { - match value { - 0x1001 => Ok(ArchPrctlCode::ARCH_SET_GS), - 0x1002 => Ok(ArchPrctlCode::ARCH_SET_FS), - 0x1003 => Ok(ArchPrctlCode::ARCH_GET_FS), - 0x1004 => Ok(ArchPrctlCode::ARCH_GET_GS), - _ => return_errno_with_message!(Errno::EINVAL, "Unknown code for arch_prctl"), - } - } -} - pub fn sys_arch_prctl(code: u64, addr: u64, context: &mut UserContext) -> Result { log_syscall_entry!(SYS_ARCH_PRCTL); let arch_prctl_code = ArchPrctlCode::try_from(code)?; diff --git a/services/libs/jinux-std/src/syscall/madvise.rs b/services/libs/jinux-std/src/syscall/madvise.rs index 5f0572138..491b7fc6a 100644 --- a/services/libs/jinux-std/src/syscall/madvise.rs +++ b/services/libs/jinux-std/src/syscall/madvise.rs @@ -38,7 +38,7 @@ fn madv_dontneed(start: Vaddr, len: usize) -> Result<()> { } #[repr(i32)] -#[derive(Debug, Clone, Copy, Pod)] +#[derive(Debug, Clone, Copy, TryFromInt)] #[allow(non_camel_case_types)] /// This definition is the same from linux pub enum MadviseBehavior { @@ -77,19 +77,3 @@ pub enum MadviseBehavior { MADV_DONTNEED_LOCKED = 24, /* like DONTNEED, but drop locked pages too */ } - -impl TryFrom for MadviseBehavior { - type Error = Error; - - fn try_from(value: i32) -> Result { - let behavior = match value { - 0 => MadviseBehavior::MADV_NORMAL, - 1 => MadviseBehavior::MADV_RANDOM, - 2 => MadviseBehavior::MADV_SEQUENTIAL, - 3 => MadviseBehavior::MADV_WILLNEED, - 4 => MadviseBehavior::MADV_DONTNEED, - _ => return_errno_with_message!(Errno::EINVAL, "invalid madvise behavior"), - }; - Ok(behavior) - } -} diff --git a/services/libs/jinux-std/src/syscall/rt_sigprocmask.rs b/services/libs/jinux-std/src/syscall/rt_sigprocmask.rs index cab5930f4..bd7acb08f 100644 --- a/services/libs/jinux-std/src/syscall/rt_sigprocmask.rs +++ b/services/libs/jinux-std/src/syscall/rt_sigprocmask.rs @@ -64,24 +64,10 @@ fn do_rt_sigprocmask( Ok(()) } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromInt)] #[repr(u32)] pub enum MaskOp { Block = 0, Unblock = 1, SetMask = 2, } - -impl TryFrom for MaskOp { - type Error = Error; - - fn try_from(value: u32) -> Result { - let op = match value { - 0 => MaskOp::Block, - 1 => MaskOp::Unblock, - 2 => MaskOp::SetMask, - _ => return_errno_with_message!(Errno::EINVAL, "invalid mask op"), - }; - Ok(op) - } -} diff --git a/services/libs/jinux-std/src/time/mod.rs b/services/libs/jinux-std/src/time/mod.rs index d8558e2c2..cd823f551 100644 --- a/services/libs/jinux-std/src/time/mod.rs +++ b/services/libs/jinux-std/src/time/mod.rs @@ -11,7 +11,7 @@ pub type time_t = i64; pub type suseconds_t = i64; pub type clock_t = i64; -#[derive(Debug, Copy, Clone, Pod)] +#[derive(Debug, Copy, Clone, TryFromInt)] #[repr(i32)] pub enum ClockID { CLOCK_REALTIME = 0, @@ -24,24 +24,6 @@ pub enum ClockID { CLOCK_BOOTTIME = 7, } -impl TryFrom for ClockID { - type Error = Error; - - fn try_from(value: clockid_t) -> Result { - Ok(match value as i32 { - 0 => ClockID::CLOCK_REALTIME, - 1 => ClockID::CLOCK_MONOTONIC, - 2 => ClockID::CLOCK_PROCESS_CPUTIME_ID, - 3 => ClockID::CLOCK_THREAD_CPUTIME_ID, - 4 => ClockID::CLOCK_MONOTONIC_RAW, - 5 => ClockID::CLOCK_REALTIME_COARSE, - 6 => ClockID::CLOCK_MONOTONIC_COARSE, - 7 => ClockID::CLOCK_BOOTTIME, - _ => return_errno_with_message!(Errno::EINVAL, "invalid clockid"), - }) - } -} - #[repr(C)] #[derive(Debug, Default, Copy, Clone, Pod)] pub struct timespec_t {