Skip to content

Commit 7b959df

Browse files
committed
relocate conversion traits and fix build
1 parent 5d7dcce commit 7b959df

3 files changed

Lines changed: 39 additions & 32 deletions

File tree

common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub mod rc;
2222
pub mod refcount;
2323
pub mod static_cell;
2424
pub mod str;
25+
#[cfg(windows)]
26+
pub mod windows;
2527

2628
pub mod vendored {
2729
pub use ascii;

common/src/windows.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::{
2+
ffi::{OsStr, OsString},
3+
os::windows::ffi::{OsStrExt, OsStringExt},
4+
};
5+
6+
pub trait ToWideString {
7+
fn to_wide(&self) -> Vec<u16>;
8+
fn to_wides_with_nul(&self) -> Vec<u16>;
9+
}
10+
impl<T> ToWideString for T
11+
where
12+
T: AsRef<OsStr>,
13+
{
14+
fn to_wide(&self) -> Vec<u16> {
15+
self.as_ref().encode_wide().collect()
16+
}
17+
fn to_wides_with_nul(&self) -> Vec<u16> {
18+
self.as_ref().encode_wide().chain(Some(0)).collect()
19+
}
20+
}
21+
22+
pub trait FromWideString
23+
where
24+
Self: Sized,
25+
{
26+
fn from_wides_until_nul(wide: &[u16]) -> Self;
27+
}
28+
impl FromWideString for OsString {
29+
fn from_wides_until_nul(wide: &[u16]) -> OsString {
30+
let len = wide.iter().take_while(|&&c| c != 0).count();
31+
OsString::from_wide(&wide[..len])
32+
}
33+
}

vm/src/stdlib/winapi.rs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ pub(crate) use _winapi::make_module;
55
mod _winapi {
66
use crate::{
77
builtins::PyStrRef,
8+
common::windows::ToWideString,
89
convert::ToPyException,
910
function::{ArgMapping, ArgSequence, OptionalArg},
1011
stdlib::os::errno_err,
1112
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
1213
};
13-
use std::ffi::{OsStr, OsString};
14-
use std::os::windows::prelude::*;
1514
use std::ptr::{null, null_mut};
1615
use winapi::shared::winerror;
1716
use winapi::um::{
@@ -410,37 +409,10 @@ mod _winapi {
410409
.map(drop)
411410
}
412411

413-
pub trait ToWideString {
414-
fn to_wide(&self) -> Vec<u16>;
415-
fn to_wides_with_nul(&self) -> Vec<u16>;
416-
}
417-
impl<T> ToWide for T
418-
where
419-
T: AsRef<OsStr>,
420-
{
421-
fn to_wide(&self) -> Vec<u16> {
422-
self.as_ref().encode_wide().collect()
423-
}
424-
fn to_wide_null(&self) -> Vec<u16> {
425-
self.as_ref().encode_wide().chain(Some(0)).collect()
426-
}
427-
}
428-
pub trait FromWide
429-
where
430-
Self: Sized,
431-
{
432-
fn from_wides_until_nul(wide: &[u16]) -> Self;
433-
}
434-
impl FromWide for OsString {
435-
fn from_wide_null(wide: &[u16]) -> OsString {
436-
let len = wide.iter().take_while(|&&c| c != 0).count();
437-
OsString::from_wide(&wide[..len])
438-
}
439-
}
440-
441-
#[pyfunction]
412+
// TODO: ctypes.LibraryLoader.LoadLibrary
413+
#[allow(dead_code)]
442414
fn LoadLibrary(path: PyStrRef, vm: &VirtualMachine) -> PyResult<isize> {
443-
let path = path.as_str().to_wide_null();
415+
let path = path.as_str().to_wides_with_nul();
444416
let handle = unsafe { LoadLibraryW(PCWSTR::from_raw(path.as_ptr())).unwrap() };
445417
if handle.is_invalid() {
446418
return Err(vm.new_runtime_error("LoadLibrary failed".to_owned()));

0 commit comments

Comments
 (0)