]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/sgx/fd.rs
Rollup merge of #105482 - wesleywiser:fix_debuginfo_ub, r=tmiasko
[rust.git] / library / std / src / sys / sgx / fd.rs
1 use fortanix_sgx_abi::Fd;
2
3 use super::abi::usercalls;
4 use crate::io::{self, IoSlice, IoSliceMut};
5 use crate::mem;
6 use crate::sys::{AsInner, FromInner, IntoInner};
7
8 #[derive(Debug)]
9 pub struct FileDesc {
10     fd: Fd,
11 }
12
13 impl FileDesc {
14     pub fn new(fd: Fd) -> FileDesc {
15         FileDesc { fd: fd }
16     }
17
18     pub fn raw(&self) -> Fd {
19         self.fd
20     }
21
22     /// Extracts the actual file descriptor without closing it.
23     pub fn into_raw(self) -> Fd {
24         let fd = self.fd;
25         mem::forget(self);
26         fd
27     }
28
29     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
30         usercalls::read(self.fd, &mut [IoSliceMut::new(buf)])
31     }
32
33     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
34         usercalls::read(self.fd, bufs)
35     }
36
37     #[inline]
38     pub fn is_read_vectored(&self) -> bool {
39         true
40     }
41
42     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
43         usercalls::write(self.fd, &[IoSlice::new(buf)])
44     }
45
46     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
47         usercalls::write(self.fd, bufs)
48     }
49
50     #[inline]
51     pub fn is_write_vectored(&self) -> bool {
52         true
53     }
54
55     pub fn flush(&self) -> io::Result<()> {
56         usercalls::flush(self.fd)
57     }
58 }
59
60 impl AsInner<Fd> for FileDesc {
61     fn as_inner(&self) -> &Fd {
62         &self.fd
63     }
64 }
65
66 impl IntoInner<Fd> for FileDesc {
67     fn into_inner(self) -> Fd {
68         let fd = self.fd;
69         mem::forget(self);
70         fd
71     }
72 }
73
74 impl FromInner<Fd> for FileDesc {
75     fn from_inner(fd: Fd) -> FileDesc {
76         FileDesc { fd }
77     }
78 }
79
80 impl Drop for FileDesc {
81     fn drop(&mut self) {
82         usercalls::close(self.fd)
83     }
84 }