Merge pull request #333 from napi-rs/reduce-useless-type-cast

refactor(napi): reduce useless type cast
This commit is contained in:
LongYinan 2020-12-02 20:09:11 +08:00 committed by GitHub
commit 834a7b5bc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 57 additions and 73 deletions

View file

@ -39,12 +39,7 @@ pub fn run<T: Task>(env: &Env, task: T) -> Result<AsyncWorkPromise<'_>> {
let mut raw_name = ptr::null_mut(); let mut raw_name = ptr::null_mut();
let s = "napi_rs_async_work"; let s = "napi_rs_async_work";
check_status!(unsafe { check_status!(unsafe {
sys::napi_create_string_utf8( sys::napi_create_string_utf8(env.0, s.as_ptr() as *const c_char, s.len(), &mut raw_name)
env.0,
s.as_ptr() as *const c_char,
s.len() as _,
&mut raw_name,
)
})?; })?;
let result = Box::leak(Box::new(AsyncWork { let result = Box::leak(Box::new(AsyncWork {
inner_task: task, inner_task: task,

View file

@ -151,12 +151,12 @@ impl Env {
true => 1, true => 1,
false => 0, false => 0,
}, },
len as _, len,
words.as_ptr(), words.as_ptr(),
&mut raw_value, &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] #[inline]
@ -182,9 +182,7 @@ impl Env {
#[inline] #[inline]
fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result<JsString> { fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result<JsString> {
let mut raw_value = ptr::null_mut(); let mut raw_value = ptr::null_mut();
check_status!(unsafe { check_status!(unsafe { sys::napi_create_string_utf8(self.0, data_ptr, len, &mut raw_value) })?;
sys::napi_create_string_utf8(self.0, data_ptr, len as _, &mut raw_value)
})?;
Ok(unsafe { JsString::from_raw_unchecked(self.0, 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> { pub fn create_string_utf16(&self, chars: &[u16]) -> Result<JsString> {
let mut raw_value = ptr::null_mut(); let mut raw_value = ptr::null_mut();
check_status!(unsafe { 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) }) Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) })
} }
@ -204,7 +202,7 @@ impl Env {
sys::napi_create_string_latin1( sys::napi_create_string_latin1(
self.0, self.0,
chars.as_ptr() as *const _, chars.as_ptr() as *const _,
chars.len() as _, chars.len(),
&mut raw_value, &mut raw_value,
) )
})?; })?;
@ -251,9 +249,7 @@ impl Env {
#[inline] #[inline]
pub fn create_array_with_length(&self, length: usize) -> Result<JsObject> { pub fn create_array_with_length(&self, length: usize) -> Result<JsObject> {
let mut raw_value = ptr::null_mut(); let mut raw_value = ptr::null_mut();
check_status!(unsafe { check_status!(unsafe { sys::napi_create_array_with_length(self.0, length, &mut raw_value) })?;
sys::napi_create_array_with_length(self.0, length as _, &mut raw_value)
})?;
Ok(unsafe { JsObject::from_raw_unchecked(self.0, 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: Vec<u8> = Vec::with_capacity(length);
let mut data_ptr = data.as_mut_ptr() as *mut c_void; let mut data_ptr = data.as_mut_ptr() as *mut c_void;
check_status!(unsafe { 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( Ok(JsBufferValue::new(
@ -287,7 +283,7 @@ impl Env {
check_status!(unsafe { check_status!(unsafe {
sys::napi_create_external_buffer( sys::napi_create_external_buffer(
self.0, self.0,
length as _, length,
data_ptr as *mut c_void, data_ptr as *mut c_void,
Some(drop_buffer), Some(drop_buffer),
&mut length as *mut usize as *mut _, &mut length as *mut usize as *mut _,
@ -322,7 +318,7 @@ impl Env {
check_status!(unsafe { check_status!(unsafe {
sys::napi_create_buffer_copy( sys::napi_create_buffer_copy(
self.0, self.0,
length as _, length,
data_ptr as *mut c_void, data_ptr as *mut c_void,
&mut copy_data, &mut copy_data,
&mut raw_value, &mut raw_value,
@ -344,7 +340,7 @@ impl Env {
let mut data: Vec<u8> = Vec::with_capacity(length as usize); let mut data: Vec<u8> = Vec::with_capacity(length as usize);
let mut data_ptr = data.as_mut_ptr() as *mut c_void; let mut data_ptr = data.as_mut_ptr() as *mut c_void;
check_status!(unsafe { 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( Ok(JsArrayBufferValue::new(
@ -362,7 +358,7 @@ impl Env {
sys::napi_create_external_arraybuffer( sys::napi_create_external_arraybuffer(
self.0, self.0,
data_ptr as *mut c_void, data_ptr as *mut c_void,
length as _, length,
Some(drop_buffer), Some(drop_buffer),
&mut length as *mut usize as *mut c_void, &mut length as *mut usize as *mut c_void,
&mut raw_value, &mut raw_value,
@ -512,10 +508,10 @@ impl Env {
sys::napi_define_class( sys::napi_define_class(
self.0, self.0,
name.as_ptr() as *const c_char, name.as_ptr() as *const c_char,
name.len() as _, name.len(),
Some(constructor_cb), Some(constructor_cb),
ptr::null_mut(), ptr::null_mut(),
raw_properties.len() as _, raw_properties.len(),
raw_properties.as_ptr(), raw_properties.as_ptr(),
&mut raw_result, &mut raw_result,
) )

View file

@ -116,17 +116,17 @@ impl JsArrayBuffer {
pub fn into_typedarray( pub fn into_typedarray(
self, self,
typedarray_type: TypedArrayType, typedarray_type: TypedArrayType,
length: u64, length: usize,
byte_offset: u64, byte_offset: usize,
) -> Result<JsTypedArray> { ) -> Result<JsTypedArray> {
let mut typedarray_value = ptr::null_mut(); let mut typedarray_value = ptr::null_mut();
check_status!(unsafe { check_status!(unsafe {
sys::napi_create_typedarray( sys::napi_create_typedarray(
self.0.env, self.0.env,
typedarray_type.into(), typedarray_type.into(),
length as _, length,
self.0.value, self.0.value,
byte_offset as _, byte_offset,
&mut typedarray_value, &mut typedarray_value,
) )
})?; })?;
@ -138,14 +138,14 @@ impl JsArrayBuffer {
} }
#[inline] #[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(); let mut dataview_value = ptr::null_mut();
check_status!(unsafe { check_status!(unsafe {
sys::napi_create_dataview( sys::napi_create_dataview(
self.0.env, self.0.env,
length as _, length,
self.0.value, self.0.value,
byte_offset as _, byte_offset,
&mut dataview_value, &mut dataview_value,
) )
})?; })?;

View file

@ -6,7 +6,7 @@ use crate::{check_status, sys, Result};
pub struct JsBigint { pub struct JsBigint {
pub(crate) raw: Value, pub(crate) raw: Value,
pub word_count: u64, pub word_count: usize,
} }
impl JsBigint { impl JsBigint {
@ -14,7 +14,7 @@ impl JsBigint {
pub(crate) fn from_raw_unchecked( pub(crate) fn from_raw_unchecked(
env: sys::napi_env, env: sys::napi_env,
value: sys::napi_value, value: sys::napi_value,
word_count: u64, word_count: usize,
) -> Self { ) -> Self {
Self { Self {
raw: Value { raw: Value {
@ -128,12 +128,12 @@ impl NapiValue for JsBigint {
} }
unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<Self> { 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( check_status!(sys::napi_get_value_bigint_words(
env, env,
value, value,
ptr::null_mut(), ptr::null_mut(),
&mut word_count as *mut u64 as *mut _, &mut word_count,
ptr::null_mut(), ptr::null_mut(),
))?; ))?;
Ok(JsBigint { Ok(JsBigint {
@ -147,12 +147,12 @@ impl NapiValue for JsBigint {
} }
unsafe fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self { 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( let status = sys::napi_get_value_bigint_words(
env, env,
value, value,
ptr::null_mut(), ptr::null_mut(),
&mut word_count as *mut u64 as *mut _, &mut word_count,
ptr::null_mut(), ptr::null_mut(),
); );
debug_assert!( debug_assert!(
@ -200,7 +200,7 @@ impl JsBigint {
self.raw.env, self.raw.env,
self.raw.value, self.raw.value,
&mut sign_bit, &mut sign_bit,
word_count as *mut u64 as *mut _, word_count,
words.as_mut_ptr(), words.as_mut_ptr(),
) )
})?; })?;

View file

@ -43,7 +43,7 @@ impl JsFunction {
self.0.env, self.0.env,
raw_this, raw_this,
self.0.value, self.0.value,
args.len() as _, args.len(),
raw_args.as_ptr(), raw_args.as_ptr(),
&mut return_value, &mut return_value,
) )
@ -70,7 +70,7 @@ impl JsFunction {
sys::napi_new_instance( sys::napi_new_instance(
self.0.env, self.0.env,
self.0.value, self.0.value,
length as _, length,
raw_args.as_ptr(), raw_args.as_ptr(),
&mut js_instance, &mut js_instance,
) )

View file

@ -369,7 +369,7 @@ macro_rules! impl_object_methods {
let mut js_key = ptr::null_mut(); let mut js_key = ptr::null_mut();
let mut result = false; let mut result = false;
check_status!(unsafe { 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 { check_status!(unsafe {
sys::napi_has_property(self.0.env, self.0.value, js_key, &mut result) 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( sys::napi_define_properties(
self.0.env, self.0.env,
self.0.value, self.0.value,
properties.len() as _, properties.len(),
properties properties
.iter() .iter()
.map(|property| property.raw()) .map(|property| property.raw())

View file

@ -55,7 +55,7 @@ impl JsString {
self.0.env, self.0.env,
self.0.value, self.0.value,
buf_ptr, buf_ptr,
len as _, len,
&mut written_char_count, &mut written_char_count,
) )
})?; })?;
@ -65,11 +65,7 @@ impl JsString {
Ok(JsStringUtf8 { Ok(JsStringUtf8 {
inner: self, inner: self,
buf: mem::ManuallyDrop::new(unsafe { buf: mem::ManuallyDrop::new(unsafe {
Vec::from_raw_parts( Vec::from_raw_parts(buf_ptr, written_char_count, written_char_count)
buf_ptr as *mut _,
written_char_count as _,
written_char_count as _,
)
}), }),
}) })
} }
@ -81,7 +77,7 @@ impl JsString {
#[inline] #[inline]
pub fn into_utf16(self) -> Result<JsStringUtf16> { 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 len = self.utf16_len()? + 1;
let mut result = Vec::with_capacity(len); let mut result = Vec::with_capacity(len);
let buf_ptr = result.as_mut_ptr(); let buf_ptr = result.as_mut_ptr();
@ -90,7 +86,7 @@ impl JsString {
self.0.env, self.0.env,
self.0.value, self.0.value,
buf_ptr, buf_ptr,
len as _, len,
&mut written_char_count, &mut written_char_count,
) )
})?; })?;
@ -99,7 +95,7 @@ impl JsString {
Ok(JsStringUtf16 { Ok(JsStringUtf16 {
inner: self, inner: self,
buf: mem::ManuallyDrop::new(unsafe { 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] #[inline]
pub fn into_latin1(self) -> Result<JsStringLatin1> { 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 len = self.latin1_len()? + 1;
let mut result = Vec::with_capacity(len); let mut result = Vec::with_capacity(len);
let buf_ptr = result.as_mut_ptr(); let buf_ptr = result.as_mut_ptr();
@ -120,8 +116,8 @@ impl JsString {
self.0.env, self.0.env,
self.0.value, self.0.value,
buf_ptr, buf_ptr,
len as _, len,
&mut written_char_count as *mut u64 as *mut _, &mut written_char_count,
) )
})?; })?;
@ -130,11 +126,7 @@ impl JsString {
Ok(JsStringLatin1 { Ok(JsStringLatin1 {
inner: self, inner: self,
buf: mem::ManuallyDrop::new(unsafe { buf: mem::ManuallyDrop::new(unsafe {
Vec::from_raw_parts( Vec::from_raw_parts(buf_ptr as *mut _, written_char_count, written_char_count)
buf_ptr as *mut _,
written_char_count as _,
written_char_count as _,
)
}), }),
}) })
} }

View file

@ -1,24 +1,27 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use std::ffi::CStr;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::os::raw::c_char;
use std::str; use std::str;
use crate::{Error, JsString, Result, Status}; use crate::{Error, JsString, Result, Status};
pub struct JsStringUtf8 { pub struct JsStringUtf8 {
pub(crate) inner: JsString, pub(crate) inner: JsString,
pub(crate) buf: ManuallyDrop<Vec<u8>>, pub(crate) buf: ManuallyDrop<Vec<c_char>>,
} }
impl JsStringUtf8 { impl JsStringUtf8 {
#[inline] #[inline]
pub fn as_str(&self) -> Result<&str> { 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))) .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))
} }
#[inline] #[inline]
pub fn as_slice(&self) -> &[u8] { pub fn as_slice(&self) -> &[u8] {
&self.buf unsafe { CStr::from_ptr(self.buf.as_ptr()) }.to_bytes()
} }
#[inline] #[inline]

View file

@ -27,7 +27,7 @@ impl<T, V: NapiValue> FuturePromise<T, V> {
sys::napi_create_string_utf8( sys::napi_create_string_utf8(
env, env,
s.as_ptr() as *const c_char, s.as_ptr() as *const c_char,
s.len() as _, s.len(),
&mut async_resource_name, &mut async_resource_name,
) )
})?; })?;
@ -55,7 +55,7 @@ impl<T, V: NapiValue> FuturePromise<T, V> {
ptr::null_mut(), ptr::null_mut(),
async_resource_name, async_resource_name,
0, 0,
initial_thread_count as _, initial_thread_count,
ptr::null_mut(), ptr::null_mut(),
None, None,
self_ref as *mut _ as *mut c_void, self_ref as *mut _ as *mut c_void,

View file

@ -1,6 +1,7 @@
use std::convert::Into; use std::convert::Into;
use std::ffi::CString;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::os::raw::{c_char, c_void}; use std::os::raw::c_void;
use std::ptr; use std::ptr;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
@ -104,16 +105,13 @@ impl<T: 'static> ThreadsafeFunction<T> {
) -> Result<Self> { ) -> Result<Self> {
let mut async_resource_name = ptr::null_mut(); let mut async_resource_name = ptr::null_mut();
let s = "napi_rs_threadsafe_function"; let s = "napi_rs_threadsafe_function";
let len = s.len();
let s = CString::new(s)?;
check_status!(unsafe { check_status!(unsafe {
sys::napi_create_string_utf8( sys::napi_create_string_utf8(env, s.as_ptr(), len, &mut async_resource_name)
env,
s.as_ptr() as *const c_char,
s.len() as _,
&mut async_resource_name,
)
})?; })?;
let initial_thread_count = 1; let initial_thread_count = 1usize;
let mut raw_tsfn = ptr::null_mut(); let mut raw_tsfn = ptr::null_mut();
let context = ThreadSafeContext(Box::from(callback)); let context = ThreadSafeContext(Box::from(callback));
let ptr = Box::into_raw(Box::new(context)) as *mut _; let ptr = Box::into_raw(Box::new(context)) as *mut _;
@ -123,7 +121,7 @@ impl<T: 'static> ThreadsafeFunction<T> {
func.0.value, func.0.value,
ptr::null_mut(), ptr::null_mut(),
async_resource_name, async_resource_name,
max_queue_size as _, max_queue_size,
initial_thread_count, initial_thread_count,
ptr, ptr,
Some(thread_finalize_cb::<T, V>), Some(thread_finalize_cb::<T, V>),
@ -280,7 +278,7 @@ unsafe extern "C" fn call_js_cb<T: 'static, V: NapiValue>(
raw_env, raw_env,
recv, recv,
js_callback, js_callback,
args_length as _, args_length,
args.as_ptr(), args.as_ptr(),
ptr::null_mut(), ptr::null_mut(),
); );