1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
use std::ffi::CString; use std::fmt::{Debug, Formatter, Result}; use std::str; use std::os::raw::c_uchar; pub const MAXLEN: usize = 1024; #[repr(C)] #[derive(Copy)] pub struct AiString { pub length: usize, pub data: [c_uchar; MAXLEN], } impl Default for AiString { fn default() -> AiString { AiString { length: 0, data: [0; MAXLEN], } } } impl AsRef<str> for AiString { fn as_ref(&self) -> &str { str::from_utf8(&self.data[0..self.length]).unwrap() } } impl<'a> From<&'a str> for AiString { fn from(s: &str) -> AiString { assert!(s.len() < MAXLEN); let cstr = CString::new(s).unwrap(); let bytes = cstr.to_bytes(); let mut aistr = AiString { length: s.len() as usize, data: [0; MAXLEN], }; for i in 0..s.len() { aistr.data[i] = bytes[i]; } aistr } } impl Clone for AiString { fn clone(&self) -> AiString { *self } } impl Debug for AiString { fn fmt(&self, f: &mut Formatter) -> Result { let s: &str = self.as_ref(); write!(f, "{:?}", s) } } impl PartialEq for AiString { fn eq(&self, other: &AiString) -> bool { self.as_ref() == other.as_ref() } } impl Eq for AiString {}