Initial implementation of owned Lua types

This commit is contained in:
Alex Orlenko
2022-10-16 23:36:26 +01:00
parent 02c08c6350
commit bf79d9e75d
10 changed files with 267 additions and 39 deletions
+18 -1
View File
@@ -12,7 +12,7 @@ use {
use crate::error::{Error, Result};
use crate::ffi;
use crate::types::LuaRef;
use crate::types::{LuaOwnedRef, LuaRef};
/// Handle to an internal Lua string.
///
@@ -20,6 +20,17 @@ use crate::types::LuaRef;
#[derive(Clone, Debug)]
pub struct String<'lua>(pub(crate) LuaRef<'lua>);
/// Owned handle to an internal Lua string.
#[derive(Clone, Debug)]
pub struct OwnedString(pub(crate) LuaOwnedRef);
impl OwnedString {
/// Get borrowed handle to the underlying Lua string.
pub const fn to_ref(&self) -> String {
String(self.0.to_ref())
}
}
impl<'lua> String<'lua> {
/// Get a `&str` slice if the Lua string is valid UTF-8.
///
@@ -122,6 +133,12 @@ impl<'lua> String<'lua> {
let ref_thread = self.0.lua.ref_thread();
unsafe { ffi::lua_topointer(ref_thread, self.0.index) }
}
/// Convert this handle to owned version.
#[inline]
pub fn into_owned(self) -> OwnedString {
OwnedString(self.0.into_owned())
}
}
impl<'lua> AsRef<[u8]> for String<'lua> {