> > + fn read_type(mut fobj: &File) -> Result<RecordType> > > + { > > + let mut tbuf = [0u8; 8]; > > + if let Err(e) = fobj.read_exact(&mut tbuf) { > > + if e.kind() == ErrorKind::UnexpectedEof { > > + return Ok(RecordType::Empty); > > + } else { > > + return Err(Error::ReadFile(e)); > > + } > > + } > > + > > + /* > > + * Safe because the layout of the trace record requires us to parse > > + * the type first, and then there is a check on the validity of the > > + * record type. > > + */ > > + let raw_t = > > + unsafe { std::mem::transmute::<[u8; 8], RecordRawType>(tbuf) }; > > A safe alternative: > https://doc.rust-lang.org/std/primitive.u64.html#method.from_ne_bytes?
Thanks! Will use it. > > + match raw_t.rtype { > > + RECORD_TYPE_MAPPING => Ok(RecordType::Mapping), > > + RECORD_TYPE_EVENT => Ok(RecordType::Event), > > + _ => Err(Error::UnknownRecType(raw_t.rtype)), > > + } > > + } > > +} [snip] > > +{ > > + fn read_header(mut fobj: &File) -> Result<Self> > > + { > > + let mut raw_hdr = [0u8; 24]; > > + fobj.read_exact(&mut raw_hdr).map_err(Error::ReadFile)?; > > + > > + /* > > + * Safe because the size of log header (struct LogHeader) > > + * is 24 bytes, which is ensured by simple trace backend. > > + */ > > + let hdr = > > + unsafe { std::mem::transmute::<[u8; 24], LogHeader>(raw_hdr) }; > > Or u64::from_ne_bytes() for each field. Will do. > > + Ok(hdr) > > + } > > +} [snip] > > +impl ReadHeader for RecordHeader > > +{ > > + fn read_header(mut fobj: &File) -> Result<Self> > > + { > > + let mut raw_hdr = [0u8; 24]; > > + fobj.read_exact(&mut raw_hdr).map_err(Error::ReadFile)?; > > + > > + /* > > + * Safe because the size of record header (struct RecordHeader) > > + * is 24 bytes, which is ensured by simple trace backend. > > + */ > > + let hdr: RecordHeader = > > + unsafe { std::mem::transmute::<[u8; 24], > > RecordHeader>(raw_hdr) }; > > Or u64::from_ne_bytes() and u32::from_ne_bytes() for all fields. Will do. Thanks, Zhao