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
use core::{
fmt::{self, Debug},
marker::PhantomData,
};
/// A type-level string type, similar to a `&'static str` const parameter.
///
/// # Examples
///
/// ### Accessing Fields
///
/// This example demonstrates how you can use `TStr` to implement a generic accessor trait.
///
/// ```rust
/// use tstr::TStr;
/// use tstr::{TS, ts};
///
/// fn main() {
/// let mut tup = (3, 5, 8);
///
/// assert_eq!(tup.get(ts!(0)), &3);
/// assert_eq!(tup.get(ts!(1)), &5);
/// assert_eq!(tup.get(ts!(2)), &8);
///
/// let old_0 = replace(&mut tup, ts!(0), 333);
/// let old_1 = replace(&mut tup, ts!(1), 555);
/// let old_2 = replace(&mut tup, ts!(2), 888);
///
/// assert_eq!(tup.get(ts!(0)), &333);
/// assert_eq!(tup.get(ts!(1)), &555);
/// assert_eq!(tup.get(ts!(2)), &888);
///
/// assert_eq!(old_0, 3);
/// assert_eq!(old_1, 5);
/// assert_eq!(old_2, 8);
///
/// }
///
/// fn replace<T, N>(this: &mut T, name: TStr<N>, replacement: T::Field) -> T::Field
/// where
/// T: Access<TStr<N>>,
/// T::Field: Clone,
/// {
/// let ret = this.get(name).clone();
/// this.set(name, replacement);
/// ret
/// }
///
///
/// trait Access<N> {
/// type Field;
///
/// fn get(&self, _field_name: N) -> &Self::Field;
/// fn set(&mut self, _field_name: N, val: Self::Field);
/// }
///
/// impl<A, B, C> Access<TS!(0)> for (A, B, C) {
/// type Field = A;
///
/// fn get(&self, _field_name: TS!(0)) -> &A {
/// &self.0
/// }
/// fn set(&mut self, _field_name: TS!(0), val: A){
/// self.0 = val;
/// }
/// }
///
/// impl<A, B, C> Access<TS!(1)> for (A, B, C) {
/// type Field = B;
///
/// fn get(&self, _field_name: TS!(1)) -> &B {
/// &self.1
/// }
/// fn set(&mut self, _field_name: TS!(1), val: B){
/// self.1 = val;
/// }
/// }
///
/// impl<A, B, C> Access<TS!(2)> for (A, B, C) {
/// type Field = C;
///
/// fn get(&self, _field_name: TS!(2)) -> &C {
/// &self.2
/// }
/// fn set(&mut self, _field_name: TS!(2), val: C){
/// self.2 = val;
/// }
/// }
///
/// ```
///
pub struct TStr<T>(pub(crate) PhantomData<fn() -> T>);
impl<T> TStr<T> {
/// Constructs the TStr.
///
/// # Example
///
/// ```rust
/// use tstr::{TS, TStr};
///
/// type FOO = TS!(foo);
///
/// let foo_1: FOO = TStr::NEW;
/// let foo_2 = FOO::NEW; // The same as the previous statement
///
/// ```
pub const NEW: Self = TStr(PhantomData);
}
#[cfg(feature = "const_generics")]
macro_rules! const_generics_using {
() => {
/// For getting the `&'static str` value of this [`TStr`].
///
/// You can use this as the bound for a generic [`TStr`] parameter.
///
/// # Example
///
/// ```rust
/// use tstr::{StrValue, ts};
///
/// asserts(ts!(foo), ts!(bar), ts!(baz));
///
/// fn asserts<A, B, C>(foo: A, bar: B, baz: C)
/// where
/// A: StrValue,
/// B: StrValue,
/// C: StrValue,
/// {
/// assert_eq!(A::STR, "foo");
/// assert_eq!(foo.to_str(), "foo");
///
/// assert_eq!(B::STR, "bar");
/// assert_eq!(bar.to_str(), "bar");
///
/// assert_eq!(C::STR, "baz");
/// assert_eq!(baz.to_str(), "baz");
///
/// }
///
/// ```
///
/// [`TStr`]: ./struct.TStr.html
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "const_generics")))]
pub trait StrValue: Debug + Copy + Default + 'static {
/// The `&'static str` value of this `TStr`.
const STR: &'static str;
/// Gets the `&'static str` value of this `TStr`.
fn to_str(self) -> &'static str {
Self::STR
}
}
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "const_generics")))]
impl<const S: &'static str> StrValue for TStr<crate::___<S>> {
const STR: &'static str = S;
}
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "const_generics")))]
impl<T> TStr<T>
where
Self: StrValue,
{
/// The `&'static str` value of this `TStr`.
///
/// # Example
///
/// ```rust
/// use tstr::TS;
///
/// type FOO = TS!(foo);
/// type BAR = TS!(bar);
///
/// assert_eq!(FOO::STR, "foo");
/// assert_eq!(BAR::STR, "bar");
///
/// ```
pub const STR: &'static str = <Self as StrValue>::STR;
}
};
}
#[cfg(feature = "const_generics")]
const_generics_using! {}
impl<T> Copy for TStr<T> {}
impl<T> Clone for TStr<T> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
impl<T> Default for TStr<T> {
#[inline(always)]
fn default() -> Self {
Self::NEW
}
}
impl<T> Debug for TStr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TStr").finish()
}
}
impl<T> core::cmp::PartialEq for TStr<T> {
#[inline(always)]
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl<T> core::cmp::Eq for TStr<T> {}
impl<T> core::cmp::PartialOrd for TStr<T> {
#[inline(always)]
fn partial_cmp(&self, _other: &Self) -> Option<core::cmp::Ordering> {
Some(core::cmp::Ordering::Equal)
}
}
impl<T> core::cmp::Ord for TStr<T> {
#[inline(always)]
fn cmp(&self, _other: &Self) -> core::cmp::Ordering {
core::cmp::Ordering::Equal
}
}