#fixed #size #str #string #array

macro no-std fixed_len_str

A procedural macro for create a smart pointer to str backed by a fixed size array,with the size given by the tokens

22 releases

0.3.3 Jan 30, 2020
0.3.2 Jan 29, 2020
0.2.9 Jan 22, 2020
0.1.9 Jan 9, 2020

#9 in #fixed-size

Download history 25/week @ 2023-10-19 55/week @ 2023-10-26 35/week @ 2023-11-02 23/week @ 2023-11-09 23/week @ 2023-11-16 50/week @ 2023-11-23 106/week @ 2023-11-30 21/week @ 2023-12-07 47/week @ 2023-12-14 62/week @ 2023-12-21 14/week @ 2023-12-28 20/week @ 2024-01-04 25/week @ 2024-01-11 61/week @ 2024-01-18 62/week @ 2024-01-25 57/week @ 2024-02-01

210 downloads per month
Used in 6 crates (2 directly)

Unlicense

12KB
101 lines

fixed_len_str

This Rust library provides a procedural macro for declare a wrapper struct for an array with the size given by the tokens which derefs to str.

For a proper API documentation of one of length 12 see fixed_len_str_example.

If you want to use serde to serialize and deserialize the fixed_len_str use features = ["serde_support"],if you want to match a pattern of type FnMut(FixedStr$len) -> bool on a str use features = ["pattern_pred_support"] and if you want the documentation visible at the expansion use default-features = false.

Usage

use fixed_len_str::fixed_len_str;

fixed_len_str!(3);

fn main() {
    let string = FixedStr3::from("abc");
    
    assert_eq!(string, "abc");
    
    let string = unsafe { FixedStr3::new_unchecked(*b"abc") };
    
    assert_eq!(string, "abc");
    
    let mut string = FixedStr3::default(); // equivalent to mem::zeroed but safe
    string.fill_str("abc");
    
    assert_eq!(string, "abc");
    
    let mut string = unsafe { FixedStr3::new_unchecked([b'a', b'b', 0]) };
    assert_eq!(string, "ab");
    string.fill_char('c');
    
    assert_eq!(string, "abc");
    assert_eq!(string.as_bytes(), (&string[..]).as_bytes()); // this is only certain with non-zero bytes
    assert_eq!(string.into_string(), String::from(&string[..])); // clone or consume at your option
    assert_eq!(FixedStr3::from(&[][..]), "");

    if cfg!(feature = "pattern_pred_support") {
        use fixed_str3::Closure; // needed due to the orphan rule
    
        assert_eq!("aaabb".matches(Closure::from(|s: FixedStr3| s == "aaa" || s == "bb"))
                          .collect::<Vec<&str>>(), ["aaa", "bb"]);
    }
}

Dependencies

~180KB