diff --git a/src/conversion.rs b/src/conversion.rs index 5864277..eb622d5 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -6,7 +6,7 @@ use std::os::raw::c_int; use std::path::{Path, PathBuf}; use std::{mem, slice, str}; -use bstr::{BStr, BString, ByteSlice, ByteVec}; +use bstr::{BStr, BString, ByteVec}; use num_traits::cast; use crate::error::{Error, Result}; @@ -730,14 +730,17 @@ impl FromLua for OsString { } impl IntoLua for &OsStr { + #[cfg(unix)] #[inline] fn into_lua(self, lua: &Lua) -> Result { - let s = <[u8]>::from_os_str(self).ok_or_else(|| Error::ToLuaConversionError { - from: "OsStr".into(), - to: "string", - message: Some("invalid utf-8 encoding".into()), - })?; - Ok(Value::String(lua.create_string(s)?)) + use std::os::unix::ffi::OsStrExt; + Ok(Value::String(lua.create_string(self.as_bytes())?)) + } + + #[cfg(not(unix))] + #[inline] + fn into_lua(self, lua: &Lua) -> Result { + self.display().to_string().into_lua(lua) } } diff --git a/src/error.rs b/src/error.rs index 092fcb9..7d20a5a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -87,15 +87,6 @@ pub enum Error { /// Underlying error returned when converting argument to a Lua value. cause: Arc, }, - /// A Rust value could not be converted to a Lua value. - ToLuaConversionError { - /// Name of the Rust type that could not be converted. - from: String, - /// Name of the Lua type that could not be created. - to: &'static str, - /// A message indicating why the conversion failed in more detail. - message: Option, - }, /// A Lua value could not be converted to the expected Rust type. FromLuaConversionError { /// Name of the Lua type that could not be converted. @@ -249,13 +240,6 @@ impl fmt::Display for Error { } write!(fmt, ": {cause}") } - Error::ToLuaConversionError { from, to, message } => { - write!(fmt, "error converting {from} to Lua {to}")?; - match message { - None => Ok(()), - Some(message) => write!(fmt, " ({message})"), - } - } Error::FromLuaConversionError { from, to, message } => { write!(fmt, "error converting Lua {from} to {to}")?; match message {