#![feature(core)]
extern crate libc;
pub fn cvt<T: ::std::num::SignedInt>(t: T) -> ::std::io::Result<T> {
let one: T = ::std::num::Int::one();
if t == -one {
Err(::std::io::Error::last_os_error())
} else {
Ok(t)
}
}
#[derive(Copy)]
pub struct FdStream {
fd : ::libc::c_int,
}
impl FdStream {
pub fn new(fd : ::libc::c_int) -> FdStream {
FdStream { fd : fd }
}
}
impl ::std::io::Read for FdStream {
fn read(&mut self, buf : &mut [u8]) -> ::std::io::Result<usize> {
let ret = try!(cvt(unsafe {
::libc::read(self.fd,
buf.as_mut_ptr() as *mut ::libc::c_void,
buf.len() as ::libc::size_t)
}));
Ok(ret as usize)
}
}
impl ::std::io::Write for FdStream {
fn write(&mut self, buf : &[u8]) -> ::std::io::Result<usize> {
let ret = try!(cvt(unsafe {
::libc::write(self.fd,
buf.as_ptr() as *const ::libc::c_void,
buf.len() as ::libc::size_t)
}));
Ok(ret as usize)
}
fn flush(&mut self) -> ::std::io::Result<()> { Ok(()) }
}