]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/hermit/fd.rs
84c547366473a1994cc4d02ed26d95f675f6358b
[rustc.git] / src / libstd / sys / hermit / fd.rs
1 #![unstable(reason = "not public", issue = "0", feature = "fd")]
2
3 use crate::io::{self, Read, ErrorKind};
4 use crate::mem;
5 use crate::sys::cvt;
6 use crate::sys::hermit::abi;
7 use crate::sys_common::AsInner;
8
9 #[derive(Debug)]
10 pub struct FileDesc {
11 fd: i32,
12 }
13
14 impl FileDesc {
15 pub fn new(fd: i32) -> FileDesc {
16 FileDesc { fd }
17 }
18
19 pub fn raw(&self) -> i32 { self.fd }
20
21 /// Extracts the actual file descriptor without closing it.
22 pub fn into_raw(self) -> i32 {
23 let fd = self.fd;
24 mem::forget(self);
25 fd
26 }
27
28 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
29 let result = unsafe { abi::read(self.fd, buf.as_mut_ptr(), buf.len()) };
30 cvt(result as i32)
31 }
32
33 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
34 let mut me = self;
35 (&mut me).read_to_end(buf)
36 }
37
38 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
39 let result = unsafe { abi::write(self.fd, buf.as_ptr(), buf.len()) };
40 cvt(result as i32)
41 }
42
43 pub fn duplicate(&self) -> io::Result<FileDesc> {
44 self.duplicate_path(&[])
45 }
46 pub fn duplicate_path(&self, _path: &[u8]) -> io::Result<FileDesc> {
47 Err(io::Error::new(ErrorKind::Other, "duplicate isn't supported"))
48 }
49
50 pub fn nonblocking(&self) -> io::Result<bool> {
51 Ok(false)
52 }
53
54 pub fn set_cloexec(&self) -> io::Result<()> {
55 Err(io::Error::new(ErrorKind::Other, "cloexec isn't supported"))
56 }
57
58 pub fn set_nonblocking(&self, _nonblocking: bool) -> io::Result<()> {
59 Err(io::Error::new(ErrorKind::Other, "nonblocking isn't supported"))
60 }
61 }
62
63 impl<'a> Read for &'a FileDesc {
64 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
65 (**self).read(buf)
66 }
67 }
68
69 impl AsInner<i32> for FileDesc {
70 fn as_inner(&self) -> &i32 { &self.fd }
71 }
72
73 impl Drop for FileDesc {
74 fn drop(&mut self) {
75 // Note that errors are ignored when closing a file descriptor. The
76 // reason for this is that if an error occurs we don't actually know if
77 // the file descriptor was closed or not, and if we retried (for
78 // something like EINTR), we might close another valid file descriptor
79 // (opened after we closed ours.
80 let _ = unsafe { abi::close(self.fd) };
81 }
82 }