#str #proc-macro #byte-array #const #string #macro #byte-slice

macro proc_strarray

Create const u8 array from str or byte str literal

9 stable releases

new 1.6.0 Dec 1, 2024
1.5.1 Nov 29, 2024
1.1.0 Jul 31, 2024

#282 in Procedural macros

Download history 20/week @ 2024-09-10 9/week @ 2024-09-17 8/week @ 2024-09-24 1/week @ 2024-10-01 218/week @ 2024-11-19 659/week @ 2024-11-26

877 downloads per month
Used in fnv0

MIT/Apache

32KB
504 lines

Procedural macros for str and byte str literals

MIT OR Apache License Crates.io Version MSRV 1.57 Safe Rust Documentation Crates.io Downloads

Create byte array from str or byte str

Procedural macro proc_strarray::str_array creates const u8 array from str literal. Variant proc_strarray::str_array0 creates zero terminated u8 array.

Macro str_array takes three arguments:

  1. name of array to be created
  2. str or byte str literal
  3. expected length of str literal (optional argument, can be omitted). Length is only used for length check. It will not trim or extend an array.

Repeat str or byte str

  1. macro str_repeat repeats str or byte str literal n times.
  2. macro str_repeat0 repeats str or byte str literal n times and adds NUL termination.

Repeat str or byte str as bytes

  1. macro str_repeat_bytes repeats str or byte str literal n times as byte slice.
  2. macro str_repeat_bytes0 repeats str or byte str literal n times as byte slice and adds NUL termination.

Return str or byte str length

  1. macro str_len returns length of str or byte str literal.
  2. macro str_len0 returns length of zero terminated str or byte str literal.

Create byte slice from str or byte str

  1. macro str_bytes creates byte slice from str or byte str literal.
  2. macro str_bytes0 creates zero terminated byte slice from str or byte str literal.

Usage

    // This code will create const array of u8
    // named STRU from content of "stru" str literal.
    use proc_strarray::str_array;
    str_array!(STRU, "stru");
    // check if newly created array have length 4
    assert_eq!(STRU.len(), 4);
    // check created array if first character is 's'
    assert_eq!(STRU[0], 's' as u8);

Documentation

rustdoc generated documentation can be found at docs.rs

License and MSRV

proc_strarray uses same license as Rust. MSRV is 1.57 and it will stay for all 1.X versions.

Dependencies

~1.5MB
~42K SLoC