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

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