WASI paths as &str and String (#37)

* Check if RawString operates on valid encodings

* Use &str and String for WASI paths
This commit is contained in:
Jakub Konka
2019-07-19 20:09:27 +02:00
committed by Dan Gohman
parent c3994bf57b
commit 08aa61f066
8 changed files with 207 additions and 294 deletions

View File

@@ -4,7 +4,7 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::{io, slice};
use std::{io, slice, str};
pub type void = ::std::os::raw::c_void;
@@ -495,6 +495,22 @@ pub unsafe fn iovec_to_host_mut<'a>(iovec: &'a mut __wasi_iovec_t) -> io::IoSlic
io::IoSliceMut::new(slice)
}
/// Creates not-owned WASI path from byte slice.
///
/// NB WASI spec requires bytes to be valid UTF-8. Otherwise,
/// `__WASI_EILSEQ` error is returned.
pub fn path_from_slice<'a>(s: &'a [u8]) -> Result<&'a str, __wasi_errno_t> {
str::from_utf8(s).map_err(|_| __WASI_EILSEQ)
}
/// Creates owned WASI path from byte vector.
///
/// NB WASI spec requires bytes to be valid UTF-8. Otherwise,
/// `__WASI_EILSEQ` error is returned.
pub fn path_from_vec<S: Into<Vec<u8>>>(s: S) -> Result<String, __wasi_errno_t> {
String::from_utf8(s.into()).map_err(|_| __WASI_EILSEQ)
}
#[cfg(test)]
mod tests {
use super::*;