#string #symbol #atom #intern #token

无 std simble

简单符号:长度最多为 8 字节的非分配字符串

4 个版本

使用旧的 Rust 2015

0.1.3 2019 年 10 月 29 日
0.1.2 2018 年 11 月 2 日
0.1.1 2018 年 9 月 6 日
0.1.0 2018 年 7 月 2 日

#218无标准库

MIT/Apache

21KB
389

Simble

简单、小巧的符号。人类可读的长度最多为 8 字节的字符串,可以内联存储并高效操作。

我为什么需要这个?

如果

  • 你正在处理长度不超过 8 字节的人类可读字符串
  • 字符串集在编译时不是固定的,因此你不能使用 enum

那么 Simble 提供了

  • 高效存储
  • 快速比较和哈希

可选功能

  • serdeLexicalPrintable 都像受限制的 String 一样序列化和反序列化

当前 nightly 功能

使用 nightly 功能启用

  • const fns,因此你可以定义在编译时解析的符号

lib.rs:

简单、小巧的符号。人类可读的长度最多为 8 字节的字符串,可以内联存储并高效操作。

use simble::*;

let foo: Symbol = "foo".parse()?;
let foos: Symbol = "foosball".parse()?;
// symbol/symbol comparisons are efficient (1 instruction on any 64-bit platform; no pointer-following)
assert!(foo != foos);
// comparison is lexical: shorter comes first
assert!(foo < foos);
// Symbols can be formatted or turned into Strings
assert_eq!(format!("{}", foo), "foo");
assert_eq!(foo.to_string(), "foo");

let foo2 = foo.to_printable();
assert_eq!(&foo2, "foo");
let bar: Printable = "bar".parse()?;
// one of these is true, but which is unspecified
assert!(foo2 < bar || foo2 > bar);

let toobig: Result<Symbol, _> = "ninechars".parse();
assert!(toobig.is_err());

// compile-time parsing on nightly:
#[cfg(feature = "nightly")]
const consts: Printable = printable("consts");
assert_eq!(&consts, "consts");
const hackery: Symbol = symbol("hackery");
assert_eq!(&hackery.to_printable(), "hackery");

值限制

  • NUL 字符("\0".parse())不允许*
  • 空符号("".parse())不允许*

*这些限制被视为不稳定,可能只需通过补丁版本升级即可解除。

存储大小

Lexical 和 Printable 都与 u64 具有相同的内存布局。

此外,具有 features="nightly" 功能的 Lexical 实现为非零类型,因此 Option<Lexical> 也可以是 8 字节。

Printable 是否为非零类型是不指定的。

依赖关系

~235KB