From c60f633a62e62b33173cc5567b17dd005a89e6cc Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Mon, 19 Dec 2022 22:11:52 +0000 Subject: [PATCH] Add "unstable" feature flag. Hide owned types under the new feature flag. Drop OwnedString/OwnedThread types (unlikely they are useful). --- Cargo.toml | 3 ++- src/conversion.rs | 47 ++++++++++++++--------------------------------- src/function.rs | 12 ++++++++++-- src/lib.rs | 12 ++++++++---- src/lua.rs | 17 ++++++++--------- src/string.rs | 19 +------------------ src/table.rs | 12 ++++++++++-- src/thread.rs | 19 +------------------ src/types.rs | 26 ++++++++++++++++++++++++++ src/userdata.rs | 12 ++++++++++-- 10 files changed, 90 insertions(+), 89 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cffce2c..b4840f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/conversion.rs b/src/conversion.rs index 5e89649..e5e1cf6 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -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> { @@ -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> { - 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 { - 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> { @@ -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> { @@ -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 { @@ -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> { @@ -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 { @@ -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> { - 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 { - 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> { @@ -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> { @@ -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 { diff --git a/src/function.rs b/src/function.rs index 0581e92..0754982 100644 --- a/src/function.rs +++ b/src/function.rs @@ -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); } diff --git a/src/lib.rs b/src/lib.rs index 3dd3ca6..de72331 100644 --- a/src/lib.rs +++ b/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. diff --git a/src/lua.rs b/src/lua.rs index e22b8d7..ea0ac53 100644 --- a/src/lua.rs +++ b/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" diff --git a/src/string.rs b/src/string.rs index 60732d8..3f40040 100644 --- a/src/string.rs +++ b/src/string.rs @@ -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> { diff --git a/src/table.rs b/src/table.rs index 0f0654e..7099ce0 100644 --- a/src/table.rs +++ b/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); } diff --git a/src/thread.rs b/src/thread.rs index 4e5bd9e..dfce272 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -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> { diff --git a/src/types.rs b/src/types.rs index a02c21a..ace1bf6 100644 --- a/src/types.rs +++ b/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); } diff --git a/src/userdata.rs b/src/userdata.rs index 48d731b..0b0d3e7 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -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); }