g-string
g-string — a stack-allocated, Copy, and generically configurable UTF-8 string type featuring:
- Stack allocation with no heap allocation in core operations.
Copysemantics for lightweight fixed-capacity strings.- Compile-time configurable invariants:
- minimum length
- maximum length
- ASCII-only enforcement
- custom validation logic
- Full
strinteroperability throughDeref<Target = str>. - Mutation APIs that preserve all invariants.
- Conversions to and from standard string-related types.
serdesupport for serialization and deserialization.GSecretfor secret storage with zeroization and redacted formatting.no_stdcompatibility with optionalallocandstdfeatures.
Overview
g-string provides stack-allocated, bounded UTF-8 string types designed for predictable memory usage, validation, and zero-allocation operation.
Unlike String, which always allocates on the heap, g-string stores its contents inline using a fixed-capacity buffer determined at compile time. This makes it suitable for embedded systems, constrained environments, protocol types, validated domain strings, and performance-sensitive applications where heap allocation is undesirable.
The crate centers around two primary types:
GString— a validated bounded string type.GSecret— a secret string type with zeroization and redacted formatting.
GString
GString is generically configurable through const generics and validation traits:
- minimum length
- maximum length
- ASCII-only restriction
- custom validation logic
All construction and mutation APIs preserve these invariants automatically.
g-string maintains full interoperability with Rust’s string ecosystem through Deref<Target = str>, conversion traits, iterator support, formatting traits, and optional serde integration.
Core goals of the GString include:
- deterministic memory usage
- zero heap allocation in core operations
- UTF-8 correctness
- ergonomic
strinteroperability - compile-time configurability
- invariant-preserving mutation APIs
no_stdcompatibility- validation embedded into type
GSecret
GSecret is wrapper around GString providing type for secret information such as password, API keys, tokens, private keys, etc. Unlike GString, GSecret is not Copy for security reason,
to prevent it from having multiple copies in many places. It's an owned type instead, getting moved and having scope before getting dropped. Normally, when a string or buffer is dropped, the memory is simply marked as reusable. The bytes are usually not overwritten immediately. That means the old secret may still physically exist in RAM until something else reuses that memory region. It's even worse in garbage-collected languages,
where secrets might linger longer before getting collected. This becomes a problem in scenarios such as:
- memory dumps
- crash reports
- swap/pagefile leakage
- use-after-free bugs
- forensic analysis
- accidental memory exposure
- debugging tools
- cold boot attacks
To avoid this lingering secrets, we need zeroization. GSecret is equipped with this mechanism to zeroizes the secrets automatically on drop(or manually). So, even the memory is still there, the data already zeroized.
This zeroization also avoid compiler optimization like dead-code elimination and optimization removal. GSecret has no Display implementation and debug is redacted. To work with the secret, method reveal is provided
making sure secret reference stays within closure scope. This type prevents secrets from leaking easily.
Example
use fmt;
use ;
;
;
;
type Username = ;
type Password = ;
Features
default, std, alloc
Default feature enable std, which also enable alloc. If you want no_std, disable default feature.
secret
Enable GSecret and all of its related attributes and methods, including secret serde.
GSecret has no serialization implementation, for security purpose. You can implement it if you need.
serde
Enable serde for both GString and GSecret(if secret also enabled).
grapheme
Enable counting number of grapheme clusters for GString.