Skip to content

Commit ea56df0

Browse files
committed
Move rust error to macro declaration.
This makes declaring an error a bit easier, as the specific error only has to be written once. This also saves repetitive code. Signed-off-by: Finn Behrens <[email protected]>
1 parent 15b033a commit ea56df0

File tree

1 file changed

+52
-72
lines changed

1 file changed

+52
-72
lines changed

rust/kernel/error.rs

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ use core::fmt;
1212
use core::num::TryFromIntError;
1313
use core::str::{self, Utf8Error};
1414

15+
macro_rules! declare_err {
16+
($err:tt) => {
17+
pub const $err: Self = Error(-(bindings::$err as i32));
18+
};
19+
($err:tt, $($doc:expr),+) => {
20+
$(
21+
#[doc = $doc]
22+
)*
23+
pub const $err: Self = Error(-(bindings::$err as i32));
24+
};
25+
}
26+
1527
/// Generic integer kernel error.
1628
///
1729
/// The kernel defines a set of integer generic error codes based on C and
@@ -24,113 +36,81 @@ use core::str::{self, Utf8Error};
2436
pub struct Error(c_types::c_int);
2537

2638
impl Error {
27-
/// Operation not permitted.
28-
pub const EPERM: Self = Error(-(bindings::EPERM as i32));
39+
declare_err!(EPERM, "Operation not permitted.");
40+
41+
declare_err!(ENOENT, "No such file or directory.");
42+
43+
declare_err!(ESRCH, "No such process.");
2944

30-
/// No such file or directory.
31-
pub const ENOENT: Self = Error(-(bindings::ENOENT as i32));
45+
declare_err!(EINTR, "Interrupted system call.");
3246

33-
/// No such process.
34-
pub const ESRCH: Self = Error(-(bindings::ESRCH as i32));
47+
declare_err!(EIO, "I/O error.");
3548

36-
/// Interrupted system call.
37-
pub const EINTR: Self = Error(-(bindings::EINTR as i32));
49+
declare_err!(ENXIO, "No such device or address.");
3850

39-
/// I/O error.
40-
pub const EIO: Self = Error(-(bindings::EIO as i32));
51+
declare_err!(E2BIG, "Argument list too long.");
4152

42-
/// No such device or address.
43-
pub const ENXIO: Self = Error(-(bindings::ENXIO as i32));
53+
declare_err!(ENOEXEC, "Exec format error.");
4454

45-
/// Argument list too long.
46-
pub const E2BIG: Self = Error(-(bindings::E2BIG as i32));
55+
declare_err!(EBADF, "Bad file number.");
4756

48-
/// Exec format error.
49-
pub const ENOEXEC: Self = Error(-(bindings::ENOEXEC as i32));
57+
declare_err!(ECHILD, "Exec format error.");
5058

51-
/// Bad file number.
52-
pub const EBADF: Self = Error(-(bindings::EBADF as i32));
59+
declare_err!(EAGAIN, "Try again.");
5360

54-
/// No child processes.
55-
pub const ECHILD: Self = Error(-(bindings::ECHILD as i32));
61+
declare_err!(ENOMEM, "Out of memory.");
5662

57-
/// Try again.
58-
pub const EAGAIN: Self = Error(-(bindings::EAGAIN as i32));
63+
declare_err!(EACCES, "Permission denied.");
5964

60-
/// Out of memory.
61-
pub const ENOMEM: Self = Error(-(bindings::ENOMEM as i32));
65+
declare_err!(EFAULT, "Bad address.");
6266

63-
/// Permission denied.
64-
pub const EACCES: Self = Error(-(bindings::EACCES as i32));
67+
declare_err!(ENOTBLK, "Block device required.");
6568

66-
/// Bad address.
67-
pub const EFAULT: Self = Error(-(bindings::EFAULT as i32));
69+
declare_err!(EBUSY, "Device or resource busy.");
6870

69-
/// Block device required.
70-
pub const ENOTBLK: Self = Error(-(bindings::ENOTBLK as i32));
71+
declare_err!(EEXIST, "File exists.");
7172

72-
/// Device or resource busy.
73-
pub const EBUSY: Self = Error(-(bindings::EBUSY as i32));
73+
declare_err!(EXDEV, "Cross-device link.");
7474

75-
/// File exists.
76-
pub const EEXIST: Self = Error(-(bindings::EEXIST as i32));
75+
declare_err!(ENODEV, "No such device.");
7776

78-
/// Cross-device link.
79-
pub const EXDEV: Self = Error(-(bindings::EXDEV as i32));
77+
declare_err!(ENOTDIR, "Not a directory.");
8078

81-
/// No such device.
82-
pub const ENODEV: Self = Error(-(bindings::ENODEV as i32));
79+
declare_err!(EISDIR, "Is a directory.");
8380

84-
/// Not a directory.
85-
pub const ENOTDIR: Self = Error(-(bindings::ENOTDIR as i32));
81+
declare_err!(EINVAL, "Invalid argument.");
8682

87-
/// Is a directory.
88-
pub const EISDIR: Self = Error(-(bindings::EISDIR as i32));
83+
declare_err!(ENFILE, "File table overflow.");
8984

90-
/// Invalid argument.
91-
pub const EINVAL: Self = Error(-(bindings::EINVAL as i32));
85+
declare_err!(EMFILE, "Too many open files.");
9286

93-
/// File table overflow.
94-
pub const ENFILE: Self = Error(-(bindings::ENFILE as i32));
87+
declare_err!(ENOTTY, "Not a typewriter.");
9588

96-
/// Too many open files.
97-
pub const EMFILE: Self = Error(-(bindings::EMFILE as i32));
89+
declare_err!(ETXTBSY, "Text file busy.");
9890

99-
/// Not a typewriter.
100-
pub const ENOTTY: Self = Error(-(bindings::ENOTTY as i32));
91+
declare_err!(EFBIG, "File too large.");
10192

102-
/// Text file busy.
103-
pub const ETXTBSY: Self = Error(-(bindings::ETXTBSY as i32));
93+
declare_err!(ENOSPC, "No space left on device.");
10494

105-
/// File too large.
106-
pub const EFBIG: Self = Error(-(bindings::EFBIG as i32));
95+
declare_err!(ESPIPE, "Illegal seek.");
10796

108-
/// No space left on device.
109-
pub const ENOSPC: Self = Error(-(bindings::ENOSPC as i32));
97+
declare_err!(EROFS, "Read-only file system.");
11098

111-
/// Illegal seek.
112-
pub const ESPIPE: Self = Error(-(bindings::ESPIPE as i32));
99+
declare_err!(EMLINK, "Too many links.");
113100

114-
/// Read-only file system.
115-
pub const EROFS: Self = Error(-(bindings::EROFS as i32));
101+
declare_err!(EPIPE, "Broken pipe.");
116102

117-
/// Too many links.
118-
pub const EMLINK: Self = Error(-(bindings::EMLINK as i32));
103+
declare_err!(EDOM, "Math argument out of domain of func.");
119104

120-
/// Broken pipe.
121-
pub const EPIPE: Self = Error(-(bindings::EPIPE as i32));
105+
declare_err!(ERANGE, "Math result not representable.");
122106

123-
/// Math argument out of domain of func.
124-
pub const EDOM: Self = Error(-(bindings::EDOM as i32));
107+
declare_err!(EDEADLK, "Resource deadlock would occur");
125108

126-
/// Math result not representable.
127-
pub const ERANGE: Self = Error(-(bindings::ERANGE as i32));
109+
declare_err!(ENAMETOOLONG, "File name too long");
128110

129-
/// Cannot assign requested address.
130-
pub const EADDRNOTAVAIL: Self = Error(-(bindings::EADDRNOTAVAIL as i32));
111+
declare_err!(EADDRNOTAVAIL, "Cannot assign requested address.");
131112

132-
/// Restart the system call.
133-
pub const ERESTARTSYS: Self = Error(-(bindings::ERESTARTSYS as i32));
113+
declare_err!(ERESTARTSYS, "Restart the system call.");
134114

135115
/// Creates an [`Error`] from a kernel error code.
136116
///

0 commit comments

Comments
 (0)