rawfile.rs 866 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use core::ops::Deref;
  2. use super::{Pal, Sys, types::*};
  3. pub struct RawFile(c_int);
  4. impl RawFile {
  5. pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> Result<RawFile, ()> {
  6. match Sys::open(path, oflag, mode) {
  7. -1 => Err(()),
  8. n => Ok(RawFile(n)),
  9. }
  10. }
  11. pub fn dup(&self) -> Result<RawFile, ()> {
  12. match Sys::dup(self.0) {
  13. -1 => Err(()),
  14. n => Ok(RawFile(n)),
  15. }
  16. }
  17. pub fn as_raw_fd(&self) -> c_int {
  18. self.0
  19. }
  20. pub fn into_raw_fd(self) -> c_int {
  21. self.0
  22. }
  23. pub fn from_raw_fd(fd: c_int) -> Self {
  24. RawFile(fd)
  25. }
  26. }
  27. impl Drop for RawFile {
  28. fn drop(&mut self) {
  29. let _ = Sys::close(self.0);
  30. }
  31. }
  32. impl Deref for RawFile {
  33. type Target = c_int;
  34. fn deref(&self) -> &c_int {
  35. &self.0
  36. }
  37. }