Merge pull request #333 from napi-rs/reduce-useless-type-cast
refactor(napi): reduce useless type cast
This commit is contained in:
commit
834a7b5bc1
10 changed files with 57 additions and 73 deletions
|
@ -39,12 +39,7 @@ pub fn run<T: Task>(env: &Env, task: T) -> Result<AsyncWorkPromise<'_>> {
|
|||
let mut raw_name = ptr::null_mut();
|
||||
let s = "napi_rs_async_work";
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_string_utf8(
|
||||
env.0,
|
||||
s.as_ptr() as *const c_char,
|
||||
s.len() as _,
|
||||
&mut raw_name,
|
||||
)
|
||||
sys::napi_create_string_utf8(env.0, s.as_ptr() as *const c_char, s.len(), &mut raw_name)
|
||||
})?;
|
||||
let result = Box::leak(Box::new(AsyncWork {
|
||||
inner_task: task,
|
||||
|
|
|
@ -151,12 +151,12 @@ impl Env {
|
|||
true => 1,
|
||||
false => 0,
|
||||
},
|
||||
len as _,
|
||||
len,
|
||||
words.as_ptr(),
|
||||
&mut raw_value,
|
||||
)
|
||||
})?;
|
||||
Ok(JsBigint::from_raw_unchecked(self.0, raw_value, len as _))
|
||||
Ok(JsBigint::from_raw_unchecked(self.0, raw_value, len))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -182,9 +182,7 @@ impl Env {
|
|||
#[inline]
|
||||
fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result<JsString> {
|
||||
let mut raw_value = ptr::null_mut();
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_string_utf8(self.0, data_ptr, len as _, &mut raw_value)
|
||||
})?;
|
||||
check_status!(unsafe { sys::napi_create_string_utf8(self.0, data_ptr, len, &mut raw_value) })?;
|
||||
Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) })
|
||||
}
|
||||
|
||||
|
@ -192,7 +190,7 @@ impl Env {
|
|||
pub fn create_string_utf16(&self, chars: &[u16]) -> Result<JsString> {
|
||||
let mut raw_value = ptr::null_mut();
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_string_utf16(self.0, chars.as_ptr(), chars.len() as _, &mut raw_value)
|
||||
sys::napi_create_string_utf16(self.0, chars.as_ptr(), chars.len(), &mut raw_value)
|
||||
})?;
|
||||
Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) })
|
||||
}
|
||||
|
@ -204,7 +202,7 @@ impl Env {
|
|||
sys::napi_create_string_latin1(
|
||||
self.0,
|
||||
chars.as_ptr() as *const _,
|
||||
chars.len() as _,
|
||||
chars.len(),
|
||||
&mut raw_value,
|
||||
)
|
||||
})?;
|
||||
|
@ -251,9 +249,7 @@ impl Env {
|
|||
#[inline]
|
||||
pub fn create_array_with_length(&self, length: usize) -> Result<JsObject> {
|
||||
let mut raw_value = ptr::null_mut();
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_array_with_length(self.0, length as _, &mut raw_value)
|
||||
})?;
|
||||
check_status!(unsafe { sys::napi_create_array_with_length(self.0, length, &mut raw_value) })?;
|
||||
Ok(unsafe { JsObject::from_raw_unchecked(self.0, raw_value) })
|
||||
}
|
||||
|
||||
|
@ -264,7 +260,7 @@ impl Env {
|
|||
let mut data: Vec<u8> = Vec::with_capacity(length);
|
||||
let mut data_ptr = data.as_mut_ptr() as *mut c_void;
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_buffer(self.0, length as _, &mut data_ptr, &mut raw_value)
|
||||
sys::napi_create_buffer(self.0, length, &mut data_ptr, &mut raw_value)
|
||||
})?;
|
||||
|
||||
Ok(JsBufferValue::new(
|
||||
|
@ -287,7 +283,7 @@ impl Env {
|
|||
check_status!(unsafe {
|
||||
sys::napi_create_external_buffer(
|
||||
self.0,
|
||||
length as _,
|
||||
length,
|
||||
data_ptr as *mut c_void,
|
||||
Some(drop_buffer),
|
||||
&mut length as *mut usize as *mut _,
|
||||
|
@ -322,7 +318,7 @@ impl Env {
|
|||
check_status!(unsafe {
|
||||
sys::napi_create_buffer_copy(
|
||||
self.0,
|
||||
length as _,
|
||||
length,
|
||||
data_ptr as *mut c_void,
|
||||
&mut copy_data,
|
||||
&mut raw_value,
|
||||
|
@ -344,7 +340,7 @@ impl Env {
|
|||
let mut data: Vec<u8> = Vec::with_capacity(length as usize);
|
||||
let mut data_ptr = data.as_mut_ptr() as *mut c_void;
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_arraybuffer(self.0, length as _, &mut data_ptr, &mut raw_value)
|
||||
sys::napi_create_arraybuffer(self.0, length, &mut data_ptr, &mut raw_value)
|
||||
})?;
|
||||
|
||||
Ok(JsArrayBufferValue::new(
|
||||
|
@ -362,7 +358,7 @@ impl Env {
|
|||
sys::napi_create_external_arraybuffer(
|
||||
self.0,
|
||||
data_ptr as *mut c_void,
|
||||
length as _,
|
||||
length,
|
||||
Some(drop_buffer),
|
||||
&mut length as *mut usize as *mut c_void,
|
||||
&mut raw_value,
|
||||
|
@ -512,10 +508,10 @@ impl Env {
|
|||
sys::napi_define_class(
|
||||
self.0,
|
||||
name.as_ptr() as *const c_char,
|
||||
name.len() as _,
|
||||
name.len(),
|
||||
Some(constructor_cb),
|
||||
ptr::null_mut(),
|
||||
raw_properties.len() as _,
|
||||
raw_properties.len(),
|
||||
raw_properties.as_ptr(),
|
||||
&mut raw_result,
|
||||
)
|
||||
|
|
|
@ -116,17 +116,17 @@ impl JsArrayBuffer {
|
|||
pub fn into_typedarray(
|
||||
self,
|
||||
typedarray_type: TypedArrayType,
|
||||
length: u64,
|
||||
byte_offset: u64,
|
||||
length: usize,
|
||||
byte_offset: usize,
|
||||
) -> Result<JsTypedArray> {
|
||||
let mut typedarray_value = ptr::null_mut();
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_typedarray(
|
||||
self.0.env,
|
||||
typedarray_type.into(),
|
||||
length as _,
|
||||
length,
|
||||
self.0.value,
|
||||
byte_offset as _,
|
||||
byte_offset,
|
||||
&mut typedarray_value,
|
||||
)
|
||||
})?;
|
||||
|
@ -138,14 +138,14 @@ impl JsArrayBuffer {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_dataview(self, length: u64, byte_offset: u64) -> Result<JsDataView> {
|
||||
pub fn into_dataview(self, length: usize, byte_offset: usize) -> Result<JsDataView> {
|
||||
let mut dataview_value = ptr::null_mut();
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_dataview(
|
||||
self.0.env,
|
||||
length as _,
|
||||
length,
|
||||
self.0.value,
|
||||
byte_offset as _,
|
||||
byte_offset,
|
||||
&mut dataview_value,
|
||||
)
|
||||
})?;
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{check_status, sys, Result};
|
|||
|
||||
pub struct JsBigint {
|
||||
pub(crate) raw: Value,
|
||||
pub word_count: u64,
|
||||
pub word_count: usize,
|
||||
}
|
||||
|
||||
impl JsBigint {
|
||||
|
@ -14,7 +14,7 @@ impl JsBigint {
|
|||
pub(crate) fn from_raw_unchecked(
|
||||
env: sys::napi_env,
|
||||
value: sys::napi_value,
|
||||
word_count: u64,
|
||||
word_count: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
raw: Value {
|
||||
|
@ -128,12 +128,12 @@ impl NapiValue for JsBigint {
|
|||
}
|
||||
|
||||
unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<Self> {
|
||||
let mut word_count: u64 = 0;
|
||||
let mut word_count = 0usize;
|
||||
check_status!(sys::napi_get_value_bigint_words(
|
||||
env,
|
||||
value,
|
||||
ptr::null_mut(),
|
||||
&mut word_count as *mut u64 as *mut _,
|
||||
&mut word_count,
|
||||
ptr::null_mut(),
|
||||
))?;
|
||||
Ok(JsBigint {
|
||||
|
@ -147,12 +147,12 @@ impl NapiValue for JsBigint {
|
|||
}
|
||||
|
||||
unsafe fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self {
|
||||
let mut word_count: u64 = 0;
|
||||
let mut word_count = 0usize;
|
||||
let status = sys::napi_get_value_bigint_words(
|
||||
env,
|
||||
value,
|
||||
ptr::null_mut(),
|
||||
&mut word_count as *mut u64 as *mut _,
|
||||
&mut word_count,
|
||||
ptr::null_mut(),
|
||||
);
|
||||
debug_assert!(
|
||||
|
@ -200,7 +200,7 @@ impl JsBigint {
|
|||
self.raw.env,
|
||||
self.raw.value,
|
||||
&mut sign_bit,
|
||||
word_count as *mut u64 as *mut _,
|
||||
word_count,
|
||||
words.as_mut_ptr(),
|
||||
)
|
||||
})?;
|
||||
|
|
|
@ -43,7 +43,7 @@ impl JsFunction {
|
|||
self.0.env,
|
||||
raw_this,
|
||||
self.0.value,
|
||||
args.len() as _,
|
||||
args.len(),
|
||||
raw_args.as_ptr(),
|
||||
&mut return_value,
|
||||
)
|
||||
|
@ -70,7 +70,7 @@ impl JsFunction {
|
|||
sys::napi_new_instance(
|
||||
self.0.env,
|
||||
self.0.value,
|
||||
length as _,
|
||||
length,
|
||||
raw_args.as_ptr(),
|
||||
&mut js_instance,
|
||||
)
|
||||
|
|
|
@ -369,7 +369,7 @@ macro_rules! impl_object_methods {
|
|||
let mut js_key = ptr::null_mut();
|
||||
let mut result = false;
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_string_utf8(self.0.env, string.as_ptr(), name.len() as _, &mut js_key)
|
||||
sys::napi_create_string_utf8(self.0.env, string.as_ptr(), name.len(), &mut js_key)
|
||||
})?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_has_property(self.0.env, self.0.value, js_key, &mut result)
|
||||
|
@ -483,7 +483,7 @@ macro_rules! impl_object_methods {
|
|||
sys::napi_define_properties(
|
||||
self.0.env,
|
||||
self.0.value,
|
||||
properties.len() as _,
|
||||
properties.len(),
|
||||
properties
|
||||
.iter()
|
||||
.map(|property| property.raw())
|
||||
|
|
|
@ -55,7 +55,7 @@ impl JsString {
|
|||
self.0.env,
|
||||
self.0.value,
|
||||
buf_ptr,
|
||||
len as _,
|
||||
len,
|
||||
&mut written_char_count,
|
||||
)
|
||||
})?;
|
||||
|
@ -65,11 +65,7 @@ impl JsString {
|
|||
Ok(JsStringUtf8 {
|
||||
inner: self,
|
||||
buf: mem::ManuallyDrop::new(unsafe {
|
||||
Vec::from_raw_parts(
|
||||
buf_ptr as *mut _,
|
||||
written_char_count as _,
|
||||
written_char_count as _,
|
||||
)
|
||||
Vec::from_raw_parts(buf_ptr, written_char_count, written_char_count)
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
@ -81,7 +77,7 @@ impl JsString {
|
|||
|
||||
#[inline]
|
||||
pub fn into_utf16(self) -> Result<JsStringUtf16> {
|
||||
let mut written_char_count = 0;
|
||||
let mut written_char_count = 0usize;
|
||||
let len = self.utf16_len()? + 1;
|
||||
let mut result = Vec::with_capacity(len);
|
||||
let buf_ptr = result.as_mut_ptr();
|
||||
|
@ -90,7 +86,7 @@ impl JsString {
|
|||
self.0.env,
|
||||
self.0.value,
|
||||
buf_ptr,
|
||||
len as _,
|
||||
len,
|
||||
&mut written_char_count,
|
||||
)
|
||||
})?;
|
||||
|
@ -99,7 +95,7 @@ impl JsString {
|
|||
Ok(JsStringUtf16 {
|
||||
inner: self,
|
||||
buf: mem::ManuallyDrop::new(unsafe {
|
||||
Vec::from_raw_parts(buf_ptr, written_char_count as _, written_char_count as _)
|
||||
Vec::from_raw_parts(buf_ptr, written_char_count, written_char_count)
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
@ -111,7 +107,7 @@ impl JsString {
|
|||
|
||||
#[inline]
|
||||
pub fn into_latin1(self) -> Result<JsStringLatin1> {
|
||||
let mut written_char_count: u64 = 0;
|
||||
let mut written_char_count = 0usize;
|
||||
let len = self.latin1_len()? + 1;
|
||||
let mut result = Vec::with_capacity(len);
|
||||
let buf_ptr = result.as_mut_ptr();
|
||||
|
@ -120,8 +116,8 @@ impl JsString {
|
|||
self.0.env,
|
||||
self.0.value,
|
||||
buf_ptr,
|
||||
len as _,
|
||||
&mut written_char_count as *mut u64 as *mut _,
|
||||
len,
|
||||
&mut written_char_count,
|
||||
)
|
||||
})?;
|
||||
|
||||
|
@ -130,11 +126,7 @@ impl JsString {
|
|||
Ok(JsStringLatin1 {
|
||||
inner: self,
|
||||
buf: mem::ManuallyDrop::new(unsafe {
|
||||
Vec::from_raw_parts(
|
||||
buf_ptr as *mut _,
|
||||
written_char_count as _,
|
||||
written_char_count as _,
|
||||
)
|
||||
Vec::from_raw_parts(buf_ptr as *mut _, written_char_count, written_char_count)
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,24 +1,27 @@
|
|||
use std::convert::TryFrom;
|
||||
use std::ffi::CStr;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::raw::c_char;
|
||||
use std::str;
|
||||
|
||||
use crate::{Error, JsString, Result, Status};
|
||||
|
||||
pub struct JsStringUtf8 {
|
||||
pub(crate) inner: JsString,
|
||||
pub(crate) buf: ManuallyDrop<Vec<u8>>,
|
||||
pub(crate) buf: ManuallyDrop<Vec<c_char>>,
|
||||
}
|
||||
|
||||
impl JsStringUtf8 {
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> Result<&str> {
|
||||
str::from_utf8(self.buf.as_slice())
|
||||
unsafe { CStr::from_ptr(self.buf.as_ptr()) }
|
||||
.to_str()
|
||||
.map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_slice(&self) -> &[u8] {
|
||||
&self.buf
|
||||
unsafe { CStr::from_ptr(self.buf.as_ptr()) }.to_bytes()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
|
@ -27,7 +27,7 @@ impl<T, V: NapiValue> FuturePromise<T, V> {
|
|||
sys::napi_create_string_utf8(
|
||||
env,
|
||||
s.as_ptr() as *const c_char,
|
||||
s.len() as _,
|
||||
s.len(),
|
||||
&mut async_resource_name,
|
||||
)
|
||||
})?;
|
||||
|
@ -55,7 +55,7 @@ impl<T, V: NapiValue> FuturePromise<T, V> {
|
|||
ptr::null_mut(),
|
||||
async_resource_name,
|
||||
0,
|
||||
initial_thread_count as _,
|
||||
initial_thread_count,
|
||||
ptr::null_mut(),
|
||||
None,
|
||||
self_ref as *mut _ as *mut c_void,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::convert::Into;
|
||||
use std::ffi::CString;
|
||||
use std::marker::PhantomData;
|
||||
use std::os::raw::{c_char, c_void};
|
||||
use std::os::raw::c_void;
|
||||
use std::ptr;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
@ -104,16 +105,13 @@ impl<T: 'static> ThreadsafeFunction<T> {
|
|||
) -> Result<Self> {
|
||||
let mut async_resource_name = ptr::null_mut();
|
||||
let s = "napi_rs_threadsafe_function";
|
||||
let len = s.len();
|
||||
let s = CString::new(s)?;
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_string_utf8(
|
||||
env,
|
||||
s.as_ptr() as *const c_char,
|
||||
s.len() as _,
|
||||
&mut async_resource_name,
|
||||
)
|
||||
sys::napi_create_string_utf8(env, s.as_ptr(), len, &mut async_resource_name)
|
||||
})?;
|
||||
|
||||
let initial_thread_count = 1;
|
||||
let initial_thread_count = 1usize;
|
||||
let mut raw_tsfn = ptr::null_mut();
|
||||
let context = ThreadSafeContext(Box::from(callback));
|
||||
let ptr = Box::into_raw(Box::new(context)) as *mut _;
|
||||
|
@ -123,7 +121,7 @@ impl<T: 'static> ThreadsafeFunction<T> {
|
|||
func.0.value,
|
||||
ptr::null_mut(),
|
||||
async_resource_name,
|
||||
max_queue_size as _,
|
||||
max_queue_size,
|
||||
initial_thread_count,
|
||||
ptr,
|
||||
Some(thread_finalize_cb::<T, V>),
|
||||
|
@ -280,7 +278,7 @@ unsafe extern "C" fn call_js_cb<T: 'static, V: NapiValue>(
|
|||
raw_env,
|
||||
recv,
|
||||
js_callback,
|
||||
args_length as _,
|
||||
args_length,
|
||||
args.as_ptr(),
|
||||
ptr::null_mut(),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue