Add "unstable" feature flag.
Hide owned types under the new feature flag. Drop OwnedString/OwnedThread types (unlikely they are useful).
This commit is contained in:
parent
56abc4a700
commit
c60f633a62
|
@ -39,6 +39,7 @@ async = ["futures-core", "futures-task", "futures-util"]
|
|||
send = []
|
||||
serialize = ["serde", "erased-serde"]
|
||||
macros = ["mlua_derive/macros"]
|
||||
unstable = []
|
||||
|
||||
[dependencies]
|
||||
mlua_derive = { version = "=0.8.0", optional = true, path = "mlua_derive" }
|
||||
|
@ -90,7 +91,7 @@ required-features = ["async", "serialize", "macros"]
|
|||
|
||||
[[example]]
|
||||
name = "async_http_server"
|
||||
required-features = ["async", "macros"]
|
||||
required-features = ["async", "macros", "unstable"]
|
||||
|
||||
[[example]]
|
||||
name = "async_tcp_server"
|
||||
|
|
|
@ -11,15 +11,18 @@ use bstr::{BStr, BString};
|
|||
use num_traits::cast;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::function::{Function, OwnedFunction};
|
||||
use crate::function::Function;
|
||||
use crate::lua::Lua;
|
||||
use crate::string::{OwnedString, String};
|
||||
use crate::table::{OwnedTable, Table};
|
||||
use crate::thread::{OwnedThread, Thread};
|
||||
use crate::string::String;
|
||||
use crate::table::Table;
|
||||
use crate::thread::Thread;
|
||||
use crate::types::{LightUserData, MaybeSend};
|
||||
use crate::userdata::{AnyUserData, OwnedAnyUserData, UserData};
|
||||
use crate::userdata::{AnyUserData, UserData};
|
||||
use crate::value::{FromLua, Nil, ToLua, Value};
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
use crate::{function::OwnedFunction, table::OwnedTable, userdata::OwnedAnyUserData};
|
||||
|
||||
impl<'lua> ToLua<'lua> for Value<'lua> {
|
||||
#[inline]
|
||||
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
|
||||
|
@ -54,20 +57,6 @@ impl<'lua> FromLua<'lua> for String<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'lua> ToLua<'lua> for OwnedString {
|
||||
#[inline]
|
||||
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
|
||||
Ok(Value::String(String(lua.adopt_owned_ref(self.0))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> FromLua<'lua> for OwnedString {
|
||||
#[inline]
|
||||
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedString> {
|
||||
String::from_lua(value, lua).map(|s| s.into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> ToLua<'lua> for Table<'lua> {
|
||||
#[inline]
|
||||
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
|
||||
|
@ -89,6 +78,7 @@ impl<'lua> FromLua<'lua> for Table<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<'lua> ToLua<'lua> for OwnedTable {
|
||||
#[inline]
|
||||
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
|
||||
|
@ -96,6 +86,7 @@ impl<'lua> ToLua<'lua> for OwnedTable {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<'lua> FromLua<'lua> for OwnedTable {
|
||||
#[inline]
|
||||
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedTable> {
|
||||
|
@ -124,6 +115,7 @@ impl<'lua> FromLua<'lua> for Function<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<'lua> ToLua<'lua> for OwnedFunction {
|
||||
#[inline]
|
||||
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
|
||||
|
@ -131,6 +123,7 @@ impl<'lua> ToLua<'lua> for OwnedFunction {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<'lua> FromLua<'lua> for OwnedFunction {
|
||||
#[inline]
|
||||
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedFunction> {
|
||||
|
@ -159,20 +152,6 @@ impl<'lua> FromLua<'lua> for Thread<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'lua> ToLua<'lua> for OwnedThread {
|
||||
#[inline]
|
||||
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
|
||||
Ok(Value::Thread(Thread(lua.adopt_owned_ref(self.0))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> FromLua<'lua> for OwnedThread {
|
||||
#[inline]
|
||||
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedThread> {
|
||||
Thread::from_lua(value, lua).map(|s| s.into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> ToLua<'lua> for AnyUserData<'lua> {
|
||||
#[inline]
|
||||
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
|
||||
|
@ -194,6 +173,7 @@ impl<'lua> FromLua<'lua> for AnyUserData<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<'lua> ToLua<'lua> for OwnedAnyUserData {
|
||||
#[inline]
|
||||
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
|
||||
|
@ -201,6 +181,7 @@ impl<'lua> ToLua<'lua> for OwnedAnyUserData {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<'lua> FromLua<'lua> for OwnedAnyUserData {
|
||||
#[inline]
|
||||
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedAnyUserData> {
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::slice;
|
|||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::ffi;
|
||||
use crate::types::{LuaOwnedRef, LuaRef};
|
||||
use crate::types::LuaRef;
|
||||
use crate::util::{
|
||||
assert_stack, check_stack, error_traceback, pop_error, ptr_to_cstr_bytes, StackGuard,
|
||||
};
|
||||
|
@ -19,9 +19,12 @@ use {futures_core::future::LocalBoxFuture, futures_util::future};
|
|||
pub struct Function<'lua>(pub(crate) LuaRef<'lua>);
|
||||
|
||||
/// Owned handle to an internal Lua function.
|
||||
#[cfg(feature = "unstable")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OwnedFunction(pub(crate) LuaOwnedRef);
|
||||
pub struct OwnedFunction(pub(crate) crate::types::LuaOwnedRef);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl OwnedFunction {
|
||||
/// Get borrowed handle to the underlying Lua function.
|
||||
pub const fn to_ref(&self) -> Function {
|
||||
|
@ -391,6 +394,8 @@ impl<'lua> Function<'lua> {
|
|||
}
|
||||
|
||||
/// Convert this handle to owned version.
|
||||
#[cfg(feature = "unstable")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||
#[inline]
|
||||
pub fn into_owned(self) -> OwnedFunction {
|
||||
OwnedFunction(self.0.into_owned())
|
||||
|
@ -408,4 +413,7 @@ mod assertions {
|
|||
use super::*;
|
||||
|
||||
static_assertions::assert_not_impl_any!(Function: Send);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
static_assertions::assert_not_impl_any!(OwnedFunction: Send);
|
||||
}
|
||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -108,15 +108,15 @@ pub use crate::{ffi::lua_CFunction, ffi::lua_State};
|
|||
|
||||
pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
|
||||
pub use crate::error::{Error, ExternalError, ExternalResult, Result};
|
||||
pub use crate::function::{Function, FunctionInfo, OwnedFunction};
|
||||
pub use crate::function::{Function, FunctionInfo};
|
||||
pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
|
||||
pub use crate::lua::{GCMode, Lua, LuaOptions};
|
||||
pub use crate::multi::Variadic;
|
||||
pub use crate::scope::Scope;
|
||||
pub use crate::stdlib::StdLib;
|
||||
pub use crate::string::{OwnedString, String};
|
||||
pub use crate::table::{OwnedTable, Table, TableExt, TablePairs, TableSequence};
|
||||
pub use crate::thread::{OwnedThread, Thread, ThreadStatus};
|
||||
pub use crate::string::String;
|
||||
pub use crate::table::{Table, TableExt, TablePairs, TableSequence};
|
||||
pub use crate::thread::{Thread, ThreadStatus};
|
||||
pub use crate::types::{Integer, LightUserData, Number, RegistryKey};
|
||||
pub use crate::userdata::{
|
||||
AnyUserData, MetaMethod, UserData, UserDataFields, UserDataMetatable, UserDataMethods,
|
||||
|
@ -148,6 +148,10 @@ pub mod serde;
|
|||
#[macro_use]
|
||||
extern crate mlua_derive;
|
||||
|
||||
// Unstable features
|
||||
#[cfg(all(feature = "unstable", not(feature = "send")))]
|
||||
pub use crate::{function::OwnedFunction, table::OwnedTable};
|
||||
|
||||
/// Create a type that implements [`AsChunk`] and can capture Rust variables.
|
||||
///
|
||||
/// This macro allows to write Lua code directly in Rust code.
|
||||
|
|
17
src/lua.rs
17
src/lua.rs
|
@ -26,8 +26,8 @@ use crate::string::String;
|
|||
use crate::table::Table;
|
||||
use crate::thread::Thread;
|
||||
use crate::types::{
|
||||
Callback, CallbackUpvalue, DestructedUserdata, Integer, LightUserData, LuaOwnedRef, LuaRef,
|
||||
MaybeSend, Number, RegistryKey,
|
||||
Callback, CallbackUpvalue, DestructedUserdata, Integer, LightUserData, LuaRef, MaybeSend,
|
||||
Number, RegistryKey,
|
||||
};
|
||||
use crate::userdata::{AnyUserData, UserData, UserDataCell};
|
||||
use crate::userdata_impl::{StaticUserDataFields, StaticUserDataMethods, UserDataProxy};
|
||||
|
@ -2469,17 +2469,16 @@ impl Lua {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn make_owned_ref(&self, lref: LuaRef) -> LuaOwnedRef {
|
||||
assert!(lref.drop, "Cannot make owned non-drop reference");
|
||||
let owned_ref = LuaOwnedRef {
|
||||
lua: Lua(self.0.clone()),
|
||||
index: lref.index,
|
||||
};
|
||||
#[cfg(feature = "unstable")]
|
||||
pub(crate) fn make_owned_ref(&self, lref: LuaRef) -> crate::types::LuaOwnedRef {
|
||||
assert!(lref.drop, "Cannot turn non-drop reference into owned");
|
||||
let owned_ref = crate::types::LuaOwnedRef::new(Lua(self.0.clone()), lref.index);
|
||||
mem::forget(lref);
|
||||
owned_ref
|
||||
}
|
||||
|
||||
pub(crate) fn adopt_owned_ref(&self, loref: LuaOwnedRef) -> LuaRef {
|
||||
#[cfg(feature = "unstable")]
|
||||
pub(crate) fn adopt_owned_ref(&self, loref: crate::types::LuaOwnedRef) -> LuaRef {
|
||||
assert!(
|
||||
Arc::ptr_eq(&loref.lua.0, &self.0),
|
||||
"Lua instance passed Value created from a different main Lua state"
|
||||
|
|
|
@ -12,7 +12,7 @@ use {
|
|||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::ffi;
|
||||
use crate::types::{LuaOwnedRef, LuaRef};
|
||||
use crate::types::LuaRef;
|
||||
|
||||
/// Handle to an internal Lua string.
|
||||
///
|
||||
|
@ -20,17 +20,6 @@ use crate::types::{LuaOwnedRef, 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.
|
||||
///
|
||||
|
@ -133,12 +122,6 @@ 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> {
|
||||
|
|
12
src/table.rs
12
src/table.rs
|
@ -11,7 +11,7 @@ use {
|
|||
use crate::error::{Error, Result};
|
||||
use crate::ffi;
|
||||
use crate::function::Function;
|
||||
use crate::types::{Integer, LuaOwnedRef, LuaRef};
|
||||
use crate::types::{Integer, LuaRef};
|
||||
use crate::util::{assert_stack, check_stack, StackGuard};
|
||||
use crate::value::{FromLua, FromLuaMulti, Nil, ToLua, ToLuaMulti, Value};
|
||||
|
||||
|
@ -23,9 +23,12 @@ use {futures_core::future::LocalBoxFuture, futures_util::future};
|
|||
pub struct Table<'lua>(pub(crate) LuaRef<'lua>);
|
||||
|
||||
/// Owned handle to an internal Lua table.
|
||||
#[cfg(feature = "unstable")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OwnedTable(pub(crate) LuaOwnedRef);
|
||||
pub struct OwnedTable(pub(crate) crate::types::LuaOwnedRef);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl OwnedTable {
|
||||
/// Get borrowed handle to the underlying Lua table.
|
||||
pub const fn to_ref(&self) -> Table {
|
||||
|
@ -538,6 +541,8 @@ impl<'lua> Table<'lua> {
|
|||
}
|
||||
|
||||
/// Convert this handle to owned version.
|
||||
#[cfg(feature = "unstable")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||
#[inline]
|
||||
pub fn into_owned(self) -> OwnedTable {
|
||||
OwnedTable(self.0.into_owned())
|
||||
|
@ -1024,4 +1029,7 @@ mod assertions {
|
|||
use super::*;
|
||||
|
||||
static_assertions::assert_not_impl_any!(Table: Send);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
static_assertions::assert_not_impl_any!(OwnedTable: Send);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::os::raw::c_int;
|
|||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::ffi;
|
||||
use crate::types::{LuaOwnedRef, LuaRef};
|
||||
use crate::types::LuaRef;
|
||||
use crate::util::{check_stack, error_traceback_thread, pop_error, StackGuard};
|
||||
use crate::value::{FromLuaMulti, ToLuaMulti};
|
||||
|
||||
|
@ -47,17 +47,6 @@ pub enum ThreadStatus {
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct Thread<'lua>(pub(crate) LuaRef<'lua>);
|
||||
|
||||
/// Owned handle to an internal Lua thread.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OwnedThread(pub(crate) LuaOwnedRef);
|
||||
|
||||
impl OwnedThread {
|
||||
/// Get borrowed handle to the underlying Lua thread.
|
||||
pub const fn to_ref(&self) -> Thread {
|
||||
Thread(self.0.to_ref())
|
||||
}
|
||||
}
|
||||
|
||||
/// Thread (coroutine) representation as an async [`Future`] or [`Stream`].
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
|
@ -346,12 +335,6 @@ impl<'lua> Thread<'lua> {
|
|||
protect_lua!(state, 0, 0, |_| ffi::luaL_sandboxthread(thread))
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this handle to owned version.
|
||||
#[inline]
|
||||
pub fn into_owned(self) -> OwnedThread {
|
||||
OwnedThread(self.0.into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> PartialEq for Thread<'lua> {
|
||||
|
|
26
src/types.rs
26
src/types.rs
|
@ -192,6 +192,7 @@ impl<'lua> LuaRef<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
#[inline]
|
||||
pub(crate) fn into_owned(self) -> LuaOwnedRef {
|
||||
self.lua.make_owned_ref(self)
|
||||
|
@ -232,23 +233,28 @@ impl<'lua> PartialEq for LuaRef<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
pub(crate) struct LuaOwnedRef {
|
||||
pub(crate) lua: Lua,
|
||||
pub(crate) index: c_int,
|
||||
_non_send: std::marker::PhantomData<*const ()>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl fmt::Debug for LuaOwnedRef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "OwnedRef({})", self.index)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl Clone for LuaOwnedRef {
|
||||
fn clone(&self) -> Self {
|
||||
self.lua.make_owned_ref(self.to_ref().clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl Drop for LuaOwnedRef {
|
||||
fn drop(&mut self) {
|
||||
drop(LuaRef {
|
||||
|
@ -259,7 +265,24 @@ impl Drop for LuaOwnedRef {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl LuaOwnedRef {
|
||||
pub(crate) const fn new(lua: Lua, index: c_int) -> Self {
|
||||
#[cfg(feature = "send")]
|
||||
{
|
||||
let _lua = lua;
|
||||
let _index = index;
|
||||
panic!("mlua must be compiled without \"send\" feature to use Owned types");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "send"))]
|
||||
LuaOwnedRef {
|
||||
lua,
|
||||
index,
|
||||
_non_send: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const fn to_ref(&self) -> LuaRef {
|
||||
LuaRef {
|
||||
lua: &self.lua,
|
||||
|
@ -275,4 +298,7 @@ mod assertions {
|
|||
|
||||
static_assertions::assert_impl_all!(RegistryKey: Send, Sync);
|
||||
static_assertions::assert_not_impl_any!(LuaRef: Send);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
static_assertions::assert_not_impl_any!(LuaOwnedRef: Send);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ use crate::ffi;
|
|||
use crate::function::Function;
|
||||
use crate::lua::Lua;
|
||||
use crate::table::{Table, TablePairs};
|
||||
use crate::types::{Callback, LuaOwnedRef, LuaRef, MaybeSend};
|
||||
use crate::types::{Callback, LuaRef, MaybeSend};
|
||||
use crate::util::{check_stack, get_userdata, take_userdata, StackGuard};
|
||||
use crate::value::{FromLua, FromLuaMulti, ToLua, ToLuaMulti};
|
||||
|
||||
|
@ -782,9 +782,12 @@ impl Serialize for UserDataSerializeError {
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct AnyUserData<'lua>(pub(crate) LuaRef<'lua>);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OwnedAnyUserData(pub(crate) LuaOwnedRef);
|
||||
pub struct OwnedAnyUserData(pub(crate) crate::types::LuaOwnedRef);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl OwnedAnyUserData {
|
||||
pub const fn to_ref(&self) -> AnyUserData {
|
||||
AnyUserData(self.0.to_ref())
|
||||
|
@ -1067,6 +1070,8 @@ impl<'lua> AnyUserData<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||
#[inline]
|
||||
pub fn into_owned(self) -> OwnedAnyUserData {
|
||||
OwnedAnyUserData(self.0.into_owned())
|
||||
|
@ -1238,4 +1243,7 @@ mod assertions {
|
|||
use super::*;
|
||||
|
||||
static_assertions::assert_not_impl_any!(AnyUserData: Send);
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
static_assertions::assert_not_impl_any!(OwnedAnyUserData: Send);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue