mirror of
https://github.com/noib3/nvim-oxi.git
synced 2025-08-04 10:59:57 +00:00
📗 added basic-calc
example
This commit is contained in:
parent
a3b1908b95
commit
e9bdec20e9
7 changed files with 73 additions and 13 deletions
11
examples/basic-calc/.cargo/config
Normal file
11
examples/basic-calc/.cargo/config
Normal file
|
@ -0,0 +1,11 @@
|
|||
[target.x86_64-apple-darwin]
|
||||
rustflags = [
|
||||
"-C", "link-arg=-undefined",
|
||||
"-C", "link-arg=dynamic_lookup",
|
||||
]
|
||||
|
||||
[target.aarch64-apple-darwin]
|
||||
rustflags = [
|
||||
"-C", "link-arg=-undefined",
|
||||
"-C", "link-arg=dynamic_lookup",
|
||||
]
|
4
examples/basic-calc/.gitignore
vendored
Normal file
4
examples/basic-calc/.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/target
|
||||
/calc.lua
|
||||
/Cargo.lock
|
||||
/lua
|
11
examples/basic-calc/Cargo.toml
Normal file
11
examples/basic-calc/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "calc"
|
||||
version = "0.1.0"
|
||||
authors = ["Riccardo Mazzarini <riccardo.mazzarini@pm.me>"]
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
nvim-oxi = { path = "../../nvim-oxi" }
|
15
examples/basic-calc/README.md
Normal file
15
examples/basic-calc/README.md
Normal file
|
@ -0,0 +1,15 @@
|
|||
# basic-calc
|
||||
|
||||
This ...
|
||||
|
||||
```lua
|
||||
local calc = require("calc")
|
||||
|
||||
-- All the following commands will print `42` in the Neovim message area.
|
||||
|
||||
print(calc.add(1, 41))
|
||||
print(calc.multiply(2, 21))
|
||||
|
||||
print(calc.compute(function(a, b) return a + b; end, 1, 41))
|
||||
print(calc.compute(function(a, b) return a * b; end, 2, 21))
|
||||
```
|
18
examples/basic-calc/src/lib.rs
Normal file
18
examples/basic-calc/src/lib.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use nvim_oxi::{self as nvim, Dictionary, Function, Object};
|
||||
|
||||
#[nvim::module]
|
||||
fn calc() -> nvim::Result<Dictionary> {
|
||||
let add = Function::from_fn(|(a, b): (i32, i32)| Ok(a + b));
|
||||
|
||||
let multiply = Function::from_fn(|(a, b): (i32, i32)| Ok(a * b));
|
||||
|
||||
let compute = Function::from_fn(
|
||||
|(fun, a, b): (Function<(i32, i32), i32>, i32, i32)| fun.call((a, b)),
|
||||
);
|
||||
|
||||
Ok(Dictionary::from_iter([
|
||||
("add", Object::from(add)),
|
||||
("multiply", Object::from(multiply)),
|
||||
("compute", Object::from(compute)),
|
||||
]))
|
||||
}
|
|
@ -5,11 +5,11 @@ use std::result::Result as StdResult;
|
|||
use std::{fmt, mem, ptr};
|
||||
|
||||
use libc::{c_char, c_int};
|
||||
use nvim_types::{LuaRef, Object};
|
||||
use nvim_types::{LuaRef, Object, ObjectKind};
|
||||
use serde::{de, ser};
|
||||
|
||||
use super::{ffi::*, LuaPoppable, LuaPushable};
|
||||
use crate::object::ToObject;
|
||||
use crate::object::{FromObject, ToObject};
|
||||
use crate::Result;
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||
|
@ -33,17 +33,17 @@ impl<A, R> ToObject for Function<A, R> {
|
|||
}
|
||||
}
|
||||
|
||||
// impl<A, R> FromObject for Function<A, R> {
|
||||
// fn from_obj(obj: Object) -> Result<Function<A, R>> {
|
||||
// // match obj.r#type
|
||||
// // (matches!(obj.r#type, nvim_types::ObjectType::kObjectTypeLuaRef))
|
||||
// // .then_some(unsafe { obj.data.$data })
|
||||
// // .ok_or_else(|| Primitive {
|
||||
// // expected: $variant,
|
||||
// // actual: obj.r#type,
|
||||
// // })
|
||||
// }
|
||||
// }
|
||||
impl<A, R> FromObject for Function<A, R> {
|
||||
fn from_obj(obj: Object) -> Result<Function<A, R>> {
|
||||
match obj.kind() {
|
||||
ObjectKind::LuaRef => {
|
||||
let luaref = unsafe { obj.as_luaref_unchecked() };
|
||||
Ok(Self(luaref, PhantomData, PhantomData))
|
||||
},
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, A, R> de::Deserialize<'de> for Function<A, R> {
|
||||
fn deserialize<D>(deserializer: D) -> StdResult<Self, D::Error>
|
||||
|
|
|
@ -107,6 +107,7 @@ impl Object {
|
|||
|
||||
/// TODO: docs
|
||||
#[inline(always)]
|
||||
#[doc(hidden)]
|
||||
pub unsafe fn as_luaref_unchecked(&self) -> LuaRef {
|
||||
self.data.luaref
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue