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