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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
#[macro_use]
mod cmp_macros;
/// The type of a type-level string, always a [`TStr`].
///
/// # Arguments
///
/// You can use any of these as arguments:
///
/// - String literals (eg: `TS!("hello")`, `TS!(r#"world"#)`)
///
/// - Integers (eg: `TS!(0)`, `TS!(100)`):
/// converting the integer to decimal, then stringifying it.
///
/// - Single identifiers (eg: `TS!(foo)`, `TS!(bar)`): stringifying the identifier.
///
/// - A comma separated list of the other valid arguments to this macro
/// (eg: `TS!(foo, "bar", 0)`), this evaluates to a tuple of `TStr`s.
///
/// - `concat!(...)`-like syntax: concatenates its arguments,
/// accepting the same arguments as this macro.
///
/// - `stringify!(...)`-like syntax: stringifies its arguments.
///
/// # Examples
///
/// ### ToVariant
///
/// This example demonstrates how you can use type-level strings to create a
/// `GetVariant` trait which gets the data in a variant if the enum is that variant.
///
/// ```rust
/// use tstr::TS;
///
/// fn main(){
/// let foo = Enum::Foo(3, 5);
/// let bar = Enum::Bar("hello".to_string());
///
/// assert_eq!(foo.to_variant(Foo::NEW), Some((3, 5)));
/// assert_eq!(foo.to_variant(Bar::NEW), None);
///
/// assert_eq!(bar.to_variant(Foo::NEW), None);
/// assert_eq!(bar.to_variant(Bar::NEW), Some("hello".to_string()));
/// }
///
/// type Foo = TS!(Foo);
///
/// type Bar = TS!(Bar);
///
/// trait ToVariant<V> {
/// type Output;
///
/// fn to_variant(&self, variant: V) -> Option<Self::Output>;
/// }
///
/// enum Enum {
/// Foo(u32, u64),
/// Bar(String),
/// }
///
/// impl ToVariant<TS!(Foo)> for Enum {
/// type Output = (u32, u64);
///
/// fn to_variant(&self, variant: TS!(Foo)) -> Option<Self::Output> {
/// match self {
/// Self::Foo(l, r) => Some((*l, *r)),
/// _ => None,
/// }
/// }
/// }
///
/// impl ToVariant<TS!(Bar)> for Enum {
/// type Output = String;
///
/// fn to_variant(&self, variant: TS!(Bar)) -> Option<Self::Output> {
/// match self {
/// Self::Bar(s) => Some(s.clone()),
/// _ => None,
/// }
/// }
/// }
///
///
/// ```
///
/// ### Equivalences
///
/// This example demonstrates which invocations of `TS` produce the same type.
///
/// ```rust
/// use tstr::TS;
///
/// type Hello1 = TS!("hello");
/// type Hello2 = TS!(hello); // This is equivalent to `TS!("hello")`
///
/// type HundredA = TS!("100");
/// type HundredB = TS!(100); // equivalent to `TS!("100")`
/// type HundredC = TS!(0x64); // equivalent to `TS!("100")`
/// type HundredD = TS!(0b1100100); // equivalent to `TS!("100")`
///
/// type Tup = TS!(foo, 1, "bar"); // equivalent to `(TS!(foo), TS!(1), TS!(bar))`
///
/// // Equivalent to TS!("foo4bar200")
/// type Conc = TS!(concat!(foo, 0b100, "bar", 200));
///
/// ```
///
/// [`TStr`]: ./struct.TStr.html
#[macro_export]
macro_rules! TS {
($($expr:expr),* $(,)* ) => {
$crate::__ts_impl!(($crate) $($expr)*)
};
}
/// A type-level string [`TStr`] value.
///
/// # Arguments
///
/// You can use anything that the [`tstr::TS`] macro accepts
///
/// # Examples
///
/// ### Indexing
///
/// This uses types from the `for_examples` module,
/// which can be seen in the docs with the "for_examples" feature.
///
/// ```rust
/// use tstr::ts;
/// use tstr::for_examples::{Foo, Bar};
///
/// let this = Foo::new(3, 5, "8");
/// assert_eq!(this[ts!(bar)], 3);
/// assert_eq!(this[ts!(baz)], 5);
/// assert_eq!(this[ts!(qux)], "8");
///
/// let other = Bar::new(13, false, Some('C'));
/// assert_eq!(other[ts!(bar)], 13);
/// assert_eq!(other[ts!(baz)], false);
/// assert_eq!(other[ts!(boom)], Some('C'));
///
/// ```
/// ### Equivalences
///
/// This example demonstrates which invocations of `ts` produce the same values.
///
/// ```rust
/// use tstr::ts;
///
/// let hello1 = ts!("hello");
/// let hello2 = ts!(hello); // This is equivalent to `ts!("hello")`
///
/// let hundreda = ts!("100");
/// let hundredb = ts!(100); // equivalent to `ts!("100")`
/// let hundredc = ts!(0x64); // equivalent to `ts!("100")`
/// let hundredd = ts!(0b1100100); // equivalent to `ts!("100")`
///
/// let tup = ts!(foo, 1, "bar"); // equivalent to `(ts!(foo), ts!(1), ts!(bar))`
///
/// // Equivalent to ts!("foo4bar200")
/// let conc = ts!(concat!(foo, 0b100, "bar", 200));
/// # const _: tstr::TS!("foo4bar200") = ts!(concat!(foo, 0b100, "bar", 200));
/// ```
///
///
/// [`TStr`]: ./struct.TStr.html
/// [`tstr::TS`]: ./macro.TS.html#arguments
#[macro_export]
macro_rules! ts {
($($expr:expr),* $(,)* ) => {{
let __look_at_the_notes__ =
<$crate::__ts_impl!(($crate) $($expr)*) as $crate::MakeTStr>::MAKE;
__look_at_the_notes__
}};
}
/// Declares `const` and `type` aliases for type-level strings.
///
/// # String Arguments
///
/// You can alias either one type-level string, or a tuple of type-level strings
///
/// ```rust
/// tstr::alias!{
/// // Aliases the "bar" type-level string as both a const and a type, named Bar.
/// pub Bar = bar;
///
/// // Aliases the "0" type-level string.
/// pub N0 = 0;
///
/// // Aliases the ("foo", "baz") tuple of type-level strings,
/// // Equivalent to `TS!(foo, baz)` and `ts!("foo", "baz")`
/// pub Tup = (foo, baz);
/// }
///
/// # const _: (tstr::TS!(foo), tstr::TS!(baz)) = Tup;
/// ```
///
/// # Examples
///
/// ### Indexing
///
/// This uses types from the `for_examples` module,
/// which can be seen in the docs with the "for_examples" feature.
///
/// ```rust
/// use std::ops::Index;
///
/// use tstr::for_examples::{Foo, Bar};
///
/// tstr::alias!{
/// // Declares both an NBar type alias and an NBar constant of that type.
/// pub NBar = bar;
///
/// // Declares both an NBaz type alias and an NBaz constant of that type.
/// pub NBaz = "baz";
///
/// // Declares both an NQux type alias and an NQux constant of that type.
/// pub NQux = "qux";
///
/// }
///
/// // The macro can also be invoked like this
/// tstr::alias!{ pub NBoom = boom }
///
/// let this = Foo::new(3, 5, "8");
/// assert_eq!(get_bar_baz(&this), (3, 5));
///
/// let other = Bar::new(13, false, Some('C'));
/// assert_eq!(get_bar_baz(&other), (13, false));
///
///
/// type IndexOut<T, N> = <T as Index<N>>::Output;
///
/// fn get_bar_baz<T>(this: &T) -> (IndexOut<T, NBar>, IndexOut<T, NBaz>)
/// where
/// T: Index<NBar> + Index<NBaz>,
/// IndexOut<T, NBar>: Copy,
/// IndexOut<T, NBaz>: Copy,
/// {
/// (this[NBar], this[NBaz])
/// }
///
/// ```
///
#[macro_export]
macro_rules! alias {
(
$(
$(#[$attr:meta])*
$vis:vis $name:ident = $expr:tt
);*
$(;)?
) => (
$(
$crate::__priv_alias!{
@decide-docs
(
$(#[$attr])*
$vis,
$name,
)
[$expr]
}
)*
);
}
#[doc(hidden)]
#[macro_export]
macro_rules! __priv_alias {
(@decide-docs
$other:tt
[($($expr:expr),* $(,)*)]
)=>{
$crate::__priv_alias!{
@inner
$other
[$($expr),*]
concat!(
"An alias for `(",
$(stringify!($expr), ", ",)*
")` as a tuple of type level strings.\n\n",
"Generated by the [`::tstr::alias`] macro."
)
}
};
(@decide-docs
$other:tt
[$expr:expr]
)=>{
$crate::__priv_alias!{
@inner
$other
[$expr]
concat!(
"An alias for `", stringify!($expr), "` as a type level string.\n\n",
"Generated by the [`::tstr::alias`] macro."
)
}
};
(@inner
(
$(#[$attr:meta])*
$vis:vis, $name:ident,
)
[$($expr:expr),*]
$autodoc:expr
)=>{
$(#[$attr])*
#[allow(broken_intra_doc_links)]
#[doc = $autodoc]
$vis type $name = $crate::TS!($($expr),*);
$(#[$attr])*
#[allow(non_upper_case_globals, broken_intra_doc_links)]
#[doc = $autodoc]
$vis const $name: $name = <$name as $crate::MakeTStr>::MAKE;
};
}