use std::io;
use pod::Pod;
#[cfg(feature = "uninitialized")]
use uninitialized::uninitialized;
#[cfg(not(feature = "uninitialized"))]
use std::mem::zeroed as uninitialized;
#[cfg(feature = "read_exact")]
use read_exact::ReadExactExt;
pub trait PodReadExt {
fn read_pod<P: Pod>(&mut self) -> io::Result<P>;
#[cfg(feature = "read_exact")]
fn read_pod_or_none<P: Pod>(&mut self) -> io::Result<Option<P>>;
}
impl<T: io::Read> PodReadExt for T {
#[inline]
fn read_pod<P: Pod>(&mut self) -> io::Result<P> {
let mut data: P = unsafe { uninitialized() };
self.read_exact(data.as_bytes_mut()).map(|_| data)
}
#[inline]
#[cfg(feature = "read_exact")]
fn read_pod_or_none<P: Pod>(&mut self) -> io::Result<Option<P>> {
let mut data: P = unsafe { uninitialized() };
self.read_exact_or_eof(data.as_bytes_mut()).map(|read| if read {
Some(data)
} else {
None
})
}
}
pub trait PodWriteExt {
fn write_pod<P: Pod>(&mut self, data: &P) -> io::Result<()>;
}
impl<T: io::Write> PodWriteExt for T {
#[inline]
fn write_pod<P: Pod>(&mut self, data: &P) -> io::Result<()> {
self.write_all(data.as_bytes())
}
}