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