mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Initial implementation of owned Lua types
This commit is contained in:
+18
-1
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user