#string #symbol #atom #intern #token

no-std simble

Simple symbols: allocation-less strings up to 8 bytes long

4 releases

Uses old Rust 2015

0.1.3 Oct 29, 2019
0.1.2 Nov 2, 2018
0.1.1 Sep 6, 2018
0.1.0 Jul 2, 2018

#218 in No standard library

MIT/Apache

21KB
389 lines

Simble

Simple, tiny symbols. Human-readable strings up to 8 bytes long that can be stored inline and manipulated efficiently.

Why would I want this?

If:

  • you are working with human-readable strings that never exceed 8 bytes
  • the set of strings is not fixed at compile time, so you can't use an enum

Then Simble offers:

  • efficient storage
  • fast comparisons and hashing

Optional features

  • serde: Lexical and Printable both serialize/deserialize like a restricted String

Current nightly features

Enable with the nightly feature:

  • const fns, so that you can define symbols that will be parsed at compile time

lib.rs:

Simple, tiny symbols. Human-readable strings up to 8 bytes long that can be stored inline and manipulated efficiently.

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");

Value restrictions

  • NUL characters ("\0".parse()) are not allowed*
  • The empty symbol ("".parse()) is not allowed*

*These restrictions are to be considered unstable and may be lifted with only a patch version increase.

Storage size

Both Lexical and Printable have the same memory layout as a [u64].

Further, with features="nightly" Lexical is implemented as a non-zero type so that Option<Lexical> can also be 8 bytes.

Whether Printable is a non-zero type is unspecified.

Dependencies

~235KB