wasmer/lib/emscripten/src/file_descriptor.rs

26 lines
675 B
Rust
Raw Normal View History

2018-12-21 05:50:24 +00:00
use std::io;
use std::io::Error;
use std::io::ErrorKind;
2018-12-21 07:08:00 +00:00
use std::io::Read;
2018-12-21 05:50:24 +00:00
pub struct FileDescriptor(libc::c_int);
impl FileDescriptor {
pub fn new(file_descriptor_number: libc::c_int) -> FileDescriptor {
FileDescriptor(file_descriptor_number)
}
}
impl Read for FileDescriptor {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let file_descriptor: libc::c_int = self.0;
2018-12-21 07:08:00 +00:00
let count =
unsafe { libc::read(file_descriptor, buf.as_mut_ptr() as *mut libc::c_void, 1) };
2018-12-21 05:50:24 +00:00
if count < 0 {
Err(Error::new(ErrorKind::Other, "read error"))
2018-12-21 07:08:00 +00:00
} else {
2018-12-21 05:50:24 +00:00
Ok(count as usize)
}
}
2018-12-21 07:08:00 +00:00
}