Reimplement SyscallFunction/Error enums using macros

This commit is contained in:
Mark Poliakov 2023-07-18 23:21:09 +03:00
parent 7ed747351a
commit e88a2857e1
4 changed files with 60 additions and 97 deletions

View File

@ -1,16 +1,16 @@
use crate::io::RawFd;
use crate::{io::RawFd, primitive_enum};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Error {
primitive_enum!(pub enum Error: u32 {
OutOfMemory = 1,
InvalidMemoryOperation,
AlreadyExists,
TimedOut,
InvalidArgument,
DoesNotExist,
IsADirectory,
InvalidFile,
}
InvalidMemoryOperation = 2,
AlreadyExists = 3,
TimedOut = 4,
InvalidArgument = 5,
DoesNotExist = 6,
IsADirectory = 7,
InvalidFile = 8,
NotImplemented = 9,
});
pub trait FromSyscallResult: Sized {
fn from_syscall_result(value: usize) -> Result<Self, Error>;
@ -25,40 +25,6 @@ pub trait SyscallError {
fn into_syscall_error(self) -> usize;
}
impl TryFrom<u32> for Error {
type Error = ();
fn try_from(value: u32) -> Result<Self, ()> {
match value {
1 => Ok(Self::OutOfMemory),
2 => Ok(Self::InvalidMemoryOperation),
3 => Ok(Self::AlreadyExists),
4 => Ok(Self::TimedOut),
5 => Ok(Self::InvalidArgument),
6 => Ok(Self::DoesNotExist),
7 => Ok(Self::IsADirectory),
8 => Ok(Self::InvalidFile),
_ => Err(()),
}
}
}
impl From<Error> for u32 {
fn from(value: Error) -> Self {
match value {
Error::OutOfMemory => 1,
Error::InvalidMemoryOperation => 2,
Error::AlreadyExists => 3,
Error::TimedOut => 4,
Error::InvalidArgument => 5,
Error::DoesNotExist => 6,
Error::IsADirectory => 7,
Error::InvalidFile => 8,
}
}
}
impl SyscallError for Error {
fn from_syscall_error(value: usize) -> Self {
Error::try_from((-(value as isize)) as u32).unwrap_or(Error::InvalidArgument)

View File

@ -1,57 +1,9 @@
#![no_std]
#![feature(trace_macros)]
pub(crate) mod macros;
pub mod error;
pub mod io;
pub mod path;
#[derive(Clone, Copy, Debug)]
pub enum SyscallFunction {
Exit = 1,
Nanosleep = 2,
MapMemory = 3,
UnmapMemory = 4,
Write = 5,
Read = 6,
Open = 7,
Close = 8,
DebugTrace = 128,
}
impl TryFrom<usize> for SyscallFunction {
type Error = ();
fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
1 => Ok(Self::Exit),
2 => Ok(Self::Nanosleep),
3 => Ok(Self::MapMemory),
4 => Ok(Self::UnmapMemory),
5 => Ok(Self::Write),
6 => Ok(Self::Read),
7 => Ok(Self::Open),
8 => Ok(Self::Close),
128 => Ok(Self::DebugTrace),
_ => Err(()),
}
}
}
impl From<SyscallFunction> for usize {
fn from(value: SyscallFunction) -> Self {
match value {
SyscallFunction::Exit => 1,
SyscallFunction::Nanosleep => 2,
SyscallFunction::MapMemory => 3,
SyscallFunction::UnmapMemory => 4,
SyscallFunction::Write => 5,
SyscallFunction::Read => 6,
SyscallFunction::Open => 7,
SyscallFunction::Close => 8,
SyscallFunction::DebugTrace => 128,
}
}
}
pub mod syscall;

31
src/macros.rs Normal file
View File

@ -0,0 +1,31 @@
#[macro_export]
macro_rules! primitive_enum {
($vis:vis enum $name:ident: $repr:ty { $( $variant:ident = $discriminant:literal, )+ }) => {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr($repr)]
$vis enum $name {
$(
$variant = $discriminant
),+
}
impl TryFrom<$repr> for $name {
type Error = ();
fn try_from(v: $repr) -> Result<$name, ()> {
match v {
$(
$discriminant => Ok($name::$variant)
,)+
_ => Err(())
}
}
}
impl From<$name> for $repr {
fn from(v: $name) -> $repr {
v as $repr
}
}
};
}

14
src/syscall.rs Normal file
View File

@ -0,0 +1,14 @@
use crate::primitive_enum;
primitive_enum!(pub enum SyscallFunction: usize {
Exit = 1,
Nanosleep = 2,
MapMemory = 3,
UnmapMemory = 4,
Write = 5,
Read = 6,
Open = 7,
Close = 8,
DebugTrace = 128,
});