[][src]Macro fixed_len_str::fixed_len_str_nz

fixed_len_str_nz!() { /* proc-macro */ }

A macro for declare an FixedStrinput struct with NonZeroU8 as utf8 encoded bytes.

Examples

#![feature(proc_macro_hygiene)] 
// this is only needed here as I expanding as statements the respective items
#![feature(pattern)] 
// needed for use the unstable API pattern 
 
use fixed_len_str::fixed_len_str_nz;
use core::mem::transmute;
 
fixed_len_str_nz!(3);
 
let string = FixedStrNZ3::from("abc");
 
assert_eq!(string, "abc");
 
let string = FixedStrNZ3::new(unsafe { transmute(*b"abc") }).unwrap();
 
assert_eq!(string, "abc");
assert_eq!(string.as_bytes(), (&string[..]).as_bytes()); 
assert_eq!(string.into_string(), String::from(&string[..])); // clone or consume at your option
 
assert_eq!(FixedStrNZ3::from(unsafe { transmute(&[b'a', b'b', b'c', b'd']) }).as_ref(), "abc");
 
if cfg!(feature = "pattern_pred_support") {
    use fixed_str_nz3::Closure; // needed due to the orphan rule
 
    assert_eq!("aaabb".matches(Closure::from(|s: FixedStrNZ3| s == "aaa" || s == "bb"))
                      .collect::<Vec<&str>>(), ["aaa"]);
}