瀏覽代碼

Merge pull request #127 from stratact/master

Update `platform::rawfile`
Jeremy Soller 6 年之前
父節點
當前提交
1f9f7ce6c8
共有 1 個文件被更改,包括 24 次插入5 次删除
  1. 24 5
      src/platform/src/rawfile.rs

+ 24 - 5
src/platform/src/rawfile.rs

@@ -1,14 +1,33 @@
 use core::ops::Deref;
+use super::{open, dup, close, types::*};
 
-pub struct RawFile(usize);
+pub struct RawFile(c_int);
 
 impl RawFile {
-    pub fn open<T: AsRef<[u8]>>(path: T, flags: usize) -> Result<RawFile> {
-        open(path, flags).map(RawFile)
+    pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> Result<RawFile, ()> {
+        match open(path, oflag, mode) {
+            -1 => Err(()),
+            n => Ok(RawFile(n))
+        }
     }
 
-    pub fn dup(&self, buf: &[u8]) -> Result<RawFile> {
-        dup(self.0, buf).map(RawFile)
+    pub fn dup(&self, _buf: &[u8]) -> Result<RawFile, ()> {
+        match dup(self.0) {
+            -1 => Err(()),
+            n => Ok(RawFile(n))
+        }
+    }
+
+    pub fn as_raw_fd(&self) -> c_int {
+        self.0
+    }
+
+    pub fn into_raw_fd(self) -> c_int {
+        self.0
+    }
+
+    pub fn from_raw_fd(fd: c_int) -> Self {
+        RawFile(fd)
     }
 }