ni

Crates.ioni
lib.rsni
version0.2.0
created_at2020-07-20 08:36:36.573661+00
updated_at2025-09-16 01:48:38.510071+00
descriptionSmall limited alloc-free named identifier
homepage
repositoryhttps://github.com/nanoqsh/ni
max_upload_size
id267143
size23,393
Nano (nanoqsh)

documentation

https://docs.rs/ni

README

ni

Small limited alloc-free named identifier

Usage

This crate provides a type of small, limited, alloc-free string - Name, that can only store:

  • Latin lowercase letters 'a'..='z'
  • Digits '0'..='9'
  • Underscores '_'

The string length cannot exceed 24. This makes it possible to store identifiers very efficiently, such as: key12, hello_world, ni_ident_version2, etc.

The Name type has the same size as &str - two pointers (16 bytes on a 64-bit platform). Unlike &str, it does not carry a lifetime because it stores the data internally. It also implements Copy, meaning it can be duplicated freely without cost since its size is tiny.

This type doesn't implement many string operations. Instead, it's mainly used for efficient in-memory storage.

Example

Add the crate to your project. You can enable a feature to get serialization/deserialization support:

cargo add ni -F serde

Currently, the crate supports serde and bincode. This is optional if you only need to store many small strings in memory.

Now we can efficiently parse data with many identifiers, for example a schema with arbitrary fields like a HashMap:

use {
    ni::Name,
    std::{collections::HashMap, io},
};

fn main() -> io::Result<()> {
    let json = r#"
    {
        "some_data": 10,
        "key0": 1,
        "key1": 2,
        "key2": 3,
        "key3": 4
    }"#;

    let data: HashMap<Name, u32> = serde_json::from_str(&json)?;
    println!("{data:#?}");

    Ok(())
}
Commit count: 4

cargo fmt