forked from indygreg/PyOxidizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.rs
33 lines (26 loc) · 1019 Bytes
/
conversion.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Bridge Rust and Python string types.
use pyo3::{ffi as pyffi, prelude::*};
use std::ffi::OsString;
#[cfg(target_family = "unix")]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_family = "windows")]
use std::os::windows::prelude::OsStrExt;
#[cfg(unix)]
pub fn osstring_to_bytes(py: Python, s: OsString) -> &PyAny {
let b = s.as_bytes();
unsafe {
let o = pyffi::PyBytes_FromStringAndSize(b.as_ptr() as *const i8, b.len() as isize);
PyObject::from_owned_ptr(py, o).into_ref(py)
}
}
#[cfg(windows)]
pub fn osstring_to_bytes(py: Python, s: OsString) -> &PyAny {
let w: Vec<u16> = s.encode_wide().collect();
unsafe {
let o = pyffi::PyBytes_FromStringAndSize(w.as_ptr() as *const i8, w.len() as isize * 2);
PyObject::from_owned_ptr(py, o).into_ref(py)
}
}