41 lines
740 B
Rust
41 lines
740 B
Rust
use io::{IoBase, Read, Write};
|
|
|
|
use crate::syscall;
|
|
|
|
#[derive(Debug)]
|
|
pub struct File {
|
|
fd: u64,
|
|
}
|
|
|
|
impl File {
|
|
/// # Safety
|
|
/// The file descriptor must be valid
|
|
pub unsafe fn from_raw_fd(fd: u64) -> Self {
|
|
Self { fd }
|
|
}
|
|
|
|
pub fn as_fd(&self) -> u64 {
|
|
self.fd
|
|
}
|
|
}
|
|
|
|
impl IoBase for File {
|
|
type Error = ();
|
|
}
|
|
|
|
impl Read for File {
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
|
Ok(syscall::read(self.as_fd(), buf) as usize)
|
|
}
|
|
}
|
|
|
|
impl Write for File {
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
|
Ok(syscall::write(self.as_fd(), buf) as usize)
|
|
}
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
todo!()
|
|
}
|
|
}
|