| Crates.io | tstr |
| lib.rs | tstr |
| version | 0.3.0 |
| created_at | 2021-01-13 04:26:55.012289+00 |
| updated_at | 2025-08-26 20:02:52.94382+00 |
| description | type-level strings on stable |
| homepage | |
| repository | https://github.com/rodrimati1992/tstr_crates/ |
| max_upload_size | |
| id | 341209 |
| size | 100,619 |
An encoding of type-level strings, with the TStr type and related macros.
This crate features all these on stable:
char const parameters.&'static str and &'static [u8]&str&strAll of the above functionality can be used in const contexts.
This example demonstrates how you can use type-level strings,
and the Index trait, to access fields of generic types by name.
use std::ops::Index;
use tstr::{TS, ts};
fn main(){
takes_person(&Person::new("Bob".into(), "Marley".into()));
takes_person(&OtherPerson::new("Bob", "Marley"));
}
fn takes_person<P>(pers: &P)
where
P: Index<TS!(name), Output = str> + Index<TS!(surname), Output = str>
{
assert_eq!(&pers[ts!(name)], "Bob");
assert_eq!(&pers[ts!(surname)], "Marley");
}
use person::Person;
mod person {
use std::ops::Index;
use tstr::TS;
pub struct Person {
name: String,
surname: String,
}
impl Person {
pub fn new(name: String, surname: String) -> Self {
Self{name, surname}
}
}
impl Index<TS!(name)> for Person {
type Output = str;
fn index(&self, _: TS!(name)) -> &str {
&self.name
}
}
impl Index<TS!(surname)> for Person {
type Output = str;
fn index(&self, _: TS!(surname)) -> &str {
&self.surname
}
}
}
use other_person::OtherPerson;
mod other_person {
use std::ops::Index;
use tstr::TS;
pub struct OtherPerson {
name: &'static str,
surname: &'static str,
}
impl OtherPerson {
pub fn new(name: &'static str, surname: &'static str) -> Self {
Self{name, surname}
}
}
impl Index<TS!(name)> for OtherPerson {
type Output = str;
fn index(&self, _: TS!(name)) -> &str {
self.name
}
}
impl Index<TS!(surname)> for OtherPerson {
type Output = str;
fn index(&self, _: TS!(surname)) -> &str {
self.surname
}
}
}
This example showcases what TStr looks like in simple type errors.
let _: tstr::TS!("Hello, world!") = ();
With no crate features enabled, the error message is this:
error[E0308]: mismatched types
--> tstr/src/lib.rs:114:37
|
5 | let _: tstr::TS!("Hello, world!") = ();
| -------------------------- ^^ expected `TStr<___<..., 13>>`, found `()`
| |
| expected due to this
|
= note: expected struct `tstr::TStr<___<(tstr::__<'H', 'e', 'l', 'l', 'o', ',', ' ', 'w'>, tstr::__<'o', 'r', 'l', 'd', '!'>, (), (), (), (), (), ()), 13>>`
found unit type `()`
As you can see, the string is represented as a collection of char const parameters.
When the "nightly_str_generics" feature is enabled (which requires the nightly compiler),
the error message is this:
error[E0308]: mismatched types
--> tstr/src/lib.rs:114:37
|
5 | let _: tstr::TS!("Hello, world!") = ();
| -------------------------- ^^ expected `TStr<___<"Hello, world!">>`, found `()`
| |
| expected due to this
|
= note: expected struct `tstr::TStr<___<"Hello, world!">>`
found unit type `()`
This library reserves the right to change how it represent type-level strings internally in every single release, and cargo feature combination.
This only affects you if you expand the code generated by macros from this crate, and then use that expanded code instead of going through the macros.
"const_panic"(enabled by default):
Enables const_panic reexports, assertion macros,
and const_panic::fmt::PanicFmt impl for TStr.
"use_syn"(disabled by default):
Changes how literals passed to the macros of this crate are parsed to use the syn crate.
Use this if there is some literal that could not be
parsed but is a valid str/integer literal.
"str_generics"(disabled by default):
Changes the representation of type-level strings to use a &'static str const parameter,
making for better compiler errors.
As of 2025-08-18, this feature can't be enabled, because it
requires &'static str to be stably usable as const parameters.
Consider using "nightly_str_generics" if this feature can't be used.
"nightly_str_generics"(disabled by default): Equivalent to the "str_generics" feature,
and enables the nightly compiler features to use &'static str const parameters.
This crate is unconditionally #![no_std], and can be used anywhere that Rust can be.
This crate supports Rust versions back to Rust 1.88.0.