copstr: COPy STRing module using const generic for capacity
copstr::Str wraps a fixed-size array of u8 and provides a
string-like interface on top. The size is specified using a const
generic argument.
The internal u8 array corresponds to UTF-8 encoded chars. All
functions guarantee that the contents are valid UTF-8 and return
an error if they are not. Truncation only happens at UTF-8
boundaries.
copstr is very useful when we want to add a string-like field
to a struct that implements Copy but we don't want to give up
this trait.
Example usage
use copstr;
use TryFrom;
// Create an owned fixed-size string with size 6 *on the stack*:
let mut string = try_from?;
// Use it as a regular string:
println!;
// Replace the contents with another string that fits the size 6:
string.replace?;
// Append a letter:
string.push?;
// Instead of returning a potential error, we can instead use
// truncating methods:
string.replace_trunc;
assert_eq!;
// `copstr::Str` implements Deref<Target=str>, so all `str`
// methods are available:
let split = format!;
assert_eq!;
// We can add a `copstr` to a struct without having to give up the
// `Copy` trait:
// We can (and should) create a type alias:
type MyStr = ;
// We can create `copstr` in const contexts:
const TEST: MyStr = new_const;
When using a const context, strings that don't fit generate a compilation error. For instance, the following doesn't compile:
const TEST_BAD: Str = new_const;