Struct tlua::LuaFunction
source · [−]pub struct LuaFunction<L> { /* private fields */ }Expand description
Handle to a function in the Lua context.
Just like you can read variables as integers and strings, you can also read Lua functions by
requesting a LuaFunction object. Once you have a LuaFunction you can call it with call().
Note: Passing parameters when calling the function is not yet implemented.
Example
let lua = tlua::Lua::new();
lua.exec("function foo() return 12 end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("foo").unwrap();
let result: i32 = foo.call().unwrap();
assert_eq!(result, 12);Implementations
sourceimpl<L> LuaFunction<L> where
L: AsLua,
impl<L> LuaFunction<L> where
L: AsLua,
pub fn into_inner(self) -> L
sourceimpl<'lua, L> LuaFunction<L> where
L: 'lua,
L: AsLua,
impl<'lua, L> LuaFunction<L> where
L: 'lua,
L: AsLua,
sourcepub fn call<V>(&'lua self) -> Result<V, LuaError> where
V: LuaRead<PushGuard<&'lua L>>,
pub fn call<V>(&'lua self) -> Result<V, LuaError> where
V: LuaRead<PushGuard<&'lua L>>,
Calls the function. Doesn’t allow passing parameters.
TODO: will eventually disappear and get replaced with call_with_args
Returns an error if there is an error while executing the Lua code (eg. a function call returns an error), or if the requested return type doesn’t match the actual return type.
Note: In order to pass parameters, see
call_with_argsinstead.
sourcepub fn into_call<V>(self) -> Result<V, LuaError> where
V: LuaRead<PushGuard<Self>>,
pub fn into_call<V>(self) -> Result<V, LuaError> where
V: LuaRead<PushGuard<Self>>,
Calls the function taking ownership of the underlying push guard. Doesn’t allow passing parameters.
TODO: will eventually disappear and get replaced with call_with_args
Returns an error if there is an error while executing the Lua code (eg. a function call returns an error), or if the requested return type doesn’t match the actual return type.
Note: In order to pass parameters, see
into_call_with_argsinstead.
sourcepub fn call_with_args<V, A>(&'lua self, args: A) -> Result<V, CallError<A::Err>> where
A: PushInto<LuaState>,
V: LuaRead<PushGuard<&'lua L>>,
pub fn call_with_args<V, A>(&'lua self, args: A) -> Result<V, CallError<A::Err>> where
A: PushInto<LuaState>,
V: LuaRead<PushGuard<&'lua L>>,
Calls the function with parameters.
TODO: should be eventually be renamed to call
Note: this function can return multiple values if V is a tuple.
- If the expected number of values is less than the actual, only the first few values will be returned.
- If the expected number of values is greater than the actual, the
function will return an error, unless the excess elements are
Option
.
You can either pass a single value by passing a single value, or multiple parameters by passing a tuple. If you pass a tuple, the first element of the tuple will be the first argument, the second element of the tuple the second argument, and so on.
Returns an error if there is an error while executing the Lua code (eg. a function call returns an error), if the requested return type doesn’t match the actual return type, or if we failed to push an argument.
Example
let lua = tlua::Lua::new();
lua.exec("function sub(a, b) return a - b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("sub").unwrap();
let result: i32 = foo.call_with_args((18, 4)).unwrap();
assert_eq!(result, 14);Multiple return values
let lua = tlua::Lua::new();
lua.exec("function divmod(a, b) return math.floor(a / b), a % b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("divmod").unwrap();
let first_result: i32 = foo.call_with_args((18, 4)).unwrap();
assert_eq!(first_result, 4);
let all_result: (i32, i32) = foo.call_with_args((18, 4)).unwrap();
assert_eq!(all_result, (4, 2));
let excess_results: (i32, i32, Option<i32>) = foo.call_with_args((18, 4)).unwrap();
assert_eq!(excess_results, (4, 2, None));sourcepub fn into_call_with_args<V, A>(self, args: A) -> Result<V, CallError<A::Err>> where
A: PushInto<LuaState>,
V: LuaRead<PushGuard<Self>>,
pub fn into_call_with_args<V, A>(self, args: A) -> Result<V, CallError<A::Err>> where
A: PushInto<LuaState>,
V: LuaRead<PushGuard<Self>>,
Calls the function with parameters taking ownership of the underlying push guard.
TODO: should be eventually be renamed to call
Note: this function can return multiple values if V is a tuple.
- If the expected number of values is less than the actual, only the first few values will be returned.
- If the expected number of values is greater than the actual, the
function will return an error, unless the excess elements are
Option
.
You can either pass a single value by passing a single value, or multiple parameters by passing a tuple. If you pass a tuple, the first element of the tuple will be the first argument, the second element of the tuple the second argument, and so on.
Returns an error if there is an error while executing the Lua code (eg. a function call returns an error), if the requested return type doesn’t match the actual return type, or if we failed to push an argument.
Example
let lua = tlua::Lua::new();
lua.exec("function sub(a, b) return a - b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("sub").unwrap();
let result: i32 = foo.into_call_with_args((18, 4)).unwrap();
assert_eq!(result, 14);Multiple return values
let lua = tlua::Lua::new();
lua.exec("function divmod(a, b) return math.floor(a / b), a % b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("divmod").unwrap();
let all_result: (i32, i32) = foo.into_call_with_args((18, 4)).unwrap();
assert_eq!(all_result, (4, 2));sourceimpl<L> LuaFunction<PushGuard<L>> where
L: AsLua,
impl<L> LuaFunction<PushGuard<L>> where
L: AsLua,
sourcepub fn load_from_reader(lua: L, code: impl Read) -> Result<Self, LuaError>
pub fn load_from_reader(lua: L, code: impl Read) -> Result<Self, LuaError>
Builds a new LuaFunction from the code of a reader.
Returns an error if reading from the Read object fails or if there is a syntax error in
the code.
Example
use std::io::Cursor;
let lua = tlua::Lua::new();
let f = tlua::LuaFunction::load_from_reader(&lua, Cursor::new("return 8")).unwrap();
let ret: i32 = f.call().unwrap();
assert_eq!(ret, 8);Trait Implementations
sourceimpl<L> AsLua for LuaFunction<L> where
L: AsLua,
impl<L> AsLua for LuaFunction<L> where
L: AsLua,
fn as_lua(&self) -> LuaState
sourcefn try_push<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)> where
Self: Sized,
T: PushInto<Self>,
fn try_push<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)> where
Self: Sized,
T: PushInto<Self>,
Try to push v onto the lua stack. Read more
sourcefn push<T>(self, v: T) -> PushGuard<Self> where
Self: Sized,
T: PushInto<Self>,
<T as PushInto<Self>>::Err: Into<Void>,
fn push<T>(self, v: T) -> PushGuard<Self> where
Self: Sized,
T: PushInto<Self>,
<T as PushInto<Self>>::Err: Into<Void>,
Push v onto the lua stack. Read more
sourcefn try_push_one<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)> where
Self: Sized,
T: PushOneInto<Self>,
fn try_push_one<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)> where
Self: Sized,
T: PushOneInto<Self>,
Try to push v onto the lua stack. Read more
sourcefn push_one<T>(self, v: T) -> PushGuard<Self> where
Self: Sized,
T: PushOneInto<Self>,
<T as PushInto<Self>>::Err: Into<Void>,
fn push_one<T>(self, v: T) -> PushGuard<Self> where
Self: Sized,
T: PushOneInto<Self>,
<T as PushInto<Self>>::Err: Into<Void>,
Push v onto the lua stack. Read more
sourcefn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self> where
Self: Sized,
I: Iterator,
<I as Iterator>::Item: PushInto<LuaState>,
<<I as Iterator>::Item as PushInto<LuaState>>::Err: Into<Void>,
fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self> where
Self: Sized,
I: Iterator,
<I as Iterator>::Item: PushInto<LuaState>,
<<I as Iterator>::Item as PushInto<LuaState>>::Err: Into<Void>,
Push iterator onto the lua stack as a lua table. Read more
sourcefn try_push_iter<I>(
self,
iterator: I
) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)> where
Self: Sized,
I: Iterator,
<I as Iterator>::Item: PushInto<LuaState>,
fn try_push_iter<I>(
self,
iterator: I
) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)> where
Self: Sized,
I: Iterator,
<I as Iterator>::Item: PushInto<LuaState>,
Push iterator onto the lua stack as a lua table. Read more
fn read<T>(self) -> Result<T, Self> where
Self: Sized,
T: LuaRead<Self>,
fn read_at<T>(self, index: i32) -> Result<T, Self> where
Self: Sized,
T: LuaRead<Self>,
fn read_at_nz<T>(self, index: NonZeroI32) -> Result<T, Self> where
Self: Sized,
T: LuaRead<Self>,
sourceimpl<L> AsRef<Object<L>> for LuaFunction<L>
impl<L> AsRef<Object<L>> for LuaFunction<L>
sourceimpl<L> Call<L> for LuaFunction<L> where
L: AsLua,
impl<L> Call<L> for LuaFunction<L> where
L: AsLua,
fn call<'lua, R>(&'lua self) -> Result<R, LuaError> where
L: 'lua,
R: LuaRead<PushGuard<&'lua L>>,
fn call_with<'lua, A, R>(&'lua self, args: A) -> Result<R, CallError<A::Err>> where
L: 'lua,
A: PushInto<LuaState>,
R: LuaRead<PushGuard<&'lua L>>,
fn into_call<R>(self) -> Result<R, LuaError> where
Self: AsLua + Sized,
R: LuaRead<PushGuard<Self>>,
fn into_call_with<A, R>(self, args: A) -> Result<R, CallError<A::Err>> where
Self: AsLua + Sized,
A: PushInto<LuaState>,
R: LuaRead<PushGuard<Self>>,
sourceimpl<L: Debug> Debug for LuaFunction<L>
impl<L: Debug> Debug for LuaFunction<L>
sourceimpl<L> From<LuaFunction<L>> for Object<L> where
L: AsLua,
impl<L> From<LuaFunction<L>> for Object<L> where
L: AsLua,
sourcefn from(o: LuaFunction<L>) -> Self
fn from(o: LuaFunction<L>) -> Self
Converts to this type from the input type.
sourceimpl<L> LuaRead<L> for LuaFunction<L> where
L: AsLua,
impl<L> LuaRead<L> for LuaFunction<L> where
L: AsLua,
sourcefn lua_read_at_position(lua: L, index: NonZeroI32) -> Result<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> Result<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> Result<Self, L>
sourceimpl<L, T> Push<L> for LuaFunction<T> where
L: AsLua,
impl<L, T> Push<L> for LuaFunction<T> where
L: AsLua,
sourcefn push_to_lua(&self, lua: L) -> PushResult<L, Self>
fn push_to_lua(&self, lua: L) -> PushResult<L, Self>
Pushes the value on the top of the stack. Read more
sourceimpl<L> TryFrom<Object<L>> for LuaFunction<L> where
L: AsLua,
impl<L> TryFrom<Object<L>> for LuaFunction<L> where
L: AsLua,
impl<L, T> PushOne<L> for LuaFunction<T> where
L: AsLua,
Auto Trait Implementations
impl<L> RefUnwindSafe for LuaFunction<L> where
L: RefUnwindSafe,
impl<L> Send for LuaFunction<L> where
L: Send,
impl<L> Sync for LuaFunction<L> where
L: Sync,
impl<L> Unpin for LuaFunction<L> where
L: Unpin,
impl<L> UnwindSafe for LuaFunction<L> where
L: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more