2个不稳定版本

0.2.0 2024年3月8日
0.1.1 2020年9月27日
0.1.0 2020年9月27日

#206 in 操作系统

Download history 856/week @ 2024-04-04 928/week @ 2024-04-11 850/week @ 2024-04-18 810/week @ 2024-04-25 984/week @ 2024-05-02 851/week @ 2024-05-09 1023/week @ 2024-05-16 977/week @ 2024-05-23 919/week @ 2024-05-30 674/week @ 2024-06-06 956/week @ 2024-06-13 815/week @ 2024-06-20 804/week @ 2024-06-27 700/week @ 2024-07-04 621/week @ 2024-07-11 850/week @ 2024-07-18

3,137 每月下载量
4 个crate(2个直接)中使用

MIT 许可证

24KB
236

Verify

system_error

跨平台系统错误库。

请查看文档以获取详细信息!


lib.rs:

跨平台系统错误库。

此库捕获了Unix平台上errno、Windows上的GetLastError()以及macOS和iOS上的kern_return_t的行为和消息。此外,这些Error值可以被转换为std::io::Error值。

示例

在Linux上

use system_error::Error;
use std::io::ErrorKind;

let os_error = Error::from_raw_os_error(1);
assert_eq!(os_error.kind(), ErrorKind::PermissionDenied);
assert_eq!(
    format!("{}", os_error),
    "Operation not permitted (os error 1)"
);
assert_eq!(
    format!("{:?}", os_error),
    "Error { kind: PermissionDenied, message: \"Operation not permitted (os error 1)\" }"
);

let kern_error = Error::from_raw_kernel_error(8);
assert_eq!(kern_error.kind(), ErrorKind::Other);
assert_eq!(
    format!("{}", kern_error),
    "Unknown error (kernel error 8)"
);
assert_eq!(
    format!("{:?}", kern_error),
    "Error { kind: Other, message: \"Unknown error (kernel error 8)\" }"
);

在macOS上

use system_error::Error;
use std::io::ErrorKind;

let os_error = Error::from_raw_os_error(1);
assert_eq!(os_error.kind(), ErrorKind::PermissionDenied);
assert_eq!(
    format!("{}", os_error),
    "Operation not permitted (os error 1)"
);
assert_eq!(
    format!("{:?}", os_error),
    "Error { kind: PermissionDenied, message: \"Operation not permitted (os error 1)\" }"
);

let kern_error = Error::from_raw_kernel_error(8);
assert_eq!(kern_error.kind(), ErrorKind::PermissionDenied);
assert_eq!(
    format!("{}", kern_error),
    "(os/kern) no access (kernel error 8)"
);
assert_eq!(
    format!("{:?}", kern_error),
    "Error { kind: PermissionDenied, message: \"(os/kern) no access (kernel error 8)\" }"
);

在Windows上

use system_error::Error;
use std::io::ErrorKind;

let os_error = Error::from_raw_os_error(5);
assert_eq!(os_error.kind(), ErrorKind::PermissionDenied);
assert_eq!(
    format!("{}", os_error),
    "Access is denied. (os error 5)"
);
assert_eq!(
    format!("{:?}", os_error),
    "Error { kind: PermissionDenied, message: \"Access is denied. (os error 5)\" }"
);

let kern_error = Error::from_raw_kernel_error(8);
assert_eq!(kern_error.kind(), ErrorKind::Other);
assert_eq!(
    format!("{}", kern_error),
    "Unknown error (kernel error 8)"
);
assert_eq!(
    format!("{:?}", kern_error),
    "Error { kind: Other, message: \"Unknown error (kernel error 8)\" }"
);

无运行时依赖