#byte-stream #identifier #generate #string #safe #valid #name-mangling

mangling

mangling generates safe, recognizable identifiers from byte streams

7 releases

0.2.4 Aug 31, 2024
0.2.3 Nov 28, 2020
0.2.2 Apr 28, 2020
0.1.1 Apr 10, 2020

#587 in Encoding

41 downloads per month

MIT license

30KB
361 lines

mangling

crates.io Rust codecov Docs.rs

Purpose

This library provides for reversibly generating identifiers (hereinafter "mangled names") that are valid under C-language rules, from arbitrary byte streams, while maintaining a measure of human readability.

Examples

See lib.rs for examples of strings with their manglings.

Rationale

The functionality of mangling was hoisted out of a compiler that needed to translate identifiers for Java symbols (e.g. method names and signatures) into symbols valid for the generated assembly language. Although the mangling process can encode any stream of bytes into a (longer) string of ASCII characters, and then convert the output back again, it is not intended to provide general encoding/decoding facilities. Instead, it is meant to provide an easy, reliable way for compiler writers to generate human-recognizable identifiers without having to invent their own mangling scheme.

Features

Schemes

At present, only one mangling scheme is supported, with a fixed format defined in the library documentation. The mangling scheme is simple, and may not be original, but is not known to be shared with any other system, so no interoperability guarantees are provided. Future revisions may change the API to allow some level of configurability or replacement of the encoding scheme.

Requirements

  1. Totality (every byte stream can be encoded into a unique mangled name)
  2. Injectivity (each mangled name can be decoded to a unique byte stream)

Minimum Supported Rust Version

The minimum supported Rust version is documented in Cargo.toml via the rust-version key. The MSRV for mangling may change at any time, but usually for good reasons.

Goals

  1. Correctness (the implementation matches its documented behavior)
  2. Consistency (the implementation behaves in a predictable manner)
  3. Readability (mangled names are visibly related to input streams)
  4. Compactness (mangled names are comparable in length to inputs)
  5. Performance (processing is computationally efficient in time and space)

Feedback

Any demonstrated failure of mangling to meet its stated requirements should be reported through Github, and pull requests are welcomed.

No runtime deps