Struct tstr::TStr

source · []
pub struct TStr<T>(_);
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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Gets a value of this type

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

Available on crate feature cmp_traits only.

Whether Self equals Rhs

Available on crate feature cmp_traits only.

Whether Self is not equal to Rhs

Available on crate feature cmp_traits only.

Returns whether self is equal to other.

Available on crate feature cmp_traits only.

Returns whether self is not equal to other.

Available on crate feature cmp_traits only.

The Ordering of Self relative to Rhs.

Available on crate feature cmp_traits only.

Compares self and other for ordering.

The u128 value of the type.

The usize value of the type. Read more

Gets the usize value of this type Read more

Gets the u128 value of this type

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.