common-strings

Crates.iocommon-strings
lib.rscommon-strings
version0.1.2
sourcesrc
created_at2022-07-20 00:23:13.545909
updated_at2022-07-20 09:37:15.211251
descriptionMacro for storing common strings as enum variants
homepage
repositoryhttps://github.com/merlinfuchs/common-strings
max_upload_size
id628590
size8,340
Merlin (merlinfuchs)

documentation

README

Macro for storing common strings as enum variants

This crate provides a single macro for storing common strings as enum variants. If you are storing a lot of strings where a few values often occur, this can reduce the memory usage.

You can enable the serde feature to add serialization and deserialization support.

Example

An example for this are Discord channel names. Names like general, offtopic, support, staff, ... make up a large percentage of all channel names. Storing a string for each channel called general wastes a lot of resources.

use common_strings::{common_strings, CommonStrings};

common_strings!(
    #[derive(Clone, Debug)]
    pub enum ChannelName {
        const General = "general";
        const Offtopic = "offtopic";
        const Support = "support";
        const Staff = "staff";
    }
);

fn main() {
    let channel_name = ChannelName::General;
    println!("{}", channel_name.as_ref()); // general

    let channel_name = ChannelName::Other(String::from("my-channel"));
    println!("{}", channel_name); // my-channel

    let channel_name = ChannelName::from_cow("offtopic".into());
    println!("{}", channel_name.into_string()); // offtopic
}
Commit count: 3

cargo fmt