Add `LuaLossyString`

This commit is contained in:
wtfsck 2022-05-18 21:06:26 +02:00
parent 7ceb290138
commit 598e106c83
1 changed files with 15 additions and 0 deletions

View File

@ -5,6 +5,7 @@ use crate::lua::Lua;
use crate::lua_api::lua_CFunction;
use crate::prelude::LuaUserData;
use libc::c_int;
use std::borrow::Cow;
pub trait FromLua<'lua> {
type RetType;
@ -51,6 +52,20 @@ impl<'lua> FromLua<'lua> for LuaAny {
}
}
/// A Lua string converted to UTF-8 (lossy conversion if it's not utf-8)
#[derive(Debug, Clone)]
pub struct LuaLossyString<'a>(pub Cow<'a, str>);
impl<'a, 'lua: 'a> FromLua<'lua> for LuaLossyString<'a> {
type RetType = Self;
#[inline]
unsafe fn from_lua(lua: &Lua<'lua>, idx: c_int) -> Self::RetType {
let bytes = unsafe { lua.get_byte_slice(idx) };
Self(String::from_utf8_lossy(bytes))
}
}
macro_rules! create_lua_option {
($lua:ident, $idx:ident { $($struct_name:ident : $ty:ty => $expr:expr,)+ }) => {
$(