Visitar URL original
Fix host_env CI target builds · RustPython/RustPython@9e07122 · GitHub
Skip to content

Commit 9e07122

Browse files
committed
Fix host_env CI target builds
1 parent c2f7f66 commit 9e07122

12 files changed

Lines changed: 636 additions & 80 deletions

File tree

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
use alloc::fmt;
2+
use core::marker::PhantomData;
3+
use std::{ffi, io};
4+
5+
pub type Offset = i64;
6+
pub type Raw = i32;
7+
8+
const EBADF: i32 = 9;
9+
10+
#[repr(transparent)]
11+
pub struct Owned {
12+
fd: Raw,
13+
}
14+
15+
impl fmt::Debug for Owned {
16+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17+
f.debug_tuple("crt_fd::Owned")
18+
.field(&self.as_raw())
19+
.finish()
20+
}
21+
}
22+
23+
#[derive(Copy, Clone)]
24+
#[repr(transparent)]
25+
pub struct Borrowed<'fd> {
26+
fd: Raw,
27+
_marker: PhantomData<&'fd Owned>,
28+
}
29+
30+
impl PartialEq for Borrowed<'_> {
31+
fn eq(&self, other: &Self) -> bool {
32+
self.as_raw() == other.as_raw()
33+
}
34+
}
35+
36+
impl Eq for Borrowed<'_> {}
37+
38+
impl fmt::Debug for Borrowed<'_> {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
f.debug_tuple("crt_fd::Borrowed")
41+
.field(&self.as_raw())
42+
.finish()
43+
}
44+
}
45+
46+
impl Owned {
47+
/// Create a `crt_fd::Owned` from a raw file descriptor.
48+
///
49+
/// # Safety
50+
///
51+
/// `fd` must be a valid file descriptor for the embedding host.
52+
#[inline]
53+
pub const unsafe fn from_raw(fd: Raw) -> Self {
54+
Self { fd }
55+
}
56+
57+
/// Create a `crt_fd::Owned` from a raw file descriptor.
58+
///
59+
/// Returns an error if `fd` is -1.
60+
///
61+
/// # Safety
62+
///
63+
/// `fd` must be a valid file descriptor for the embedding host.
64+
#[inline]
65+
pub unsafe fn try_from_raw(fd: Raw) -> io::Result<Self> {
66+
if fd == -1 {
67+
Err(ebadf())
68+
} else {
69+
Ok(unsafe { Self::from_raw(fd) })
70+
}
71+
}
72+
73+
#[inline]
74+
pub const fn borrow(&self) -> Borrowed<'_> {
75+
unsafe { Borrowed::borrow_raw(self.as_raw()) }
76+
}
77+
78+
#[inline]
79+
pub const fn as_raw(&self) -> Raw {
80+
self.fd
81+
}
82+
83+
#[inline]
84+
pub fn into_raw(self) -> Raw {
85+
let fd = self.fd;
86+
core::mem::forget(self);
87+
fd
88+
}
89+
90+
pub fn leak<'fd>(self) -> Borrowed<'fd> {
91+
unsafe { Borrowed::borrow_raw(self.into_raw()) }
92+
}
93+
}
94+
95+
impl Drop for Owned {
96+
fn drop(&mut self) {}
97+
}
98+
99+
impl<'fd> Borrowed<'fd> {
100+
/// Create a `crt_fd::Borrowed` from a raw file descriptor.
101+
///
102+
/// # Safety
103+
///
104+
/// `fd` must be a valid file descriptor for the embedding host.
105+
#[inline]
106+
pub const unsafe fn borrow_raw(fd: Raw) -> Self {
107+
Self {
108+
fd,
109+
_marker: PhantomData,
110+
}
111+
}
112+
113+
/// Create a `crt_fd::Borrowed` from a raw file descriptor.
114+
///
115+
/// Returns an error if `fd` is -1.
116+
///
117+
/// # Safety
118+
///
119+
/// `fd` must be a valid file descriptor for the embedding host.
120+
#[inline]
121+
pub unsafe fn try_borrow_raw(fd: Raw) -> io::Result<Self> {
122+
if fd == -1 {
123+
Err(ebadf())
124+
} else {
125+
Ok(unsafe { Self::borrow_raw(fd) })
126+
}
127+
}
128+
129+
#[inline]
130+
pub const fn as_raw(self) -> Raw {
131+
self.fd
132+
}
133+
}
134+
135+
#[inline]
136+
fn ebadf() -> io::Error {
137+
io::Error::from_raw_os_error(EBADF)
138+
}
139+
140+
pub fn open(_path: &ffi::CStr, _flags: i32, _mode: i32) -> io::Result<Owned> {
141+
Err(unsupported())
142+
}
143+
144+
pub fn openat(_dir: Borrowed<'_>, _path: &ffi::CStr, _flags: i32, _mode: i32) -> io::Result<Owned> {
145+
Err(unsupported())
146+
}
147+
148+
pub fn fsync(_fd: Borrowed<'_>) -> io::Result<()> {
149+
Err(ebadf())
150+
}
151+
152+
pub fn close(_fd: Owned) -> io::Result<()> {
153+
Err(ebadf())
154+
}
155+
156+
pub fn ftruncate(_fd: Borrowed<'_>, _len: Offset) -> io::Result<()> {
157+
Err(ebadf())
158+
}
159+
160+
pub fn write(_fd: Borrowed<'_>, _buf: &[u8]) -> io::Result<usize> {
161+
Err(ebadf())
162+
}
163+
164+
pub fn read(_fd: Borrowed<'_>, _buf: &mut [u8]) -> io::Result<usize> {
165+
Err(ebadf())
166+
}
167+
168+
fn unsupported() -> io::Error {
169+
io::Error::new(
170+
io::ErrorKind::Unsupported,
171+
"host file descriptors are unsupported on this platform",
172+
)
173+
}

0 commit comments

Comments
 (0)