refinement_types/
type_str.rs

1//! Type-level strings.
2
3use crate::static_str::StaticStr;
4
5/// Represents type-level strings.
6pub trait TypeStr {
7    /// The string value.
8    const VALUE: StaticStr;
9}
10
11#[doc(hidden)]
12pub mod import {
13    pub use core::{fmt, marker::PhantomData};
14}
15
16/// Lifts static strings to type-level strings.
17///
18/// # Examples
19///
20/// ```
21/// use refinement_types::type_str;
22///
23/// type_str!(HelloWorld = "Hello, world!");
24/// ```
25///
26/// Is equivalent to:
27///
28/// ```
29/// use core::{fmt, marker::PhantomData};
30///
31/// use refinement_types::{StaticStr, TypeStr};
32///
33/// struct HelloWorld {
34///     private: PhantomData<()>,
35/// }
36///
37/// impl TypeStr for HelloWorld {
38///     const VALUE: StaticStr = "Hello, world!";
39/// }
40/// ```
41#[macro_export]
42macro_rules! type_str {
43    ($vis: vis $name: ident = $value: expr $(=> $doc: expr)?) => {
44        $(
45            #[doc = $doc]
46        )?
47        $vis struct $name {
48            private: $crate::type_str::import::PhantomData<()>,
49        }
50
51        impl $crate::type_str::TypeStr for $name {
52            const VALUE: $crate::static_str::StaticStr = $value;
53        }
54    };
55}