feat(sys): remove rust enum in sys

This commit is contained in:
LongYinan 2021-11-29 12:52:42 +08:00
parent a25f0b990c
commit a7067d6732
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
7 changed files with 133 additions and 108 deletions

View file

@ -88,19 +88,19 @@ pub enum TypedArrayType {
impl From<sys::napi_typedarray_type> for TypedArrayType {
fn from(value: sys::napi_typedarray_type) -> Self {
match value {
sys::TypedarrayType::napi_int8_array => Self::Int8,
sys::TypedarrayType::napi_uint8_array => Self::Uint8,
sys::TypedarrayType::napi_uint8_clamped_array => Self::Uint8Clamped,
sys::TypedarrayType::napi_int16_array => Self::Int16,
sys::TypedarrayType::napi_uint16_array => Self::Uint16,
sys::TypedarrayType::napi_int32_array => Self::Int32,
sys::TypedarrayType::napi_uint32_array => Self::Uint32,
sys::TypedarrayType::napi_float32_array => Self::Float32,
sys::TypedarrayType::napi_float64_array => Self::Float64,
sys::TypedarrayType::int8_array => Self::Int8,
sys::TypedarrayType::uint8_array => Self::Uint8,
sys::TypedarrayType::uint8_clamped_array => Self::Uint8Clamped,
sys::TypedarrayType::int16_array => Self::Int16,
sys::TypedarrayType::uint16_array => Self::Uint16,
sys::TypedarrayType::int32_array => Self::Int32,
sys::TypedarrayType::uint32_array => Self::Uint32,
sys::TypedarrayType::float32_array => Self::Float32,
sys::TypedarrayType::float64_array => Self::Float64,
#[cfg(feature = "napi6")]
sys::TypedarrayType::napi_bigint64_array => Self::BigInt64,
sys::TypedarrayType::bigint64_array => Self::BigInt64,
#[cfg(feature = "napi6")]
sys::TypedarrayType::napi_biguint64_array => Self::BigUint64,
sys::TypedarrayType::biguint64_array => Self::BigUint64,
_ => Self::Unknown,
}
}

View file

@ -98,8 +98,12 @@ impl TryFrom<sys::napi_key_collection_mode> for KeyCollectionMode {
fn try_from(value: sys::napi_key_collection_mode) -> Result<Self> {
match value {
sys::napi_key_collection_mode::napi_key_include_prototypes => Ok(Self::IncludePrototypes),
sys::napi_key_collection_mode::napi_key_own_only => Ok(Self::OwnOnly),
sys::KeyCollectionMode::include_prototypes => Ok(Self::IncludePrototypes),
sys::KeyCollectionMode::own_only => Ok(Self::OwnOnly),
_ => Err(Error::new(
crate::Status::InvalidArg,
format!("Invalid key collection mode: {}", value),
)),
}
}
}
@ -108,10 +112,8 @@ impl TryFrom<sys::napi_key_collection_mode> for KeyCollectionMode {
impl From<KeyCollectionMode> for sys::napi_key_collection_mode {
fn from(value: KeyCollectionMode) -> Self {
match value {
KeyCollectionMode::IncludePrototypes => {
sys::napi_key_collection_mode::napi_key_include_prototypes
}
KeyCollectionMode::OwnOnly => sys::napi_key_collection_mode::napi_key_own_only,
KeyCollectionMode::IncludePrototypes => sys::KeyCollectionMode::include_prototypes,
KeyCollectionMode::OwnOnly => sys::KeyCollectionMode::own_only,
}
}
}
@ -132,12 +134,16 @@ impl TryFrom<sys::napi_key_filter> for KeyFilter {
fn try_from(value: sys::napi_key_filter) -> Result<Self> {
match value {
sys::napi_key_filter::napi_key_all_properties => Ok(Self::AllProperties),
sys::napi_key_filter::napi_key_writable => Ok(Self::Writable),
sys::napi_key_filter::napi_key_enumerable => Ok(Self::Enumerable),
sys::napi_key_filter::napi_key_configurable => Ok(Self::Configurable),
sys::napi_key_filter::napi_key_skip_strings => Ok(Self::SkipStrings),
sys::napi_key_filter::napi_key_skip_symbols => Ok(Self::SkipSymbols),
sys::KeyFilter::all_properties => Ok(Self::AllProperties),
sys::KeyFilter::writable => Ok(Self::Writable),
sys::KeyFilter::enumerable => Ok(Self::Enumerable),
sys::KeyFilter::configurable => Ok(Self::Configurable),
sys::KeyFilter::skip_strings => Ok(Self::SkipStrings),
sys::KeyFilter::skip_symbols => Ok(Self::SkipSymbols),
_ => Err(Error::new(
crate::Status::InvalidArg,
format!("Invalid key filter [{}]", value),
)),
}
}
}
@ -146,12 +152,12 @@ impl TryFrom<sys::napi_key_filter> for KeyFilter {
impl From<KeyFilter> for sys::napi_key_filter {
fn from(value: KeyFilter) -> Self {
match value {
KeyFilter::AllProperties => Self::napi_key_all_properties,
KeyFilter::Writable => Self::napi_key_writable,
KeyFilter::Enumerable => Self::napi_key_enumerable,
KeyFilter::Configurable => Self::napi_key_configurable,
KeyFilter::SkipStrings => Self::napi_key_skip_strings,
KeyFilter::SkipSymbols => Self::napi_key_skip_symbols,
KeyFilter::AllProperties => sys::KeyFilter::all_properties,
KeyFilter::Writable => sys::KeyFilter::writable,
KeyFilter::Enumerable => sys::KeyFilter::enumerable,
KeyFilter::Configurable => sys::KeyFilter::configurable,
KeyFilter::SkipStrings => sys::KeyFilter::skip_strings,
KeyFilter::SkipSymbols => sys::KeyFilter::skip_symbols,
}
}
}
@ -168,8 +174,12 @@ impl TryFrom<sys::napi_key_conversion> for KeyConversion {
fn try_from(value: sys::napi_key_conversion) -> Result<Self> {
match value {
sys::napi_key_conversion::napi_key_keep_numbers => Ok(Self::KeepNumbers),
sys::napi_key_conversion::napi_key_numbers_to_strings => Ok(Self::NumbersToStrings),
sys::KeyConversion::keep_numbers => Ok(Self::KeepNumbers),
sys::KeyConversion::numbers_to_strings => Ok(Self::NumbersToStrings),
_ => Err(Error::new(
crate::Status::InvalidArg,
format!("Invalid key conversion [{}]", value),
)),
}
}
}
@ -178,8 +188,8 @@ impl TryFrom<sys::napi_key_conversion> for KeyConversion {
impl From<KeyConversion> for sys::napi_key_conversion {
fn from(value: KeyConversion) -> Self {
match value {
KeyConversion::KeepNumbers => Self::napi_key_keep_numbers,
KeyConversion::NumbersToStrings => Self::napi_key_numbers_to_strings,
KeyConversion::KeepNumbers => sys::KeyConversion::keep_numbers,
KeyConversion::NumbersToStrings => sys::KeyConversion::numbers_to_strings,
}
}
}

View file

@ -14,14 +14,14 @@ pub struct Property {
pub(crate) is_ctor: bool,
}
#[repr(u32)]
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum PropertyAttributes {
Default = sys::napi_property_attributes::napi_default as _,
Writable = sys::napi_property_attributes::napi_writable as _,
Enumerable = sys::napi_property_attributes::napi_enumerable as _,
Configurable = sys::napi_property_attributes::napi_configurable as _,
Static = sys::napi_property_attributes::napi_static as _,
Default = sys::PropertyAttributes::default,
Writable = sys::PropertyAttributes::writable,
Enumerable = sys::PropertyAttributes::enumerable,
Configurable = sys::PropertyAttributes::configurable,
Static = sys::PropertyAttributes::static_,
}
impl Default for PropertyAttributes {
@ -33,11 +33,11 @@ impl Default for PropertyAttributes {
impl From<PropertyAttributes> for sys::napi_property_attributes {
fn from(value: PropertyAttributes) -> Self {
match value {
PropertyAttributes::Default => sys::napi_property_attributes::napi_default,
PropertyAttributes::Writable => sys::napi_property_attributes::napi_writable,
PropertyAttributes::Enumerable => sys::napi_property_attributes::napi_enumerable,
PropertyAttributes::Configurable => sys::napi_property_attributes::napi_configurable,
PropertyAttributes::Static => sys::napi_property_attributes::napi_static,
PropertyAttributes::Default => sys::PropertyAttributes::default,
PropertyAttributes::Writable => sys::PropertyAttributes::writable,
PropertyAttributes::Enumerable => sys::PropertyAttributes::enumerable,
PropertyAttributes::Configurable => sys::PropertyAttributes::configurable,
PropertyAttributes::Static => sys::PropertyAttributes::static_,
}
}
}

View file

@ -78,15 +78,12 @@ pub(crate) async fn resolve_from_future<Data: Send, Fut: Future<Output = Result<
sys::napi_call_threadsafe_function(
tsfn_value.0,
Box::into_raw(Box::from(val)) as *mut c_void,
sys::napi_threadsafe_function_call_mode::napi_tsfn_nonblocking,
sys::ThreadsafeFunctionCallMode::nonblocking,
)
})
.expect("Failed to call thread safe function");
check_status!(unsafe {
sys::napi_release_threadsafe_function(
tsfn_value.0,
sys::napi_threadsafe_function_release_mode::napi_tsfn_release,
)
sys::napi_release_threadsafe_function(tsfn_value.0, sys::ThreadsafeFunctionReleaseMode::release)
})
.expect("Failed to release thread safe function");
}

View file

@ -10,8 +10,6 @@ use std::sync::Arc;
use crate::{check_status, sys, Env, Error, JsError, NapiRaw, Result, Status};
use sys::napi_threadsafe_function_call_mode;
/// ThreadSafeFunction Context object
/// the `value` is the value passed to `call` method
pub struct ThreadSafeCallContext<T: 'static> {
@ -25,15 +23,11 @@ pub enum ThreadsafeFunctionCallMode {
Blocking,
}
impl From<ThreadsafeFunctionCallMode> for napi_threadsafe_function_call_mode {
impl From<ThreadsafeFunctionCallMode> for sys::napi_threadsafe_function_call_mode {
fn from(value: ThreadsafeFunctionCallMode) -> Self {
match value {
ThreadsafeFunctionCallMode::Blocking => {
napi_threadsafe_function_call_mode::napi_tsfn_blocking
}
ThreadsafeFunctionCallMode::NonBlocking => {
napi_threadsafe_function_call_mode::napi_tsfn_nonblocking
}
ThreadsafeFunctionCallMode::Blocking => sys::ThreadsafeFunctionCallMode::blocking,
ThreadsafeFunctionCallMode::NonBlocking => sys::ThreadsafeFunctionCallMode::nonblocking,
}
}
}
@ -261,7 +255,7 @@ impl<T: 'static, ES: ErrorStrategy::T> ThreadsafeFunction<T, ES> {
check_status!(unsafe {
sys::napi_release_threadsafe_function(
self.raw_tsfn,
sys::napi_threadsafe_function_release_mode::napi_tsfn_abort,
sys::ThreadsafeFunctionReleaseMode::abort,
)
})?;
self.aborted.store(true, Ordering::Release);
@ -316,7 +310,7 @@ impl<T: 'static, ES: ErrorStrategy::T> Drop for ThreadsafeFunction<T, ES> {
let release_status = unsafe {
sys::napi_release_threadsafe_function(
self.raw_tsfn,
sys::napi_threadsafe_function_release_mode::napi_tsfn_release,
sys::ThreadsafeFunctionReleaseMode::release,
)
};
assert!(