Skip to main content

rustjava_runtime/runtime/
io.rs

1use alloc::boxed::Box;
2
3use dyn_clone::{DynClone, clone_trait_object};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
6pub struct FileDescriptorId(u32);
7
8impl FileDescriptorId {
9    pub fn new(id: u32) -> Self {
10        Self(id)
11    }
12
13    pub fn id(&self) -> u32 {
14        self.0
15    }
16}
17
18#[derive(Debug)]
19pub enum IOError {
20    Unsupported,
21    NotFound,
22    Io,
23}
24
25pub type IOResult<T> = Result<T, IOError>;
26pub type FileSize = u64;
27
28#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
29pub struct FileOpenOptions {
30    pub read: bool,
31    pub write: bool,
32    pub append: bool,
33    pub truncate: bool,
34    pub create: bool,
35}
36
37#[derive(Eq, PartialEq)]
38pub enum FileType {
39    File,
40    Directory,
41}
42
43pub struct FileStat {
44    pub size: FileSize,
45    pub r#type: FileType,
46}
47
48#[async_trait::async_trait]
49pub trait File: Send + DynClone {
50    async fn read(&mut self, buf: &mut [u8]) -> IOResult<usize>;
51    async fn write(&mut self, buf: &[u8]) -> IOResult<usize>;
52    async fn seek(&mut self, pos: FileSize) -> IOResult<()>;
53    async fn tell(&self) -> IOResult<FileSize>;
54    async fn set_len(&mut self, len: FileSize) -> IOResult<()>;
55    async fn metadata(&self) -> IOResult<FileStat>;
56}
57
58clone_trait_object!(File);