Crates.io | common-strings |
lib.rs | common-strings |
version | 0.1.2 |
source | src |
created_at | 2022-07-20 00:23:13.545909 |
updated_at | 2022-07-20 09:37:15.211251 |
description | Macro for storing common strings as enum variants |
homepage | |
repository | https://github.com/merlinfuchs/common-strings |
max_upload_size | |
id | 628590 |
size | 8,340 |
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.
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
}