Skip to content

Commit

Permalink
fix: resource impl (#242)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <[email protected]>
  • Loading branch information
fabriziosestito authored Dec 21, 2024
1 parent e17dabb commit 515ede6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
5 changes: 4 additions & 1 deletion native/rhai_rustler/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::sync::Mutex;

use rhai::AST;
use rustler::ResourceArc;
use rustler::{Resource, ResourceArc};

pub struct ASTResource {
pub ast: Mutex<AST>,
}

#[rustler::resource_impl]
impl Resource for ASTResource {}

#[rustler::nif]
fn ast_empty() -> ResourceArc<ASTResource> {
ResourceArc::new(ASTResource {
Expand Down
5 changes: 4 additions & 1 deletion native/rhai_rustler/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rhai::{
use rhai_dylib::loader::{libloading::Libloading, Loader};
use rhai_dylib::module_resolvers::libloading::DylibModuleResolver;

use rustler::{Env, NifUnitEnum, ResourceArc, Term};
use rustler::{Env, NifUnitEnum, Resource, ResourceArc, Term};

use crate::{
ast::ASTResource,
Expand All @@ -29,6 +29,9 @@ pub struct EngineResource {
pub engine: Mutex<Engine>,
}

#[rustler::resource_impl]
impl Resource for EngineResource {}

#[rustler::nif]
fn engine_new() -> ResourceArc<EngineResource> {
let engine = Engine::new();
Expand Down
15 changes: 2 additions & 13 deletions native/rhai_rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ mod types;
use rhai::config::hashing::set_hashing_seed;
use rustler::{Env, Term};

use crate::ast::*;
use crate::engine::*;
use crate::scope::*;

fn load(env: Env, _: Term) -> bool {
fn load(_: Env, _: Term) -> bool {
// Set dylib ahash seed
if let Err(value) = set_hashing_seed(Some([1, 3, 3, 7])) {
eprintln!(
Expand All @@ -21,14 +17,7 @@ fn load(env: Env, _: Term) -> bool {
return false;
}

let resources = [
rustler::resource!(EngineResource, env),
rustler::resource!(ScopeResource, env),
rustler::resource!(ASTResource, env),
];

// If any resource was not loaded correctly, return false
!resources.into_iter().any(|resource| !resource)
true
}

rustler::init!("Elixir.Rhai.Native", load = load);
5 changes: 4 additions & 1 deletion native/rhai_rustler/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Mutex;

use rhai::Scope;
use rustler::{Encoder, Env, ResourceArc, Term};
use rustler::{Encoder, Env, Resource, ResourceArc, Term};

use crate::{
error::{RhaiRustlerError, ScopeError},
Expand All @@ -12,6 +12,9 @@ pub struct ScopeResource {
pub scope: Mutex<Scope<'static>>,
}

#[rustler::resource_impl]
impl Resource for ScopeResource {}

#[rustler::nif]
fn scope_new() -> ResourceArc<ScopeResource> {
ResourceArc::new(ScopeResource {
Expand Down

0 comments on commit 515ede6

Please sign in to comment.