-
Notifications
You must be signed in to change notification settings - Fork 0
Rust Extensions
Build native Lua modules with Rust for better performance.
LPM supports building Rust extensions that compile to native Lua modules (dynamic libraries). These modules can be loaded by Lua just like any other module.
LPM can both:
-
Build your own Rust extensions locally using
lpm build -
Install Rust packages from LuaRocks that use
luarocks-build-rust-mluabuild backend
Add Rust build configuration to package.yaml:
name: my-project
version: 1.0.0
build:
type: rust
manifest: "Cargo.toml"
modules:
mymodule: "target/release/libmymodule.so" # Linux/macOS
# or
mymodule: "target/release/mymodule.dll" # WindowsYour Rust code should use mlua to create Lua modules:
// src/lib.rs
use mlua::prelude::*;
#[mlua::lua_module]
fn mymodule(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("hello", lua.create_function(|_, ()| {
Ok("Hello from Rust!")
})?)?;
Ok(exports)
}# Cargo.toml
[package]
name = "mymodule"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"] # Important: must be cdylib
[dependencies]
mlua = { version = "0.11", features = ["lua54", "vendored"] }LPM can install Rust packages from LuaRocks that use the luarocks-build-rust-mlua build backend:
# Install a Rust package from LuaRocks
lpm install rustaceanvimLPM will:
- Download the package source
- Detect it uses Rust build type
- Run
cargo build --releaseto build the extension - Install the built library and Lua files
Prerequisites: Rust toolchain (rustc, cargo) must be installed.
lpm buildlpm build --target x86_64-unknown-linux-gnu
lpm build --target aarch64-apple-darwin
lpm build --target x86_64-pc-windows-msvclpm build --all-targetsBuilds for all common platforms:
x86_64-unknown-linux-gnuaarch64-unknown-linux-gnux86_64-apple-darwinaarch64-apple-darwinx86_64-pc-windows-msvc
LPM uses:
-
macOS/Linux:
cargo-zigbuildwith Zig for cross-compilation -
Windows:
cargo-xwinfor MSVC targets
No additional setup required - LPM handles everything automatically.
After building, use the module like any Lua module:
-- main.lua
local mymodule = require("mymodule")
print(mymodule.hello()) -- "Hello from Rust!"LPM automatically sets up package.cpath to find native modules.
LPM supports downloading pre-built binaries from external URLs:
- Checks local cache - Uses cached binaries if available
-
Parses binary URLs from rockspec - Looks for
binary_urlsin rockspec metadata - Downloads from URL - Downloads binary matching your Lua version and platform
- Falls back to building from source - If no binary URL is found
Add binary URLs to your rockspec metadata:
metadata = {
binary_urls = {
["5.4-x86_64-unknown-linux-gnu"] = "https://github.com/user/repo/releases/download/v1.0.0/libmymodule-linux-x64.so",
["5.4-aarch64-apple-darwin"] = "https://github.com/user/repo/releases/download/v1.0.0/libmymodule-macos-arm64.dylib",
["5.4-x86_64-pc-windows-msvc"] = "https://github.com/user/repo/releases/download/v1.0.0/mymodule-windows-x64.dll",
}
}The key format is: "{lua_version}-{target_triple}"
LPM will automatically:
- Detect your Lua version (e.g., 5.4)
- Detect your platform (e.g., x86_64-unknown-linux-gnu)
- Match and download the appropriate binary
- Cache it for future installations
Package built binaries for distribution:
lpm package
lpm package --target x86_64-unknown-linux-gnuCreates distributable archives with the compiled modules.
Publish packages with Rust extensions:
# Include pre-built binaries in published package
lpm publish --with-binariesLPM will:
- Build for all common targets
- Package the binaries
- Include them in the published package
Rust extensions must specify which Lua versions they support:
lua_version: "5.4" # Or ">=5.1", "5.1 || 5.3 || 5.4"LPM will:
- Detect your installed Lua version
- Build extensions compatible with that version
- Cache builds per Lua version
-
Always use
cdylib: Rust code must compile as dynamic libraries -
Specify modules: Map module names to library paths in
package.yaml - Version constraints: Specify Lua version requirements
- Test locally: Build and test before publishing
- Pre-built binaries: Consider providing pre-built binaries for common targets