Skip to content

Commit 4a61eba

Browse files
committed
rustpython_vm::windows
1 parent 58df09b commit 4a61eba

File tree

3 files changed

+72
-62
lines changed

3 files changed

+72
-62
lines changed

vm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub mod utils;
7979
pub mod version;
8080
pub mod vm;
8181
pub mod warn;
82+
#[cfg(windows)]
83+
pub mod windows;
8284

8385
pub use self::compiler::parser::source_code;
8486
pub use self::convert::{TryFromBorrowedObject, TryFromObject};

vm/src/stdlib/winapi.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ mod _winapi {
66
use crate::{
77
builtins::PyStrRef,
88
common::windows::ToWideString,
9-
convert::{ToPyException, ToPyObject, ToPyResult},
9+
convert::{ToPyException, ToPyResult},
1010
function::{ArgMapping, ArgSequence, OptionalArg},
1111
stdlib::os::errno_err,
12+
windows::WindowsSysResult,
1213
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
1314
};
1415
use std::ptr::{null, null_mut};
@@ -56,67 +57,6 @@ mod _winapi {
5657
UI::WindowsAndMessaging::SW_HIDE,
5758
};
5859

59-
trait WindowsSysResultValue {
60-
type Ok: ToPyObject;
61-
fn is_err(&self) -> bool;
62-
fn into_ok(self) -> Self::Ok;
63-
}
64-
65-
impl WindowsSysResultValue for RAW_HANDLE {
66-
type Ok = HANDLE;
67-
fn is_err(&self) -> bool {
68-
*self == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE
69-
}
70-
fn into_ok(self) -> Self::Ok {
71-
HANDLE(self)
72-
}
73-
}
74-
75-
impl WindowsSysResultValue for BOOL {
76-
type Ok = ();
77-
fn is_err(&self) -> bool {
78-
*self == 0
79-
}
80-
fn into_ok(self) -> Self::Ok {}
81-
}
82-
83-
struct WindowsSysResult<T>(T);
84-
85-
impl<T: WindowsSysResultValue> WindowsSysResult<T> {
86-
fn is_err(&self) -> bool {
87-
self.0.is_err()
88-
}
89-
fn into_pyresult(self, vm: &VirtualMachine) -> PyResult<T::Ok> {
90-
if self.is_err() {
91-
Err(errno_err(vm))
92-
} else {
93-
Ok(self.0.into_ok())
94-
}
95-
}
96-
}
97-
98-
impl<T: WindowsSysResultValue> ToPyResult for WindowsSysResult<T> {
99-
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
100-
let ok = self.into_pyresult(vm)?;
101-
Ok(ok.to_pyobject(vm))
102-
}
103-
}
104-
105-
type HandleInt = usize; // TODO: change to isize when fully ported to windows-rs
106-
107-
impl TryFromObject for HANDLE {
108-
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
109-
let handle = HandleInt::try_from_object(vm, obj)?;
110-
Ok(HANDLE(handle as isize))
111-
}
112-
}
113-
114-
impl ToPyObject for HANDLE {
115-
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
116-
(self.0 as HandleInt).to_pyobject(vm)
117-
}
118-
}
119-
12060
#[pyfunction]
12161
fn CloseHandle(handle: HANDLE) -> WindowsSysResult<BOOL> {
12262
WindowsSysResult(unsafe { windows_sys::Win32::Foundation::CloseHandle(handle.0) })

vm/src/windows.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::{
2+
convert::{ToPyObject, ToPyResult},
3+
stdlib::os::errno_err,
4+
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
5+
};
6+
use windows::Win32::Foundation::HANDLE;
7+
use windows_sys::Win32::Foundation::{BOOL, HANDLE as RAW_HANDLE};
8+
9+
pub(crate) trait WindowsSysResultValue {
10+
type Ok: ToPyObject;
11+
fn is_err(&self) -> bool;
12+
fn into_ok(self) -> Self::Ok;
13+
}
14+
15+
impl WindowsSysResultValue for RAW_HANDLE {
16+
type Ok = HANDLE;
17+
fn is_err(&self) -> bool {
18+
*self == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE
19+
}
20+
fn into_ok(self) -> Self::Ok {
21+
HANDLE(self)
22+
}
23+
}
24+
25+
impl WindowsSysResultValue for BOOL {
26+
type Ok = ();
27+
fn is_err(&self) -> bool {
28+
*self == 0
29+
}
30+
fn into_ok(self) -> Self::Ok {}
31+
}
32+
33+
pub(crate) struct WindowsSysResult<T>(pub T);
34+
35+
impl<T: WindowsSysResultValue> WindowsSysResult<T> {
36+
pub fn is_err(&self) -> bool {
37+
self.0.is_err()
38+
}
39+
pub fn into_pyresult(self, vm: &VirtualMachine) -> PyResult<T::Ok> {
40+
if self.is_err() {
41+
Err(errno_err(vm))
42+
} else {
43+
Ok(self.0.into_ok())
44+
}
45+
}
46+
}
47+
48+
impl<T: WindowsSysResultValue> ToPyResult for WindowsSysResult<T> {
49+
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
50+
let ok = self.into_pyresult(vm)?;
51+
Ok(ok.to_pyobject(vm))
52+
}
53+
}
54+
55+
type HandleInt = usize; // TODO: change to isize when fully ported to windows-rs
56+
57+
impl TryFromObject for HANDLE {
58+
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
59+
let handle = HandleInt::try_from_object(vm, obj)?;
60+
Ok(HANDLE(handle as isize))
61+
}
62+
}
63+
64+
impl ToPyObject for HANDLE {
65+
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
66+
(self.0 as HandleInt).to_pyobject(vm)
67+
}
68+
}

0 commit comments

Comments
 (0)