llam 0.1.1

Safe, Go-style Rust bindings for the LLAM runtime
#[cfg(unix)]
use crate::io;
#[cfg(unix)]
use std::fs::File as StdFile;
#[cfg(unix)]
use std::io::{Read, Result, Write};
#[cfg(unix)]
use std::os::fd::{AsRawFd, RawFd};
#[cfg(unix)]
use std::path::Path;

#[cfg(unix)]
pub struct File {
    inner: StdFile,
}

#[cfg(unix)]
impl File {
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        Ok(Self {
            inner: StdFile::open(path)?,
        })
    }

    pub fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
        Ok(Self {
            inner: StdFile::create(path)?,
        })
    }

    pub fn from_std(inner: StdFile) -> Self {
        Self { inner }
    }

    pub fn into_std(self) -> StdFile {
        self.inner
    }
}

#[cfg(unix)]
impl Read for File {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        io::read(self.inner.as_raw_fd(), buf)
    }
}

#[cfg(unix)]
impl Write for File {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        io::write(self.inner.as_raw_fd(), buf)
    }

    fn flush(&mut self) -> Result<()> {
        self.inner.flush()
    }
}

#[cfg(unix)]
impl AsRawFd for File {
    fn as_raw_fd(&self) -> RawFd {
        self.inner.as_raw_fd()
    }
}