Giter Club home page Giter Club logo

tstr_crates's Introduction

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.

tstr_crates's People

Contributors

rodrimati1992 avatar

Stargazers

Vlad Zagvozdkin avatar Ilya Salauyeu avatar Richie Seputro avatar Jay avatar Qqwy / Marten avatar Tiago Carvalho avatar Tomas Zemanovic avatar Jakub Dąbek avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

nwalfield

tstr_crates's Issues

tstr with `const_generics` doesn't compile

I'm trying to use StrValue. This requires enabling const_generics. Unfortunately, when I enable that feature, tstr doesn't compile any more:

$ rustc --version
rustc 1.76.0 (07dca489a 2024-02-04)
$ cargo build
   Compiling tstr v0.2.3
error: `&'static str` is forbidden as the type of a const generic parameter
  --> /home/us/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tstr-0.2.3/src/./p.rs:40:33
   |
40 |         pub struct ___<const S: &'static str>;
   |                                 ^^^^^^^^^^^^
...
52 | declare_const_items! {}
   | ----------------------- in this macro invocation
   |
   = note: the only supported types are integers, `bool` and `char`
   = note: this error originates in the macro `declare_const_items` (in Nightly builds, run with -Z macro-backtrace for more info)
...

I'm using an empty project created by cargo init and just add the following to Cargo.toml:

tstr = { version = "0.2", features = ["const_generics"] }

I tried compiling with rustc version 1.46, which I think should be supported, but I got the same error (I had to also change the edition to 2018). As such, I think I'm probably holding it wrong, and my request is to improve the documentation.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.