Move common functionality into hostcalls mod

This commit is contained in:
Jakub Konka
2019-06-07 18:23:46 +02:00
committed by Dan Gohman
parent c113ff32e6
commit 54a897cf91
19 changed files with 2155 additions and 2004 deletions

View File

@@ -4,7 +4,9 @@
#![allow(unused)]
use crate::host;
use std::ffi::{OsStr, OsString};
use std::marker::PhantomData;
use std::os::windows::prelude::{OsStrExt, OsStringExt};
use std::slice;
use winapi::shared::{ntdef, ws2def};
@@ -82,3 +84,17 @@ pub unsafe fn iovec_to_win_mut<'a>(iovec: &'a mut host::__wasi_iovec_t) -> IoVec
let slice = slice::from_raw_parts_mut(iovec.buf as *mut u8, iovec.buf_len);
IoVecMut::new(slice)
}
pub fn path_from_raw(raw_path: &[u8]) -> OsString {
OsString::from_wide(&raw_path.iter().map(|&x| x as u16).collect::<Vec<u16>>())
}
pub fn path_to_raw<P: AsRef<OsStr>>(path: P) -> Vec<u8> {
path.as_ref()
.encode_wide()
.map(u16::to_le_bytes)
.fold(Vec::new(), |mut acc, bytes| {
acc.extend_from_slice(&bytes);
acc
})
}