|
@ -7,5 +7,40 @@ pub fn array_to_utf8<const X: usize>(array: [u8; X]) -> Result<String, std::stri |
|
|
}
|
|
|
}
|
|
|
s
|
|
|
s
|
|
|
})
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*pub fn utf8_to_array<const X: usize>(string: String) -> Result<[u8; X], std::string::FromUtf8Error> {
|
|
|
|
|
|
let mut array = [0u8; X];
|
|
|
|
|
|
let bytes = string.as_bytes();
|
|
|
|
|
|
array[..bytes.len()].clone_from_slice(&bytes);
|
|
|
|
|
|
Ok(array)
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
|
macro_rules! utf8_to_array {
|
|
|
|
|
|
($s: expr, $size: expr) => {
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut array = [0u8; $size];
|
|
|
|
|
|
let bytes = $s.as_bytes();
|
|
|
|
|
|
array[..bytes.len()].clone_from_slice(&bytes);
|
|
|
|
|
|
array
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod test {
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_utf8_to_array() {
|
|
|
|
|
|
let s = "asdf".to_owned();
|
|
|
|
|
|
let a = utf8_to_array!(s, 8);
|
|
|
|
|
|
|
|
|
|
|
|
let mut e = [0u8; 8];
|
|
|
|
|
|
e[..4].clone_from_slice(b"asdf");
|
|
|
|
|
|
assert!(a == e);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|