Skip to content

Conversation

BrainStackOverFlow
Copy link

Added a new attribute: #[num_enum(from_primitive(no_panic))], with alternative syntax of #[num_enum(from_primitive(no_panic=true))].
this configuration is off by default.

this configuration generates a #[no_panic::no_panic] attribute on the (from this crate https://docs.rs/no-panic/latest/no_panic/) from_primitive() methods generated by #[derive(FromPrimitive)]

I made A POC that's works on my machine with manual tests.

If you are willing to accept such feature, I will add the necessary tests & docs.
I am open to make changes according to feedback.

example code from my project:

#[derive(Debug, IntoPrimitive, FromPrimitive)]
#[num_enum(from_primitive(no_panic))]
#[repr(isize)]
#[non_exhaustive]
pub enum Errno {
    EPERM = EPERM,
    ENOENT = ENOENT,
    // many more variants ...
    EHWPOISON = EHWPOISON,
    #[num_enum(catch_all)]
    Unknown(isize),
}

#[no_panic::no_panic]
pub unsafe fn syscall6w(
    no: isize,
    arg0: isize,
    arg1: isize,
    arg2: isize,
    arg3: isize,
    arg4: isize,
    arg5: isize,
) -> Result<isize, SyscallError> {
    let result = syscall6(no, arg0, arg1, arg2, arg3, arg4, arg5);

    if result as usize > -4096isize as usize {
        Err(SyscallError(no, Errno::from_primitive(-result)))
    } else {
        Ok(result)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant