third_party_rust_nix/README_zh.md

44 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

# Rust bindings to **nix** APIs
[![Cirrus Build Status](https://api.cirrus-ci.com/github/nix-rust/nix.svg)](https://cirrus-ci.com/github/nix-rust/nix)
[![crates.io](https://img.shields.io/crates/v/nix.svg)](https://crates.io/crates/nix)
[Documentation (Releases)](https://docs.rs/nix/)
## 引入背景
Nix试图为各种*nix平台的API提供友好的绑定Linux、Darwin、...)。对于许多系统APINix提供了一个安全的替代方案以取代[libc crate](https://github.com/rust-lang/libc)所暴露的不安全API。 具体做法是用类型/抽象来包装libc的功能强制执行合法、安全的使用。
Nix提供一个例子对比与libc系统调用的区别
[gethostname](https://man7.org/linux/man-pages/man2/gethostname.2.html)
```rust,ignore
// libc api (unsafe, requires handling return code/errno)
pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int;
// nix api (returns a nix::Result<OsString>)
pub fn gethostname() -> Result<OsString>;
```
## nix在OH中的使用
在"BUILD.gn"中使用deps字段添加对num-traits的crates的依赖例如
```BUILD.gn
ohos_rust_shared_library("foo") {
source = [ "src/lib.rs" ]
deps = [ "//third_party/rust/crates/nix:lib" ]
}
```
## Minimum Supported Rust Version (MSRV)
nix在Rust 1.56.1及更高版本上得到支持。
## License
Nix使用MIT License。 更多细节见[LICENSE](LICENSE)。
## 开发者贡献
在使用该工具的过程中有任何问题欢迎开发者在社区issue中反馈。
<br>