const_sizes 0.1.54

提供友好可读的常量大小
Documentation
# const_sizes

[![crates.io](https://img.shields.io/crates/v/const_sizes.svg)](https://crates.io/crates/const_sizes)
[![docs.rs](https://docs.rs/const_sizes/badge.svg)](https://docs.rs/const_sizes)

友好可读的 2 的幂次常量大小(power-of-two size constants),对标 Linux 内核 `<linux/sizes.h>` 的 `SZ_*` 宏。

`#![no_std]`、零依赖、纯 `pub const usize`,可在裸机 / 嵌入式 / 内核场景直接使用。

## 为什么需要它

写裸机或内核代码时,到处充斥 `0x1000`、`0x10_0000`、`16 * 1024` 这类魔数:

```rust
const KERNEL_HEAP: usize = 16 * 1024 * 1024;     // 多少?数 0
const PAGE_SIZE:   usize = 0x1000;                // 4K?还是别的?
```

换成 `const_sizes` 之后语义自解释、计算在编译期完成、不会写错位数:

```rust
use const_sizes::{SZ_4K, SZ_16M};

const KERNEL_HEAP: usize = SZ_16M;
const PAGE_SIZE:   usize = SZ_4K;
```

## 用法

把依赖加到 `Cargo.toml`:

```toml
[dependencies]
const_sizes = "0.1"
```

直接按需 `use`:

```rust
use const_sizes::{SZ_64, SZ_4K, SZ_2M, SZ_1G};

// 缓存行
#[repr(align(64))]
struct CacheLine([u8; SZ_64]);

// 页大小
const PAGE_SIZE: usize = SZ_4K;

// 大页
const HUGE_PAGE: usize = SZ_2M;

// 用户空间上限
const USER_SPACE_END: usize = SZ_1G;
```

## 常量列表

单位均为 power-of-two binary(1K = 1024,1M = 1 048 576,1G = 1 073 741 824)。

| 区间    | 常量                                                          |
| ------- | ------------------------------------------------------------- |
| 字节    | `SZ_1` `SZ_2` `SZ_4` `SZ_8` `SZ_16` `SZ_32` `SZ_64` `SZ_128` `SZ_256` `SZ_512` |
| KiB     | `SZ_1K` `SZ_2K` `SZ_4K` `SZ_8K` `SZ_16K` `SZ_32K` `SZ_64K` `SZ_128K` `SZ_256K` `SZ_512K` |
| MiB     | `SZ_1M` `SZ_2M` `SZ_4M` `SZ_8M` `SZ_16M` `SZ_32M` `SZ_64M` `SZ_128M` `SZ_256M` `SZ_512M` |
| GiB     | `SZ_1G` `SZ_2G` `SZ_4G`                                       |

注:

- 全部为 `pub const usize`,可用于数组长度、`const` 表达式、`#[repr(align(...))]` 等任意 const 上下文。
- `SZ_4G` 在 32 位 target 上不可用(`usize` 装不下),仅 64 位平台有效。

## MSRV

跟随 workspace 声明的 `rust-version`,当前为 1.95。

## License

Apache-2.0,见仓库根目录 `LICENSE`。