forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.rs
More file actions
161 lines (125 loc) · 3.56 KB
/
Copy pathutil.rs
File metadata and controls
161 lines (125 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::PyObject;
use core::convert::Infallible;
use core::ffi::{c_char, c_double, c_int, c_long, c_ulonglong, c_void};
use rustpython_vm::{PyObjectRef, PyRef, PyResult, VirtualMachine};
pub(crate) trait FfiResult<Output = Self> {
const ERR_VALUE: Output;
fn into_output(self, vm: &VirtualMachine) -> Output;
}
impl FfiResult for () {
const ERR_VALUE: () = ();
fn into_output(self, _vm: &VirtualMachine) {
self
}
}
impl FfiResult<c_int> for () {
const ERR_VALUE: c_int = -1;
fn into_output(self, _vm: &VirtualMachine) -> c_int {
0
}
}
impl<T> FfiResult<*mut PyObject> for PyRef<T>
where
Self: Into<PyObjectRef>,
{
const ERR_VALUE: *mut PyObject = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut PyObject {
self.into().into_raw().as_ptr()
}
}
impl FfiResult<*mut PyObject> for PyObjectRef {
const ERR_VALUE: *mut PyObject = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut PyObject {
self.into_raw().as_ptr()
}
}
impl FfiResult for *mut PyObject {
const ERR_VALUE: *mut PyObject = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut PyObject {
self
}
}
impl FfiResult<*mut PyObject> for *const PyObject {
const ERR_VALUE: *mut PyObject = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut PyObject {
self.cast_mut()
}
}
impl FfiResult for *mut c_void {
const ERR_VALUE: *mut c_void = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut c_void {
self
}
}
impl FfiResult<*mut c_char> for *const u8 {
const ERR_VALUE: *mut c_char = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut c_char {
self.cast_mut().cast()
}
}
impl FfiResult<*mut c_char> for *mut u8 {
const ERR_VALUE: *mut c_char = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *mut c_char {
self.cast()
}
}
impl FfiResult for *const c_char {
const ERR_VALUE: *const c_char = core::ptr::null_mut();
fn into_output(self, _vm: &VirtualMachine) -> *const c_char {
self
}
}
impl FfiResult<isize> for usize {
const ERR_VALUE: isize = -1;
fn into_output(self, _vm: &VirtualMachine) -> isize {
self.try_into()
.expect("Output value is too large to fit into target type")
}
}
impl FfiResult for c_long {
const ERR_VALUE: Self = -1;
fn into_output(self, _vm: &VirtualMachine) -> Self {
self
}
}
impl FfiResult for c_ulonglong {
const ERR_VALUE: Self = Self::MAX;
fn into_output(self, _vm: &VirtualMachine) -> Self {
self
}
}
impl FfiResult for c_double {
const ERR_VALUE: Self = -1.0;
fn into_output(self, _vm: &VirtualMachine) -> Self {
self
}
}
impl FfiResult<c_int> for bool {
const ERR_VALUE: c_int = -1;
fn into_output(self, _vm: &VirtualMachine) -> c_int {
self as c_int
}
}
impl FfiResult<()> for PyResult<Infallible> {
const ERR_VALUE: () = ();
fn into_output(self, vm: &VirtualMachine) {
match self {
Err(err) => vm.set_exception(Some(err)),
}
}
}
impl<Output, T> FfiResult<Output> for PyResult<T>
where
T: FfiResult<Output>,
{
const ERR_VALUE: Output = T::ERR_VALUE;
fn into_output(self, vm: &VirtualMachine) -> Output {
self.map_or_else(
|err| {
vm.set_exception(Some(err));
T::ERR_VALUE
},
|obj| obj.into_output(vm),
)
}
}