1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Common error types for kernel development.
use core::alloc::AllocError;

use alloc::collections::TryReserveError;

/// Kernel errors.
#[derive(Debug, Copy, Clone)]
pub enum Error {
    /// When global allocator returns zero.
    OutOfMemory(&'static str),
    /// When access is forbidden.
    Forbidden(&'static str),
    /// When something not found.
    NotFound(&'static str),
    /// Cannot or will not process the request due to something that is perceived
    /// to be a client error.
    BadRequest(&'static str),
    /// When operation timeout.
    Timeout(&'static str),
    /// When something already exists.
    Conflict(&'static str),
    /// Access to the target resource that is no longer available.
    /// For example, when accessing a removed sd card.
    Gone(&'static str),
    /// When hardware error.
    InternalError(&'static str),
    /// When disk used out.
    InsufficientStorage(&'static str),
    /// When feature not yet implemented.
    NotImplemented(&'static str),
    /// When page fault triggered at invalid virtual address.
    SegmentFault,
}

/// Sugar of error.
pub type Result<T> = core::result::Result<T, Error>;

impl From<AllocError> for Error {
    fn from(x: AllocError) -> Self {
        Error::OutOfMemory("alloc error")
    }
}

impl From<TryReserveError> for Error {
    fn from(x: TryReserveError) -> Self {
        Error::OutOfMemory("try reserve error")
    }
}

impl From<hashbrown::TryReserveError> for Error {
    fn from(x: hashbrown::TryReserveError) -> Self {
        Error::OutOfMemory("try reserve error")
    }
}