Skip to content

Commit c8436e2

Browse files
committed
Make function module public
Reduce number of function-specific types exported to the mlua root and keep them inside the module.
1 parent 613748e commit c8436e2

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

src/function.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,84 @@
1+
//! Lua function handling.
2+
//!
3+
//! This module provides types for working with Lua functions from Rust, including
4+
//! both Lua-defined functions and native Rust callbacks.
5+
//!
6+
//! # Main Types
7+
//!
8+
//! - [`Function`] - A handle to a Lua function that can be called from Rust.
9+
//! - [`FunctionInfo`] - Debug information about a function (name, source, line numbers, etc.).
10+
//! - [`CoverageInfo`] - Code coverage data for Luau functions (requires `luau` feature).
11+
//!
12+
//! # Calling Functions
13+
//!
14+
//! Use [`Function::call`] to invoke a Lua function synchronously:
15+
//!
16+
//! ```
17+
//! # use mlua::{Function, Lua, Result};
18+
//! # fn main() -> Result<()> {
19+
//! let lua = Lua::new();
20+
//!
21+
//! // Get a built-in function
22+
//! let print: Function = lua.globals().get("print")?;
23+
//! print.call::<()>("Hello from Rust!")?;
24+
//!
25+
//! // Call a function that returns values
26+
//! let tonumber: Function = lua.globals().get("tonumber")?;
27+
//! let n: i32 = tonumber.call("42")?;
28+
//! assert_eq!(n, 42);
29+
//! # Ok(())
30+
//! # }
31+
//! ```
32+
//!
33+
//! For asynchronous execution, use `Function::call_async` (requires `async` feature):
34+
//!
35+
//! ```ignore
36+
//! let result: String = my_async_func.call_async(args).await?;
37+
//! ```
38+
//!
39+
//! # Creating Functions
40+
//!
41+
//! Functions can be created from Rust closures using [`Lua::create_function`]:
42+
//!
43+
//! ```
44+
//! # use mlua::{Lua, Result};
45+
//! # fn main() -> Result<()> {
46+
//! let lua = Lua::new();
47+
//!
48+
//! let greet = lua.create_function(|_, name: String| {
49+
//! Ok(format!("Hello, {}!", name))
50+
//! })?;
51+
//!
52+
//! lua.globals().set("greet", greet)?;
53+
//! let result: String = lua.load(r#"greet("World")"#).eval()?;
54+
//! assert_eq!(result, "Hello, World!");
55+
//! # Ok(())
56+
//! # }
57+
//! ```
58+
//!
59+
//! For simpler cases, use [`Function::wrap`] or [`Function::wrap_raw`] to convert a Rust function
60+
//! directly:
61+
//!
62+
//! ```
63+
//! # use mlua::{Function, Lua, Result};
64+
//! # fn main() -> Result<()> {
65+
//! let lua = Lua::new();
66+
//!
67+
//! fn add(a: i32, b: i32) -> i32 { a + b }
68+
//!
69+
//! lua.globals().set("add", Function::wrap_raw(add))?;
70+
//! let sum: i32 = lua.load("add(2, 3)").eval()?;
71+
//! assert_eq!(sum, 5);
72+
//! # Ok(())
73+
//! # }
74+
//! ```
75+
//!
76+
//! # Function Environments
77+
//!
78+
//! Lua functions have an associated environment table that determines how global
79+
//! variables are resolved. Use [`Function::environment`] and [`Function::set_environment`]
80+
//! to inspect or modify this environment.
81+
182
use std::cell::RefCell;
283
use std::os::raw::{c_int, c_void};
384
use std::{mem, ptr, slice};
@@ -681,7 +762,9 @@ impl LuaType for Function {
681762
const TYPE_ID: c_int = ffi::LUA_TFUNCTION;
682763
}
683764

765+
/// Future for asynchronous function calls.
684766
#[cfg(feature = "async")]
767+
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
685768
#[must_use = "futures do nothing unless you `.await` or poll them"]
686769
pub struct AsyncCallFuture<R: FromLuaMulti>(Result<AsyncThread<R>>);
687770

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ mod chunk;
7676
mod conversion;
7777
mod debug;
7878
mod error;
79-
mod function;
8079
#[cfg(any(feature = "luau", doc))]
8180
mod luau;
8281
mod memory;
@@ -94,6 +93,7 @@ mod util;
9493
mod value;
9594
mod vector;
9695

96+
pub mod function;
9797
pub mod prelude;
9898

9999
pub use bstr::BString;
@@ -102,7 +102,7 @@ pub use ffi::{self, lua_CFunction, lua_State};
102102
pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
103103
pub use crate::debug::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
104104
pub use crate::error::{Error, ErrorContext, ExternalError, ExternalResult, Result};
105-
pub use crate::function::{Function, FunctionInfo};
105+
pub use crate::function::Function;
106106
pub use crate::multi::{MultiValue, Variadic};
107107
pub use crate::scope::Scope;
108108
pub use crate::state::{GCMode, Lua, LuaOptions, WeakLua};
@@ -130,7 +130,6 @@ pub use crate::debug::HookTriggers;
130130
pub use crate::{
131131
buffer::Buffer,
132132
chunk::{CompileConstant, Compiler},
133-
function::CoverageInfo,
134133
luau::{HeapDump, NavigateError, Require, TextRequirer},
135134
vector::Vector,
136135
};

src/prelude.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ pub use crate::{
55
AnyUserData as LuaAnyUserData, BorrowedBytes as LuaBorrowedBytes, BorrowedStr as LuaBorrowedStr,
66
Chunk as LuaChunk, Either as LuaEither, Error as LuaError, ErrorContext as LuaErrorContext,
77
ExternalError as LuaExternalError, ExternalResult as LuaExternalResult, FromLua, FromLuaMulti,
8-
Function as LuaFunction, FunctionInfo as LuaFunctionInfo, GCMode as LuaGCMode, Integer as LuaInteger,
9-
IntoLua, IntoLuaMulti, LightUserData as LuaLightUserData, Lua, LuaNativeFn, LuaNativeFnMut, LuaOptions,
10-
LuaString, MetaMethod as LuaMetaMethod, MultiValue as LuaMultiValue, Nil as LuaNil, Number as LuaNumber,
8+
Function as LuaFunction, GCMode as LuaGCMode, Integer as LuaInteger, IntoLua, IntoLuaMulti,
9+
LightUserData as LuaLightUserData, Lua, LuaNativeFn, LuaNativeFnMut, LuaOptions, LuaString,
10+
MetaMethod as LuaMetaMethod, MultiValue as LuaMultiValue, Nil as LuaNil, Number as LuaNumber,
1111
ObjectLike as LuaObjectLike, RegistryKey as LuaRegistryKey, Result as LuaResult, StdLib as LuaStdLib,
1212
Table as LuaTable, TablePairs as LuaTablePairs, TableSequence as LuaTableSequence, Thread as LuaThread,
1313
ThreadStatus as LuaThreadStatus, UserData as LuaUserData, UserDataFields as LuaUserDataFields,
1414
UserDataMetatable as LuaUserDataMetatable, UserDataMethods as LuaUserDataMethods,
1515
UserDataRef as LuaUserDataRef, UserDataRefMut as LuaUserDataRefMut,
1616
UserDataRegistry as LuaUserDataRegistry, Value as LuaValue, Variadic as LuaVariadic,
17-
VmState as LuaVmState, WeakLua,
17+
VmState as LuaVmState, WeakLua, function::FunctionInfo as LuaFunctionInfo,
1818
};
1919

2020
#[cfg(not(feature = "luau"))]
@@ -24,9 +24,8 @@ pub use crate::HookTriggers as LuaHookTriggers;
2424
#[cfg(feature = "luau")]
2525
#[doc(no_inline)]
2626
pub use crate::{
27-
CompileConstant as LuaCompileConstant, CoverageInfo as LuaCoverageInfo,
28-
NavigateError as LuaNavigateError, Require as LuaRequire, TextRequirer as LuaTextRequirer,
29-
Vector as LuaVector,
27+
CompileConstant as LuaCompileConstant, NavigateError as LuaNavigateError, Require as LuaRequire,
28+
TextRequirer as LuaTextRequirer, Vector as LuaVector,
3029
};
3130

3231
#[cfg(feature = "async")]

tests/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn test_function_coverage() -> Result<()> {
267267

268268
assert_eq!(
269269
report[0],
270-
mlua::CoverageInfo {
270+
mlua::function::CoverageInfo {
271271
function: None,
272272
line_defined: 1,
273273
depth: 0,
@@ -276,7 +276,7 @@ fn test_function_coverage() -> Result<()> {
276276
);
277277
assert_eq!(
278278
report[1],
279-
mlua::CoverageInfo {
279+
mlua::function::CoverageInfo {
280280
function: Some("abc".into()),
281281
line_defined: 4,
282282
depth: 1,
@@ -285,7 +285,7 @@ fn test_function_coverage() -> Result<()> {
285285
);
286286
assert_eq!(
287287
report[2],
288-
mlua::CoverageInfo {
288+
mlua::function::CoverageInfo {
289289
function: None,
290290
line_defined: 12,
291291
depth: 1,
@@ -294,7 +294,7 @@ fn test_function_coverage() -> Result<()> {
294294
);
295295
assert_eq!(
296296
report[3],
297-
mlua::CoverageInfo {
297+
mlua::function::CoverageInfo {
298298
function: None,
299299
line_defined: 13,
300300
depth: 2,

0 commit comments

Comments
 (0)