Struct tstr::TStr

source ·
pub struct TStr<T>(/* private fields */);
Expand description

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.

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;
    }
}

Implementations§

source§

impl<T> TStr<T>

source

pub const NEW: Self = _

Constructs the TStr.

§Example
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
source§

impl<T> TStr<T>
where Self: StrValue,

source

pub const STR: &'static str = <Self as StrValue>::STR

Available on crate feature const_generics only.

The &'static str value of this TStr.

§Example
use tstr::TS;

type FOO = TS!(foo);
type BAR = TS!(bar);

assert_eq!(FOO::STR, "foo");
assert_eq!(BAR::STR, "bar");

Trait Implementations§

source§

impl<T> Clone for TStr<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for TStr<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for TStr<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> MakeTStr for TStr<T>

source§

const MAKE: Self = TStr::NEW

Gets a value of this type
source§

impl<T> Ord for TStr<T>

source§

fn cmp(&self, _other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T> PartialEq for TStr<T>

source§

fn eq(&self, _other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialOrd for TStr<T>

source§

fn partial_cmp(&self, _other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, U> TStrEq<TStr<U>> for TStr<T>
where T: TStrEq<U>,

Available on crate feature cmp_traits only.
source§

const EQ: bool = T::EQ

Whether Self equals Rhs
source§

const NE: bool = _

Whether Self is not equal to Rhs
source§

fn tstr_eq(&self, _other: &Rhs) -> bool

Returns whether self is equal to other.
source§

fn tstr_ne(&self, _other: &Rhs) -> bool

Returns whether self is not equal to other.
source§

impl<T, U> TStrOrd<TStr<U>> for TStr<T>
where T: TStrOrd<U>,

Available on crate features cmp_traits and const_generics only.
source§

const CMP: Ordering = T::CMP

The Ordering of Self relative to Rhs.
source§

fn tstr_cmp(&self, _other: &Rhs) -> Ordering

Compares self and other for ordering.
source§

impl<T> ToUint for TStr<T>
where T: ToUint,

source§

const U128: u128 = T::U128

The u128 value of the type.
source§

const USIZE: usize = _

The usize value of the type. Read more
source§

fn to_usize(&self) -> usize

Gets the usize value of this type Read more
source§

fn to_u128(&self) -> u128

Gets the u128 value of this type
source§

impl<T> Copy for TStr<T>

source§

impl<T> Eq for TStr<T>

Auto Trait Implementations§

§

impl<T> Freeze for TStr<T>

§

impl<T> RefUnwindSafe for TStr<T>

§

impl<T> Send for TStr<T>

§

impl<T> Sync for TStr<T>

§

impl<T> Unpin for TStr<T>

§

impl<T> UnwindSafe for TStr<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.