# stdlib **Repository Path**: iot-ua/stdlib ## Basic Information - **Project Name**: stdlib - **Description**: 对底层操作系统功能的封装,同时兼容 RUST STD 。为上层应用提供与系统,STD 无关的开发环境。 - **Primary Language**: Rust - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-24 - **Last Updated**: 2023-02-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # stdlib #### 1. 介绍 对底层操作系统功能的封装,同时兼容 `RUST STD` 。为上层应用提供与系统,`STD` 无关的开发环境。 #### 2. 编译指南 ##### 2.1. 编译环境准备 1. 安装 `Rust`。通过 `rustup` 下载 `Rust`,这是一个管理 `Rust` 版本和相关工具的命令行工具。 2. 安装 `Rust` 编译器 。本项目采用的编译器是 `rustc 1.61.0-nightly` 。这里需要强调的是 `nightly` 版本的是官方用来实验新功能的 **不稳定版本** 。此项目用到了很多 `Rust` 的 **不稳定特性**,因此只能选择 `nightly`。 3. 安装 `Cargo`,这是 `Rust` 的构建系统和包管理器,大多数 `Rustacean` 们使用 `Cargo` 来管理他们的 `Rust` 项目,它可以帮你处理很多任务,比如构建代码,下载依赖库并编译这些库。 4. 使用 `Cargo` 编译本项目。`cargo build` 编译一个包,`cargo check` 检查本地包及其所有依赖项是否有错误,`cargo clean` 删除 `Cargo` 过去生成的文件。`cargo build --release` 表示构建带有优化的包。 ##### 2.2. 项目目录结构说明 - `stdlib` - `examples` - `src` - `no_std` `NoSTD`实现,应用于嵌入式设备。 - `collections` 对象池实现。使用池的思想解决频繁分配内存导致的内存碎片问题。 - `error` 错误定义 - `io` 错误封装,`Result` - `lazy` 懒加载 - `sync` - `Condvar` 条件变量 - `mutex` 互斥锁 - `once` 一种同步原语,可用于运行一次性全局初始化。 - `rwlock` 读写锁 - `spinlock` 自旋锁 - `thread` 线程 - `std` `STD`库的封裝。 - `io` 错误封装,`Result` - `sync` 与`NoSTD`的`sync`结构相同 - `thread` 线程 ##### 2.3. 条件编译 ###### 2.3.1. 通过配置 `Cargo.toml` 配置文件可以实现条件编译 ``` toml [features] # default 字段配置了默认启动哪些 feature。每一个 feature 都是一个列表,列表中的项表示将依赖于哪些 feature。 # default = ["alloc"] 表示默认依赖于 alloc 。而 alloc 又依赖于 arch 库,arch库是 NO-STD的。即默认为 NO-STD 编译。 # 若将 default = ["alloc"] 修改为 default = ["std"] 则表示依赖 rust STD。 # 注意事项:不能同时开启 alloc 和 std。编译时只能选择其中一个。即 default = ["alloc","std"] 是错误的,无法编译。 default = ["alloc"] alloc = ["arch"] std = ["parking"] ``` ###### 2.3.2. 通过`Cargo`命令实现条件编译 ```shell # 注意事项:不能同时开启 alloc 和 std。编译时只能选择其中一个。即 cargo build --no-default-features --features "stdlib/std stdlib/alloc" 是错误的,无法编译。 # STD 编译 # --no-default-features 表示不开启默认 features ,--features "stdlib/std" 会依赖用户列出的 features,也就是 stdlib/std。 cargo build --no-default-features --features "stdlib/std" # NO-STD # --no-default-features 表示不开启默认 features ,--features "stdlib/alloc" 会依赖用户列出的 features,也就是 stdlib/alloc。 cargo build --no-default-features --features "stdlib/alloc" ```