Crates.io | static-or-heap-string |
lib.rs | static-or-heap-string |
version | 0.1.0 |
source | src |
created_at | 2024-07-17 04:14:46.147825 |
updated_at | 2024-07-17 04:14:46.147825 |
description | An enum type for handling both static and heap-allocated strings. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1305797 |
size | 14,001 |
static-or-heap-string
is a Rust crate that provides an enum type, StaticOrHeapString
, to handle strings that can either be static string slices (&'static str
) or heap-allocated String
s. This allows for flexible and efficient string handling in situations where either static or dynamic string data might be encountered.
PartialEq
, PartialOrd
, Ord
).Add this to your Cargo.toml
:
[dependencies]
static-or-heap-string = "0.1.0"
serde = { version = "1.0", features = ["derive"] }
Import and use the StaticOrHeapString enum in your code:
use static_or_heap_string::StaticOrHeapString;
use serde_json;
fn main() {
// Example usage
let static_str = StaticOrHeapString::Static("hello");
let heap_str = StaticOrHeapString::Heap(String::from("world"));
println!("{:?}", static_str);
println!("{:?}", heap_str);
// Serialize and deserialize
let serialized = serde_json::to_string(&static_str).unwrap();
let deserialized: StaticOrHeapString = serde_json::from_str(&serialized).unwrap();
assert_eq!(static_str, deserialized);
}
pub enum StaticOrHeapString {
Static(&'static str),
Heap(String),
}
Static(&'static str)
: Represents a static string slice.
Heap(String)
: Represents a heap-allocated string.
as_str
Returns the string slice representation of the enum variant.
impl StaticOrHeapString {
pub fn as_str(&self) -> &str {
match self {
StaticOrHeapString::Static(s) => s,
StaticOrHeapString::Heap(s) => s.as_str(),
}
}
}
PartialEq
, Eq
: Compare the string content, ignoring the variant.
PartialOrd
, Ord
: Compare the string content lexicographically.
Hash
: Hash the string content, ignoring the variant.
Debug
: Format the string content for debugging.
Clone
: Clone the enum, preserving the variant.
Serialize
, Deserialize
: Serialize and deserialize the string content using Serde.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.