[−][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_ref().as_bytes()); assert_eq!(string.clone().into_string(), String::from(string.as_ref())); // clone or consume at your option assert_eq!(FixedStrNZ3::from_vec(unsafe { transmute(vec![b'a', b'b', b'c', b'd']) }).as_ref(), "abc"); // the vector is transmuted from Vec<u8> into Vec<NonZeroU8>,because both types share the same layout it is sound if cfg!(feature = "pattern_pred_support") { use fixed_str_nz3::Closure; // needed due to the orphan rule assert_eq!("aaaaa".matches(Closure::from(|s: FixedStrNZ3| s == "aaa" || s == "aa")) .collect::<Vec<&str>>(), ["aaa"]); }