rodrimati1992 / tstr_crates

Type-level strings
zlib License
9 stars 1 forks source link

Rust crates-io api-docs

This crate provides an encoding of type-level strings as types.

Examples

Indexing

This example demonstrates how you can use type-level strings, and the Index trait, to access fields of generic types by name.

use std::ops::Index;

use tstr::{TS, ts};

fn main(){
    takes_person(&Person::new("Bob".into(), "Marley".into()));

    takes_person(&OtherPerson::new("Bob", "Marley"));
}

fn takes_person<P>(pers: &P)
where
    P: Index<TS!(name), Output = str> + Index<TS!(surname), Output = str>
{
    assert_eq!(&pers[ts!(name)], "Bob");
    assert_eq!(&pers[ts!(surname)], "Marley");
}

use person::Person;
mod person {
    use std::ops::Index;

    use tstr::TS;

    pub struct Person {
        name: String,
        surname: String,
    }

    impl Person {
        pub fn new(name: String, surname: String) -> Self {
            Self{name, surname}
        }
    }

    impl Index<TS!(name)> for Person {
        type Output = str;

        fn index(&self, _: TS!(name)) -> &str {
            &self.name
        }
    }

    impl Index<TS!(surname)> for Person {
        type Output = str;

        fn index(&self, _: TS!(surname)) -> &str {
            &self.surname
        }
    }
}

use other_person::OtherPerson;
mod other_person {
    use std::ops::Index;

    use tstr::TS;

    pub struct OtherPerson {
        name: &'static str,
        surname: &'static str,
    }

    impl OtherPerson {
        pub fn new(name: &'static str, surname: &'static str) -> Self {
            Self{name, surname}
        }
    }

    impl Index<TS!(name)> for OtherPerson {
        type Output = str;

        fn index(&self, _: TS!(name)) -> &str {
            self.name
        }
    }

    impl Index<TS!(surname)> for OtherPerson {
        type Output = str;

        fn index(&self, _: TS!(surname)) -> &str {
            self.surname
        }
    }
}

Macro expansion

This library reserves the right to change how it represent type-level strings internally in every single release, and cargo feature combination.

This only affects you if you expand the code generated by macros from this crate, and then use that expanded code instead of going through the macros.

Cargo features

No-std support

This crate is unconditionally #![no_std], and can be used anywhere that Rust can be.

Minimum Supported Rust Version

This crate supports Rust versions back to Rust 1.40.0.