Skip to content

Commit

Permalink
Makes errno fully derivable
Browse files Browse the repository at this point in the history
  • Loading branch information
tompro committed Feb 24, 2024
1 parent 57e4ab2 commit d8c94a6
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/kernel/src/errno.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::convert::Infallible;
use std::error::Error;
use std::num::NonZeroI32;

use macros::Errno;
use thiserror::Error;

// This file contains errno used in a PS4 system. The value of each errno must be the same as the
// PS4.

Expand Down
1 change: 0 additions & 1 deletion src/kernel/src/fs/dev/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::fs::{
};
use crate::process::VThread;
use macros::Errno;
use std::num::NonZeroI32;
use std::sync::Arc;
use thiserror::Error;

Expand Down
2 changes: 2 additions & 0 deletions src/kernel/src/fs/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ enum MountError {
#[derive(Debug, Error, Errno)]
enum GetVnodeError {
#[error("cannot open the specified file")]
#[errno(EIO)]
OpenFileFailed(#[source] std::io::Error),

#[error("cannot determine file type")]
#[errno(EIO)]
GetFileTypeFailed(#[source] std::io::Error),
}
2 changes: 1 addition & 1 deletion src/kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ pub enum OpenError {
#[derive(Debug, Error, Errno)]
pub enum WriteError {}

#[derive(Debug, Error)]
#[derive(Debug, Error, Errno)]
pub enum IoctlError {
#[error("Couldn't get file")]
FailedToGetFile(#[from] GetFileError),
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/fs/tmp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use self::node::{AllocNodeError, Node, Nodes};
use super::{Filesystem, FsConfig, Mount, MountFlags, MountOpts, MountSource, VPathBuf, Vnode};
use crate::errno::{Errno, EINVAL};
use crate::ucred::{Ucred, Uid};
use std::num::NonZeroI32;
use macros::Errno;
use std::sync::atomic::AtomicI32;
use std::sync::Arc;
use thiserror::Error;
Expand Down
13 changes: 6 additions & 7 deletions src/macros/src/errno.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::{punctuated::Punctuated, Fields, ItemEnum, Meta, Token, Variant};
use syn::{punctuated::Punctuated, Fields, ItemEnum, Meta, Token, Variant, Index};

pub fn transform(arg: ItemEnum) -> syn::Result<TokenStream> {
let enum_name = &arg.ident;
Expand All @@ -12,7 +12,7 @@ pub fn transform(arg: ItemEnum) -> syn::Result<TokenStream> {
.collect::<Result<Vec<_>, _>>()?;

if arms.is_empty() {
Ok(quote!(
Ok(quote!(
impl Errno for #enum_name {
fn errno(&self) -> std::num::NonZeroI32 {
match *self {}
Expand Down Expand Up @@ -105,14 +105,13 @@ fn process_variant(variant: &Variant, enum_name: &Ident) -> syn::Result<TokenStr
return match pos {
Some(pos) => {
let variant_name = &variant.ident;

// The field at index `pos` is the one we are interested in

let inner = (0..fields.len())
.map(|i| if i == pos { quote!(e) } else { quote!(_) })
.fold(quote!(), |acc, c| quote!(#acc #c));
// We have to use this. otherwise the macro would expand to something like
// `{ 0usize: e, .. }` which is accepted, but only temporarily
let index = Index::from(pos);

Ok(quote!(#enum_name::#variant_name ( #inner ) => e.errno(),))
Ok(quote!(#enum_name::#variant_name { #index: e, .. } => e.errno(),))
}
None => Err(syn::Error::new_spanned(
variant,
Expand Down

0 comments on commit d8c94a6

Please sign in to comment.