FlatString
A FlatString is a string that is stored in a fixed-size / flat array of characters. This is useful for scenarios where you want to avoid dynamic memory allocation, or where you want to store a string in a fixed-size buffer. A FlatString is a simple struct that contains:
- a fixed-size array of type u8
- the length of the string
- the number of characters.
The maximum store capacity of the FlatString is limited to 255 bytes (so a string has to be less than 255 characters long). As such, the length of the string (in bytes) and the number of characters are stored as a u8
as well.
For example, a FlatString<14>
will have a size of 16 bytes (14 bytes for the string, 1 byte for the length, and 1 byte for the number of characters).
Usage
To use a FlatString, you need to add the following to your Cargo.toml
:
[]
= "1.0.0"
Then, you can use the FlatString
as follows:
use FlatString;
Methods
The following methods are available for the FlatString
:
Method | Description |
---|---|
from_str |
Create a FlatString from a string slice |
new |
Create an empty FlatString |
len |
Get the length of the string |
is_empty |
Check if the string is empty |
chars_count |
Get the number of characters in the string |
capacity |
Get the capacity of the string |
as_str |
Get the string as a &str |
clear |
Clear the string |
push_str |
Ads a string slice to the existig string. If the resulted string size is bigger than the string capacity, the string will be truncated to fit in the allocated capacty. |
push |
Adds a character to the existing string. If the resulted string size is bigger than the string capacity, the string will be truncated to fit in the allocated capacty. |
try_push_str |
Ads a string slice to the existig string only if the resulted string fits in the preallocated capacity. In this case this method will return Some(&str) otherwise it returns None |
try_push |
Adds a character to the existing string only if the resulted string fits in the preallocated capacity. In this case this method will return Some(&str) otherwise it returns None |
set |
Set the string to a new value. If the new string size is bigger than the string capacity, the string will be truncated to fit in the allocated capacty. |
FlatString
implements the following traits:
std::fmt::Display
andstd::fmt::Debug
(this allows you to print the string usingprintln!
anddbg!
)std::ops::Deref
(this allows you to use the*
operator to get the string as a&str
). This will also allow you to use theFlatString
as a&str
in function arguments.Copy
andClone
(this allows you to copy theFlatString
using theCopy
trait)PartialEq
andEq
(this allows you to compare twoFlatString
using the==
operator)PartialOrd
andOrd
(this allows you to compare twoFlatString
using the<
,>
,<=
, and>=
operators)Default
(this allows you to create an emptyFlatString
)
Example
-
Create a
FlatString
from a string slice:let s = from_str; // s will be "Hello World !"
-
Create a
FlatString
from a string slice that is larger than its capacity:let s = from_str; // s will be "Hello Worl" (truncated to fit 10 bytes)
-
Add a
&str
to an existingFlatString
:let mut s = from_str; // s is "Hello" s.push_str; // s is "Hello World !"
-
Add a
&str
to an existingFlatString
and exceed the capacity:let mut s = from_str; // s is "Hello" s.push_str; // s is "Hello Wo" (truncated to fit 8 bytes)
-
Add a
&str
to an existingFlatString
usingtry_push_str
:let mut s = new; if let Some = s.try_push_str else let result = s.try_push_str
-
Validating the size of a
FlatString
let mut s = new; assert_eq!; // the size of the FlatString is 10 bytes: // - 8 bytes for the string // - 1 byte for the length // - 1 byte for the number of characters