1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! An encoding of type-level strings, with the [`TStr`] type and related macros.
//!
//! This crate features all these on stable:
//! - a relatively readable default representation of type-level strings
//! based on `char` const parameters.
//! - items for converting type-level strings to `&'static str` and `&'static [u8]`
//! - functions for comparing type-level strings to each other and `&str`
//! - macros for asserting the (in)equality of type-level strings to each other and `&str`
//!
//! All of the above functionality can be used in const contexts.
//!
//! # Examples
//!
//! ### Indexing
//!
//! This example demonstrates how you can use type-level strings,
//! and the [`Index`] trait, to access fields of generic types by name.
//!
//! ```rust
//! 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
//! }
//! }
//! }
//!
//! ```
//!
//! ### Type errors
//!
//! This example showcases what TStr looks like in simple type errors.
//!
//! ```rust,compile_fail
//! let _: tstr::TS!("Hello, world!") = ();
//! ```
//!
//! With no crate features enabled, the error message is this:
//! ```text
//! 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:
//! ```text
//! 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 `()`
//! ```
//!
//!
//!
//! # Macro expansion
//!
//! 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.
//!
//! # Cargo features
//!
//! - `"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.
//!
//! - `"serde"`(disabled by default):
//! Enables serde dependency and implements `serde::{Serialize, Deserialize}` for `TStr`
//!
//! - `"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.
//!
//! # No-std support
//!
//! This crate is unconditionally `#![no_std]`, and can be used anywhere that Rust can be.
//!
//! # Minimum Supported Rust Version
//!
//! This crate supports Rust versions back to Rust 1.88.0.
//!
//! [`TStr`]: crate::TStr
//! [`const_panic`]: const_panic
//! [`Index`]: core::ops::Index
//! [`tstr::utils`]: crate::utils
//////////
// lints
//////////
pub use __TStrRepr;
pub use __TStrRepr;
extern crate self as tstr;
pub use __ts_impl;
use crate__TStrArgBinary;
pub use crate::;
pub use typewit;
pub use ;
include!