static-or-heap-string

Crates.iostatic-or-heap-string
lib.rsstatic-or-heap-string
version0.1.0
sourcesrc
created_at2024-07-17 04:14:46.147825
updated_at2024-07-17 04:14:46.147825
descriptionAn enum type for handling both static and heap-allocated strings.
homepage
repositoryhttps://github.com/klebs6/klebs-general
max_upload_size
id1305797
size14,001
(klebs6)

documentation

README

static-or-heap-string

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 Strings. This allows for flexible and efficient string handling in situations where either static or dynamic string data might be encountered.

Features

  • Supports serialization and deserialization with Serde.
  • Implements comparison traits (PartialEq, PartialOrd, Ord).
  • Implements hashing, ignoring the variant type.
  • Provides a unified API for accessing the string data.

Usage

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);
}

Enum Variants

pub enum StaticOrHeapString {
    Static(&'static str),
    Heap(String),
}

Static(&'static str): Represents a static string slice. Heap(String): Represents a heap-allocated string.

Methods

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(),
        }
    }
}

Trait Implementations

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Commit count: 248

cargo fmt