Crate ni

Crate ni 

Source
Expand description

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(())
}

Macros§

name
Creates a name from the string. Panics when name is invalid.

Structs§

DecodedName
Return type from the decode method used to obtain a slice or string.
Name
Small limited alloc-free named identifier.

Enums§

Error
The name creation error.