📗 added basic-calc example

This commit is contained in:
noib3 2022-07-12 16:24:33 +02:00
parent a3b1908b95
commit e9bdec20e9
No known key found for this signature in database
GPG key ID: 7AF92216C504A017
7 changed files with 73 additions and 13 deletions

View 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
View file

@ -0,0 +1,4 @@
/target
/calc.lua
/Cargo.lock
/lua

View 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" }

View 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))
```

View 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)),
]))
}

View file

@ -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>

View file

@ -107,6 +107,7 @@ impl Object {
/// TODO: docs
#[inline(always)]
#[doc(hidden)]
pub unsafe fn as_luaref_unchecked(&self) -> LuaRef {
self.data.luaref
}