Skip to content

Commit

Permalink
fix clippy warnings (#295)
Browse files Browse the repository at this point in the history
* fix clippy warnings

* fix more clippy warnings

* chore: add back removed code from examples

---------

Co-authored-by: Thomas Frank <[email protected]>
Co-authored-by: Luis Moreno <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent a2771e0 commit 9b19339
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 77 deletions.
3 changes: 2 additions & 1 deletion examples/class-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use node_bindgen::core::val::JsEnv;
use node_bindgen::core::TryIntoJs;
use node_bindgen::derive::node_bindgen;

struct MyJson {
pub struct MyJson {
val: f64,
}

Expand All @@ -25,6 +25,7 @@ impl TryIntoJs for MyJson {
}
}


struct MyObject {
val: f64,
}
Expand Down
11 changes: 0 additions & 11 deletions nj-core/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ fn napi_value_type_to_string(js_type: napi_valuetype) -> &'static str {
}
}

#[derive(Clone)]
pub struct JsNapiValue(napi_value);

unsafe impl Send for JsNapiValue {}

impl From<napi_value> for JsNapiValue {
fn from(value: napi_value) -> Self {
Self(value)
}
}

#[derive(Clone, Copy, Debug)]
pub struct JsEnv(napi_env);

Expand Down
2 changes: 1 addition & 1 deletion nj-core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ fn init_module() {
};

unsafe {
napi_module_register(&mut _MODULE);
napi_module_register(ptr::addr_of_mut!(_MODULE));
}
}
52 changes: 0 additions & 52 deletions nj-core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::ptr;
use tracing::error;
use tracing::trace;
use tracing::debug;
use async_trait::async_trait;
use futures_lite::Future;

use fluvio_future::task::spawn;
Expand All @@ -13,9 +12,7 @@ use crate::sys::napi_value;
use crate::val::JsEnv;
use crate::NjError;
use crate::sys::napi_env;
use crate::sys::napi_callback_info;
use crate::TryIntoJs;
use crate::IntoJs;
use crate::assert_napi;
use crate::ThreadSafeFunction;

Expand Down Expand Up @@ -119,55 +116,6 @@ where
}
}

#[async_trait]
pub trait JSWorker: Sized + Send + 'static {
type Output: TryIntoJs;

/// create new worker based on argument based in the callback
/// only need if it is called as method
fn create_worker(_env: &JsEnv, _info: napi_callback_info) -> Result<Self, NjError> {
Err(NjError::InvalidType(
"worker".to_owned(),
"worker".to_owned(),
))
}

/// call by Node to create promise
extern "C" fn start_promise(env: napi_env, info: napi_callback_info) -> napi_value {
let js_env = JsEnv::new(env);

let result: Result<napi_value, NjError> = (|| {
let worker = Self::create_worker(&js_env, info)?;
worker.create_promise(&js_env)
})();

result.into_js(&js_env)
}

/// create promise and schedule work
/// when this is finished it will return result in the main thread
fn create_promise(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let (promise, deferred) = js_env.create_promise()?;
let function_name = format!("async_worker_th_{}", std::any::type_name::<Self>());
let ts_fn = js_env.create_thread_safe_function(
&function_name,
None,
Some(promise_complete::<Self::Output>),
)?;
let js_deferred = JsDeferred(deferred);

spawn(async move {
let result = self.execute().await;
finish_worker(ts_fn, result, js_deferred);
});

Ok(promise)
}

/// execute this in async worker thread
async fn execute(mut self) -> Self::Output;
}

pub trait NjFutureExt: Future {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError>
where
Expand Down
12 changes: 0 additions & 12 deletions nj-derive/src/ast/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use syn::ItemFn;
use syn::Ident;
use syn::TypePath;
use syn::ImplItemMethod;
Expand All @@ -7,17 +6,6 @@ use syn::PathArguments;
use syn::Lifetime;
use syn::GenericArgument;

/// traits for function item
pub trait FunctionItem {
fn name(&self) -> &Ident;
}

impl FunctionItem for ItemFn {
fn name(&self) -> &Ident {
&self.sig.ident
}
}

pub trait TypePathUtil {
fn name_identifier(&self) -> Option<&Ident>;
fn lifetime(&self) -> Option<&Lifetime>;
Expand Down

0 comments on commit 9b19339

Please sign in to comment.