nc/syscalls/
types.rs

1// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
2// Use of this source is governed by Apache-2.0 License that can be found
3// in the LICENSE file.
4
5/// Error No.
6pub type Errno = i32;
7
8/// Syscall No.
9pub type Sysno = usize;
10
11pub const MAX_ERRNO: Errno = 4095;
12
13/// Check return value is error or not.
14///
15/// Returning from the syscall, a value in the range between -4095 and -1 indicates an error,
16/// it is -errno.
17///
18/// # Errors
19///
20/// Returns errno if system call fails.
21#[inline]
22pub const fn check_errno(ret: usize) -> Result<usize, Errno> {
23    #[allow(clippy::cast_possible_wrap)]
24    let reti = ret as isize;
25    if reti < 0 && reti >= (-MAX_ERRNO) as isize {
26        #[allow(clippy::cast_possible_truncation)]
27        let reti = (-reti) as Errno;
28        Err(reti)
29    } else {
30        Ok(ret)
31    }
32}