Simplify current codes with TryFromNum

This commit is contained in:
Jianfeng Jiang
2023-04-17 16:11:36 +08:00
committed by Tate, Hongliang Tian
parent 6ecc7dc557
commit 6cea03b871
16 changed files with 82 additions and 188 deletions

69
Cargo.lock generated
View File

@ -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"

View File

@ -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]

View File

@ -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,
};

View File

@ -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,

View File

@ -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

View File

@ -33,7 +33,7 @@
//! }
//! }
//! ```
//!
//!
#![no_std]
@ -44,4 +44,4 @@ pub enum TryFromIntError {
}
#[cfg(feature = "derive")]
pub use int_to_c_enum_derive::TryFromInt;
pub use int_to_c_enum_derive::TryFromInt;

View File

@ -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"] }

View File

@ -256,6 +256,12 @@ impl From<alloc::ffi::NulError> for Error {
}
}
impl From<int_to_c_enum::TryFromIntError> 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) => {

View File

@ -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<i32> for FcntlCmd {
type Error = Error;
fn try_from(value: i32) -> Result<Self> {
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,
}

View File

@ -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<u32> for IoctlCmd {
type Error = Error;
fn try_from(value: u32) -> Result<Self> {
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,
}

View File

@ -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;

View File

@ -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<u32> for ResourceType {
type Error = Error;
fn try_from(value: u32) -> Result<Self> {
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)]

View File

@ -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<u64> for ArchPrctlCode {
type Error = Error;
fn try_from(value: u64) -> Result<Self> {
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<SyscallReturn> {
log_syscall_entry!(SYS_ARCH_PRCTL);
let arch_prctl_code = ArchPrctlCode::try_from(code)?;

View File

@ -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<i32> for MadviseBehavior {
type Error = Error;
fn try_from(value: i32) -> Result<Self> {
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)
}
}

View File

@ -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<u32> for MaskOp {
type Error = Error;
fn try_from(value: u32) -> Result<Self> {
let op = match value {
0 => MaskOp::Block,
1 => MaskOp::Unblock,
2 => MaskOp::SetMask,
_ => return_errno_with_message!(Errno::EINVAL, "invalid mask op"),
};
Ok(op)
}
}

View File

@ -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<clockid_t> for ClockID {
type Error = Error;
fn try_from(value: clockid_t) -> Result<Self> {
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 {