moos
Memory-Optimized Objects and Strings (no_std)
Overview
moos — pronounced "moose" — is a small collection of memory-optimized
string types for Rust, implementing small string optimization (SSO) techniques
and copy-on-write (COW) semantics to minimize heap allocations and improve the
performance of string operations.
Designed for use in no_std environments, moos prioritizes performance,
memory efficiency, and interoperability with common Rust string types. It is
ideal for applications where memory usage is a concern, such as embedded systems
or real-time applications.
Usage
[]
= "0.1"
CowStr<'a>
Memory-optimized alternative to Cow<'a, str>. It supports a special
CowStr::Inlined variant — in addition to
CowStr::Owned and CowStr::Borrowed, like
its std::borrow counterpart — which allows
small strings to be stored inline on the stack, reducing
heap allocations and improving performance for small string operations common in
many applications.
Variants
CowStr::Owned
Represents an owned string that is always heap-allocated.
use CowStr;
let owned_str = Owned;
assert!;
CowStr::Borrowed
Represents a borrowed string slice (&str) that is stored on the stack.
use CowStr;
let borrowed_str = Borrowed;
assert!;
CowStr::Inlined
Represents a small string that is stored inline on the stack. This variant is
used for strings that are shorter than or equal to MAX_INLINE_STR_LEN.
use CowStr;
let inlined_str = Inlined;
assert!;
Example
use CowStr;
// `CowStr::Owned` variant - always heap-allocated
let owned_str = from;
// `CowStr::Inlined` variant - stored on the stack
let small_str = from; // Stored inline on x64
// `CowStr::Borrowed` variant - stored on the stack
let large_str = from;
assert!;
assert!;
assert!;
InlineStr
The InlineStr type is a fixed-size string type that can store small strings
directly on the stack, up to a maximum length defined by MAX_INLINE_STR_LEN.
This allows for efficient storage and manipulation of small strings without heap allocations, making it ideal for performance-critical applications where memory usage is a concern, such as embedded systems or real-time applications.
- Supports UTF-8 encoded strings.
- Provides conversion methods to and from standard string types.
- Implements common traits like
Deref,AsRef<str>,Display,Debug - Supports comparison and ordering operations.
- Supports serialization/deserialization with serde
Note: Requires the
serdefeature flag to be enabled.
use InlineStr;
// Create an InlineStr from a string slice
let inline_str = try_from.unwrap;
// Implements the Display trait for easy printing
println!;
// Can be compared with regular strings and slices
assert_eq!;
// Supports mutation of the underlying byte buffer
let mut mutable_inline_str = inline_str;
mutable_inline_str.as_bytes_mut.copy_from_slice;
println!;
Attempting to create an InlineStr from a string that is too long:
let long_string = "This string is longer than the max length for InlineStr.";
match try_from
MAX_INLINE_STR_LEN
The constant MAX_INLINE_STR_LEN defines the maximum length of an inline string
in bytes, determined by the target architecture's pointer width. On 64-bit
systems, this is typically 22 B, while on 32-bit systems, it's usually 10 B.
This value is calculated as 3 times the size of an
isize(to account for UTF-8 encoding), minus 2 bytes to reserve space for au8length byte and a null terminator (\0) character (not stored but conceptually present in a manner similar to C-style strings).
StringTooLongError
The StringTooLongError is an error type returned when attempting to create an
InlineStr from a string or string slice (&str) that exceeds the maximum
allowed length defined by MAX_INLINE_STR_LEN.
use ;
// Attempt to create an InlineStr from a string that is too long
let long_string = "This string is longer than the max length for InlineStr.";
match try_from
MIT © Nicholas Berlette. All rights reserved.
moos · github · issues · docs · contributing