Newtype file descriptors.
To avoid various bugs regarding the typing of file descriptors, we newtype them into a simple wrapper type. - Document some stuff.
This commit is contained in:
parent
3a232cc60f
commit
94a1a0fa0c
3 changed files with 24 additions and 11 deletions
|
@ -1,7 +1,7 @@
|
|||
use core::str;
|
||||
|
||||
use syscall::Result;
|
||||
use super::Scheme;
|
||||
use super::{Scheme, Fd};
|
||||
|
||||
pub struct DebugScheme;
|
||||
|
||||
|
@ -14,21 +14,21 @@ impl Scheme for DebugScheme {
|
|||
/// Read the file `number` into the `buffer`
|
||||
///
|
||||
/// Returns the number of bytes read
|
||||
fn read(&mut self, _file: usize, _buffer: &mut [u8]) -> Result<usize> {
|
||||
fn read(&mut self, _file: Fd, _buffer: &mut [u8]) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// Write the `buffer` to the `file`
|
||||
///
|
||||
/// Returns the number of bytes written
|
||||
fn write(&mut self, _file: usize, buffer: &[u8]) -> Result<usize> {
|
||||
fn write(&mut self, _file: Fd, buffer: &[u8]) -> Result<usize> {
|
||||
//TODO: Write bytes, do not convert to str
|
||||
print!("{}", unsafe { str::from_utf8_unchecked(buffer) });
|
||||
Ok(buffer.len())
|
||||
}
|
||||
|
||||
/// Close the file `number`
|
||||
fn close(&mut self, _file: usize) -> Result<()> {
|
||||
fn close(&mut self, _file: Fd) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,8 +17,11 @@ use syscall::Result;
|
|||
|
||||
use self::debug::DebugScheme;
|
||||
|
||||
pub use self::fd::Fd;
|
||||
|
||||
/// Debug scheme
|
||||
pub mod debug;
|
||||
mod fd;
|
||||
|
||||
/// Scheme list type
|
||||
pub type SchemeList = BTreeMap<Box<[u8]>, Arc<Mutex<Box<Scheme + Send>>>>;
|
||||
|
@ -50,16 +53,16 @@ pub trait Scheme {
|
|||
/// Returns a file descriptor or an error
|
||||
fn open(&mut self, path: &[u8], flags: usize) -> Result<usize>;
|
||||
|
||||
/// Read the file `number` into the `buffer`
|
||||
/// Read from some file descriptor into the `buffer`
|
||||
///
|
||||
/// Returns the number of bytes read
|
||||
fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize>;
|
||||
fn read(&mut self, fd: Fd, buffer: &mut [u8]) -> Result<usize>;
|
||||
|
||||
/// Write the `buffer` to the `file`
|
||||
/// Write the `buffer` to the file descriptor
|
||||
///
|
||||
/// Returns the number of bytes written
|
||||
fn write(&mut self, file: usize, buffer: &[u8]) -> Result<usize>;
|
||||
fn write(&mut self, fd: Fd, buffer: &[u8]) -> Result<usize>;
|
||||
|
||||
/// Close the file `number`
|
||||
fn close(&mut self, file: usize) -> Result<()>;
|
||||
/// Close the file descriptor
|
||||
fn close(&mut self, fd: Fd) -> Result<()>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue