7 releases

0.2.4 Mar 29, 2024
0.2.3 May 14, 2022
0.2.2 Sep 12, 2021
0.2.1 May 23, 2021
0.1.1 Jan 13, 2021

#370 in No standard library

Download history 4420/week @ 2024-01-03 3469/week @ 2024-01-10 4247/week @ 2024-01-17 3623/week @ 2024-01-24 3281/week @ 2024-01-31 3907/week @ 2024-02-07 5688/week @ 2024-02-14 4892/week @ 2024-02-21 6148/week @ 2024-02-28 4504/week @ 2024-03-06 6099/week @ 2024-03-13 4647/week @ 2024-03-20 5625/week @ 2024-03-27 4867/week @ 2024-04-03 4949/week @ 2024-04-10 3715/week @ 2024-04-17

20,069 downloads per month
Used in 29 crates (via repr_offset)

Zlib license

77KB
1.5K SLoC

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

  • "rust_1_46": Enables const functions in tstr::utils for comparing &str and &[u8].

  • "cmp_traits": Enables the traits for comparing type-level strings.

  • "use_syn": Changes how literals passed to the macros of this crate are parsed to use the syn crate. Use this if there is some literal that could not be parsed but is a valid str/integer literal.

  • "min_const_generics": changes the representation of type-level strings to use many char const parameter, making for better compiler errors for non-alphanumeric-ascii strings. Requires Rust 1.51.0.

  • "const_generics": Changes the representation of type-level strings to use a &'static str const parameter, making for better compiler errors, and a few more features. As of 2023-03-17, this feature can't be enabled, because it requires &'static str to be stably usable as const parameters. Consider using "nightly_const_generics" if this feature can't be used.

  • "nightly_const_generics": Equivalent to the "const_generics" feature, and enables the nightly compiler features to use &'static str const parameters.

  • "for_examples": Enables the for_examples module, with a few types used in documentation examples.

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.

Dependencies

~205KB