kernel_api/syscall/
result.rs1use core::fmt;
2use crate::allocator::AllocError;
3use crate::ptr::PointerError;
4
5pub type Result<T> = core::result::Result<T, Error>;
7
8macro_rules! define_error {
9 ($(#[$attr:meta])* pub enum Error {
10 $($(#[$item_attr:meta])* $name:ident = $val:literal),*
11 $(,)?
12 }) => {
13 $(#[$attr])* pub enum Error {
14 _Reserved = 0,
15 $($(#[$item_attr])* $name = $val),* ,
16 _MaxVal,
17 }
18
19 const _: () = { assert!((Error::_MaxVal as u16) < 4096, "cannot have more than 4096 error codes"); };
20
21 impl From<u32> for Error {
22 fn from(value: u32) -> Error {
23 match value {
24 $($val => Error::$name),*,
25 _ => Self::Invalid,
26 }
27 }
28 }
29 };
30}
31
32define_error! {
33 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
35 #[repr(u16)]
36 #[non_exhaustive]
37 pub enum Error {
38 InvalidUtf8 = 1,
40 UnsupportedProtocol = 2,
42 UnknownProtocol = 3,
44 InvalidEndpoint = 4,
46 NameInUse = 5,
48 InvalidHandle = 6,
50 Overflow = 7,
52 InvalidPointer = 8,
54 DeadServer = 9,
56 InvalidReturn = 10,
58 Invalid = 11,
60 AllocationFailure = 12,
62 InvalidArg = 13,
64 AsyncUnsupported = 14,
66 FutureCompat = 15,
68 EndOfData = 16,
70 ProtocolOverlap = 17,
72 ConditionNotMet = 18,
74 }
75}
76
77impl From<PointerError> for Error {
78 fn from(_: PointerError) -> Self {
79 Self::InvalidPointer
80 }
81}
82
83impl From<AllocError> for Error {
84 fn from(_: AllocError) -> Self {
85 Self::AllocationFailure
86 }
87}
88
89impl fmt::Display for Error {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 write!(f, "syscall failed")
92 }
93}
94
95impl core::error::Error for Error {}