typeslice-macros

Crates.iotypeslice-macros
lib.rstypeslice-macros
version0.1.2
sourcesrc
created_at2024-03-01 17:25:37.424271
updated_at2024-03-01 20:14:07.301468
descriptiontype-level slices
homepagehttps://github.com/aatifsyed/typeslice
repositoryhttps://github.com/aatifsyed/typeslice
max_upload_size
id1159097
size5,702
Aatif Syed (aatifsyed)

documentation

https://docs.rs/typeslice

README

Type-level slices of primitives.

Rust permits certain constant parameters in generics:

struct Foo<const CHAR: char>;

Presently these are limited to the primitive integers, [prim@char] and [prim@bool], so e.g slices of different chars cannot be represented.

struct Fails<const CHARS: [char]>;
type Message = Fails<['h', 'e', 'l', 'l', 'o']>;

This crate emulates the above with recursive types, and the TypeSlice trait.

type Message = typeslice::char!['h', 'e', 'l', 'l', 'o'];
// or, equivalently
type Message = typeslice::from_str!("hello");

You can inspect the message at const time or runtime through the List in TypeSlice::LIST:

use typeslice::TypeSlice;

fn get_reply<T: TypeSlice<char>>() -> &'static str {
    if T::LIST.slice_eq(&['h', 'i']) {
        return "hello"
    }
    if T::LIST.into_iter().copied().eq("salut".chars()) {
        return "bonjour"
    }
    "¿que?"
}

assert_eq!(get_reply::<typeslice::from_str!("hi")>(), "hello");

If you enjoy this crate, you may also like typenum or frunk

Commit count: 0

cargo fmt