Skip to main content

LuaHost

Struct LuaHost 

pub struct LuaHost<T> { /* private fields */ }
Expand description

kevy-side per-shard Lua host. Wraps a kevy_lua::Bridge plus the scoped-pointer plumbing.

T is whatever shard state the dispatch closure needs (Store, KeyspaceStore, …). It must outlive every LuaHost::eval call (trivially true: kevy holds the &mut T while delegating).

use std::collections::HashMap;
use kevy_lua_host::LuaHost;

type Keyspace = HashMap<Vec<u8>, Vec<u8>>;
let mut host = LuaHost::<Keyspace>::new(|ks, argv, _ro| {
    ks.insert(argv[1].to_vec(), argv[2].to_vec());
    b"+OK\r\n".to_vec()
});
let mut shard = Keyspace::new();
host.eval(&mut shard, b"return redis.call('SET', KEYS[1], ARGV[1])", &[b"k"], &[b"v"]);
assert_eq!(shard.get(&b"k"[..]), Some(&b"v".to_vec()));

Implementations§

§

impl<T: 'static> LuaHost<T>

pub fn new<F>(dispatch_fn: F) -> Self
where F: Fn(&mut T, &[&[u8]], bool) -> Vec<u8> + 'static,

Build a host with dispatch_fn as the redis.call backend.

dispatch_fn receives a &mut T (the current shard context), the script’s argv (command + args), and the read_only flag. It must return RESP reply bytes — production callers route to kevy’s dispatch, tests just return canned replies.

The closure receives &mut T via with_current, so it must be Fn(&mut T, …) -> … rather than FnMut. (kevy’s dispatch path is &mut self, so Fn(&mut T, …) is exactly what we need.)

// the context here is a log of every command the script sent
let mut host = kevy_lua_host::LuaHost::<Vec<Vec<u8>>>::new(|log, argv, _ro| {
    log.push(argv[0].to_vec());
    b"+OK\r\n".to_vec()
});
let mut log = Vec::new();
host.eval(&mut log, b"redis.call('PING') return redis.call('ECHO')", &[], &[]);
assert_eq!(log, [b"PING".to_vec(), b"ECHO".to_vec()]);

pub fn eval( &mut self, ctx: &mut T, script: &[u8], keys: &[&[u8]], args: &[&[u8]], ) -> Reply

Run a script. Scoped-installs ctx so the dispatch closure can find it via with_current, then delegates to Bridge::eval.

let mut host = kevy_lua_host::LuaHost::<i64>::new(|n, _argv, _ro| {
    *n += 1;
    format!(":{n}\r\n").into_bytes()
});
let mut counter = 10;
let reply = host.eval(&mut counter, b"return redis.call('INCR', KEYS[1])", &[b"c"], &[]);
assert_eq!(reply, b":11\r\n");
assert_eq!(counter, 11);

pub fn eval_ro( &mut self, ctx: &mut T, script: &[u8], keys: &[&[u8]], args: &[&[u8]], ) -> Reply

Read-only counterpart of Self::eval: the dispatch closure receives read_only = true.

let mut host = kevy_lua_host::LuaHost::<Vec<bool>>::new(|seen, _argv, ro| {
    seen.push(ro);
    b"+OK\r\n".to_vec()
});
let mut seen = Vec::new();
host.eval(&mut seen, b"return redis.call('GET', 'k')", &[], &[]);
host.eval_ro(&mut seen, b"return redis.call('GET', 'k')", &[], &[]);
assert_eq!(seen, [false, true]);

pub fn evalsha( &mut self, ctx: &mut T, sha1: ScriptSha1, keys: &[&[u8]], args: &[&[u8]], ) -> Reply

Run a previously-loaded script by SHA1.

let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
let sha = host.script_load(b"return tonumber(ARGV[1]) * 2");
assert_eq!(host.evalsha(&mut (), sha, &[], &[b"21"]), b":42\r\n");
assert!(host.evalsha(&mut (), [0; 20], &[], &[]).starts_with(b"-NOSCRIPT"));

pub fn evalsha_ro( &mut self, ctx: &mut T, sha1: ScriptSha1, keys: &[&[u8]], args: &[&[u8]], ) -> Reply

Read-only EVALSHA: the dispatch closure receives read_only = true.

let mut host = kevy_lua_host::LuaHost::<Vec<bool>>::new(|seen, _argv, ro| {
    seen.push(ro);
    b"+OK\r\n".to_vec()
});
let sha = host.script_load(b"return redis.call('GET', 'k')");
let mut seen = Vec::new();
host.evalsha_ro(&mut seen, sha, &[], &[]);
assert_eq!(seen, [true]);

pub fn script_load(&mut self, script: &[u8]) -> ScriptSha1

SCRIPT LOAD — cache without running. No context needed.

let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
let sha = host.script_load(b"return 1");
assert_eq!(sha, kevy_lua::sha1::sha1(b"return 1"));
assert_eq!(host.script_exists(&[sha]), [true]);

pub fn script_exists(&self, sha1s: &[ScriptSha1]) -> Vec<bool>

SCRIPT EXISTS.

let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
host.eval(&mut (), b"return 1", &[], &[]);
let ran = kevy_lua::sha1::sha1(b"return 1");
assert_eq!(host.script_exists(&[ran, [0; 20]]), [true, false]);

pub fn script_flush(&mut self, mode: FlushMode)

SCRIPT FLUSH.

let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
let sha = host.script_load(b"return 1");
host.script_flush(kevy_lua::FlushMode::Sync);
assert_eq!(host.script_exists(&[sha]), [false]);

pub fn set_instr_budget(&mut self, n: i64)

Forward kevy_lua::Bridge::set_instr_budget — set the per-Vm instruction cap. The operator wires [lua] time_limit_ms here at server startup.

let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
host.set_instr_budget(1_000);
assert!(host.eval(&mut (), b"while true do end", &[], &[]).starts_with(b"-"));

pub fn set_allowed_dialects(&mut self, versions: &[LuaVersion])

Forward kevy_lua::Bridge::set_allowed_dialects.

use kevy_lua::LuaVersion;
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
host.set_allowed_dialects(&[LuaVersion::Lua51, LuaVersion::Lua54]);
assert_eq!(host.eval(&mut (), b"#!lua version=5.4\nreturn 3 // 2", &[], &[]), b":1\r\n");
assert!(host.eval(&mut (), b"#!lua version=5.3\nreturn 1", &[], &[]).starts_with(b"-ERR"));

Trait Implementations§

§

impl<T: Debug> Debug for LuaHost<T>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for LuaHost<T>

§

impl<T> !Send for LuaHost<T>

§

impl<T> !Sync for LuaHost<T>

§

impl<T> !UnwindSafe for LuaHost<T>

§

impl<T> Freeze for LuaHost<T>
where PhantomData<fn() -> T>: Freeze,

§

impl<T> Unpin for LuaHost<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for LuaHost<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.