Merge pull request #887 from napi-rs/remove-enum

feat(sys): remove rust enum in sys
This commit is contained in:
LongYinan 2021-11-29 14:58:05 +08:00 committed by GitHub
commit adfa14cf94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 { impl From<sys::napi_typedarray_type> for TypedArrayType {
fn from(value: sys::napi_typedarray_type) -> Self { fn from(value: sys::napi_typedarray_type) -> Self {
match value { match value {
sys::TypedarrayType::napi_int8_array => Self::Int8, sys::TypedarrayType::int8_array => Self::Int8,
sys::TypedarrayType::napi_uint8_array => Self::Uint8, sys::TypedarrayType::uint8_array => Self::Uint8,
sys::TypedarrayType::napi_uint8_clamped_array => Self::Uint8Clamped, sys::TypedarrayType::uint8_clamped_array => Self::Uint8Clamped,
sys::TypedarrayType::napi_int16_array => Self::Int16, sys::TypedarrayType::int16_array => Self::Int16,
sys::TypedarrayType::napi_uint16_array => Self::Uint16, sys::TypedarrayType::uint16_array => Self::Uint16,
sys::TypedarrayType::napi_int32_array => Self::Int32, sys::TypedarrayType::int32_array => Self::Int32,
sys::TypedarrayType::napi_uint32_array => Self::Uint32, sys::TypedarrayType::uint32_array => Self::Uint32,
sys::TypedarrayType::napi_float32_array => Self::Float32, sys::TypedarrayType::float32_array => Self::Float32,
sys::TypedarrayType::napi_float64_array => Self::Float64, sys::TypedarrayType::float64_array => Self::Float64,
#[cfg(feature = "napi6")] #[cfg(feature = "napi6")]
sys::TypedarrayType::napi_bigint64_array => Self::BigInt64, sys::TypedarrayType::bigint64_array => Self::BigInt64,
#[cfg(feature = "napi6")] #[cfg(feature = "napi6")]
sys::TypedarrayType::napi_biguint64_array => Self::BigUint64, sys::TypedarrayType::biguint64_array => Self::BigUint64,
_ => Self::Unknown, _ => 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> { fn try_from(value: sys::napi_key_collection_mode) -> Result<Self> {
match value { match value {
sys::napi_key_collection_mode::napi_key_include_prototypes => Ok(Self::IncludePrototypes), sys::KeyCollectionMode::include_prototypes => Ok(Self::IncludePrototypes),
sys::napi_key_collection_mode::napi_key_own_only => Ok(Self::OwnOnly), 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 { impl From<KeyCollectionMode> for sys::napi_key_collection_mode {
fn from(value: KeyCollectionMode) -> Self { fn from(value: KeyCollectionMode) -> Self {
match value { match value {
KeyCollectionMode::IncludePrototypes => { KeyCollectionMode::IncludePrototypes => sys::KeyCollectionMode::include_prototypes,
sys::napi_key_collection_mode::napi_key_include_prototypes KeyCollectionMode::OwnOnly => sys::KeyCollectionMode::own_only,
}
KeyCollectionMode::OwnOnly => sys::napi_key_collection_mode::napi_key_own_only,
} }
} }
} }
@ -132,12 +134,16 @@ impl TryFrom<sys::napi_key_filter> for KeyFilter {
fn try_from(value: sys::napi_key_filter) -> Result<Self> { fn try_from(value: sys::napi_key_filter) -> Result<Self> {
match value { match value {
sys::napi_key_filter::napi_key_all_properties => Ok(Self::AllProperties), sys::KeyFilter::all_properties => Ok(Self::AllProperties),
sys::napi_key_filter::napi_key_writable => Ok(Self::Writable), sys::KeyFilter::writable => Ok(Self::Writable),
sys::napi_key_filter::napi_key_enumerable => Ok(Self::Enumerable), sys::KeyFilter::enumerable => Ok(Self::Enumerable),
sys::napi_key_filter::napi_key_configurable => Ok(Self::Configurable), sys::KeyFilter::configurable => Ok(Self::Configurable),
sys::napi_key_filter::napi_key_skip_strings => Ok(Self::SkipStrings), sys::KeyFilter::skip_strings => Ok(Self::SkipStrings),
sys::napi_key_filter::napi_key_skip_symbols => Ok(Self::SkipSymbols), 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 { impl From<KeyFilter> for sys::napi_key_filter {
fn from(value: KeyFilter) -> Self { fn from(value: KeyFilter) -> Self {
match value { match value {
KeyFilter::AllProperties => Self::napi_key_all_properties, KeyFilter::AllProperties => sys::KeyFilter::all_properties,
KeyFilter::Writable => Self::napi_key_writable, KeyFilter::Writable => sys::KeyFilter::writable,
KeyFilter::Enumerable => Self::napi_key_enumerable, KeyFilter::Enumerable => sys::KeyFilter::enumerable,
KeyFilter::Configurable => Self::napi_key_configurable, KeyFilter::Configurable => sys::KeyFilter::configurable,
KeyFilter::SkipStrings => Self::napi_key_skip_strings, KeyFilter::SkipStrings => sys::KeyFilter::skip_strings,
KeyFilter::SkipSymbols => Self::napi_key_skip_symbols, 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> { fn try_from(value: sys::napi_key_conversion) -> Result<Self> {
match value { match value {
sys::napi_key_conversion::napi_key_keep_numbers => Ok(Self::KeepNumbers), sys::KeyConversion::keep_numbers => Ok(Self::KeepNumbers),
sys::napi_key_conversion::napi_key_numbers_to_strings => Ok(Self::NumbersToStrings), 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 { impl From<KeyConversion> for sys::napi_key_conversion {
fn from(value: KeyConversion) -> Self { fn from(value: KeyConversion) -> Self {
match value { match value {
KeyConversion::KeepNumbers => Self::napi_key_keep_numbers, KeyConversion::KeepNumbers => sys::KeyConversion::keep_numbers,
KeyConversion::NumbersToStrings => Self::napi_key_numbers_to_strings, KeyConversion::NumbersToStrings => sys::KeyConversion::numbers_to_strings,
} }
} }
} }

View file

@ -14,14 +14,14 @@ pub struct Property {
pub(crate) is_ctor: bool, pub(crate) is_ctor: bool,
} }
#[repr(u32)] #[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum PropertyAttributes { pub enum PropertyAttributes {
Default = sys::napi_property_attributes::napi_default as _, Default = sys::PropertyAttributes::default,
Writable = sys::napi_property_attributes::napi_writable as _, Writable = sys::PropertyAttributes::writable,
Enumerable = sys::napi_property_attributes::napi_enumerable as _, Enumerable = sys::PropertyAttributes::enumerable,
Configurable = sys::napi_property_attributes::napi_configurable as _, Configurable = sys::PropertyAttributes::configurable,
Static = sys::napi_property_attributes::napi_static as _, Static = sys::PropertyAttributes::static_,
} }
impl Default for PropertyAttributes { impl Default for PropertyAttributes {
@ -33,11 +33,11 @@ impl Default for PropertyAttributes {
impl From<PropertyAttributes> for sys::napi_property_attributes { impl From<PropertyAttributes> for sys::napi_property_attributes {
fn from(value: PropertyAttributes) -> Self { fn from(value: PropertyAttributes) -> Self {
match value { match value {
PropertyAttributes::Default => sys::napi_property_attributes::napi_default, PropertyAttributes::Default => sys::PropertyAttributes::default,
PropertyAttributes::Writable => sys::napi_property_attributes::napi_writable, PropertyAttributes::Writable => sys::PropertyAttributes::writable,
PropertyAttributes::Enumerable => sys::napi_property_attributes::napi_enumerable, PropertyAttributes::Enumerable => sys::PropertyAttributes::enumerable,
PropertyAttributes::Configurable => sys::napi_property_attributes::napi_configurable, PropertyAttributes::Configurable => sys::PropertyAttributes::configurable,
PropertyAttributes::Static => sys::napi_property_attributes::napi_static, 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( sys::napi_call_threadsafe_function(
tsfn_value.0, tsfn_value.0,
Box::into_raw(Box::from(val)) as *mut c_void, 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"); .expect("Failed to call thread safe function");
check_status!(unsafe { check_status!(unsafe {
sys::napi_release_threadsafe_function( sys::napi_release_threadsafe_function(tsfn_value.0, sys::ThreadsafeFunctionReleaseMode::release)
tsfn_value.0,
sys::napi_threadsafe_function_release_mode::napi_tsfn_release,
)
}) })
.expect("Failed to release thread safe function"); .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 crate::{check_status, sys, Env, Error, JsError, NapiRaw, Result, Status};
use sys::napi_threadsafe_function_call_mode;
/// ThreadSafeFunction Context object /// ThreadSafeFunction Context object
/// the `value` is the value passed to `call` method /// the `value` is the value passed to `call` method
pub struct ThreadSafeCallContext<T: 'static> { pub struct ThreadSafeCallContext<T: 'static> {
@ -25,15 +23,11 @@ pub enum ThreadsafeFunctionCallMode {
Blocking, 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 { fn from(value: ThreadsafeFunctionCallMode) -> Self {
match value { match value {
ThreadsafeFunctionCallMode::Blocking => { ThreadsafeFunctionCallMode::Blocking => sys::ThreadsafeFunctionCallMode::blocking,
napi_threadsafe_function_call_mode::napi_tsfn_blocking ThreadsafeFunctionCallMode::NonBlocking => sys::ThreadsafeFunctionCallMode::nonblocking,
}
ThreadsafeFunctionCallMode::NonBlocking => {
napi_threadsafe_function_call_mode::napi_tsfn_nonblocking
}
} }
} }
} }
@ -261,7 +255,7 @@ impl<T: 'static, ES: ErrorStrategy::T> ThreadsafeFunction<T, ES> {
check_status!(unsafe { check_status!(unsafe {
sys::napi_release_threadsafe_function( sys::napi_release_threadsafe_function(
self.raw_tsfn, self.raw_tsfn,
sys::napi_threadsafe_function_release_mode::napi_tsfn_abort, sys::ThreadsafeFunctionReleaseMode::abort,
) )
})?; })?;
self.aborted.store(true, Ordering::Release); 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 { let release_status = unsafe {
sys::napi_release_threadsafe_function( sys::napi_release_threadsafe_function(
self.raw_tsfn, self.raw_tsfn,
sys::napi_threadsafe_function_release_mode::napi_tsfn_release, sys::ThreadsafeFunctionReleaseMode::release,
) )
}; };
assert!( assert!(

View file

@ -19,6 +19,7 @@ napi5 = ["napi4"]
napi6 = ["napi5"] napi6 = ["napi5"]
napi7 = ["napi6"] napi7 = ["napi6"]
napi8 = ["napi7"] napi8 = ["napi7"]
experimental = []
[package.metadata.workspaces] [package.metadata.workspaces]
independent = true independent = true

View file

@ -57,17 +57,19 @@ pub struct uv_loop_s {
} }
pub type napi_deferred = *mut napi_deferred__; pub type napi_deferred = *mut napi_deferred__;
#[repr(C)] pub type napi_property_attributes = i32;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum napi_property_attributes { pub mod PropertyAttributes {
napi_default = 0, use super::napi_property_attributes;
napi_writable = 1 << 0,
napi_enumerable = 1 << 1, pub const default: napi_property_attributes = 0;
napi_configurable = 1 << 2, pub const writable: napi_property_attributes = 1 << 0;
pub const enumerable: napi_property_attributes = 1 << 1;
pub const configurable: napi_property_attributes = 1 << 2;
// Used with napi_define_class to distinguish static properties // Used with napi_define_class to distinguish static properties
// from instance properties. Ignored by napi_define_properties. // from instance properties. Ignored by napi_define_properties.
napi_static = 1 << 10, pub const static_: napi_property_attributes = 1 << 10;
} }
pub type napi_valuetype = i32; pub type napi_valuetype = i32;
@ -89,19 +91,19 @@ pub mod ValueType {
pub type napi_typedarray_type = i32; pub type napi_typedarray_type = i32;
pub mod TypedarrayType { pub mod TypedarrayType {
pub const napi_int8_array: i32 = 0; pub const int8_array: i32 = 0;
pub const napi_uint8_array: i32 = 1; pub const uint8_array: i32 = 1;
pub const napi_uint8_clamped_array: i32 = 2; pub const uint8_clamped_array: i32 = 2;
pub const napi_int16_array: i32 = 3; pub const int16_array: i32 = 3;
pub const napi_uint16_array: i32 = 4; pub const uint16_array: i32 = 4;
pub const napi_int32_array: i32 = 5; pub const int32_array: i32 = 5;
pub const napi_uint32_array: i32 = 6; pub const uint32_array: i32 = 6;
pub const napi_float32_array: i32 = 7; pub const float32_array: i32 = 7;
pub const napi_float64_array: i32 = 8; pub const float64_array: i32 = 8;
#[cfg(feature = "napi6")] #[cfg(feature = "napi6")]
pub const napi_bigint64_array: i32 = 9; pub const bigint64_array: i32 = 9;
#[cfg(feature = "napi6")] #[cfg(feature = "napi6")]
pub const napi_biguint64_array: i32 = 10; pub const biguint64_array: i32 = 10;
} }
pub type napi_status = i32; pub type napi_status = i32;
@ -158,29 +160,40 @@ pub struct napi_extended_error_info {
pub error_code: napi_status, pub error_code: napi_status,
} }
#[repr(C)] #[cfg(feature = "napi6")]
#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub type napi_key_collection_mode = i32;
pub enum napi_key_collection_mode {
napi_key_include_prototypes, #[cfg(feature = "napi6")]
napi_key_own_only, pub mod KeyCollectionMode {
pub use super::napi_key_collection_mode;
pub const include_prototypes: napi_key_collection_mode = 0;
pub const own_only: napi_key_collection_mode = 1;
} }
#[repr(C)] #[cfg(feature = "napi6")]
#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub type napi_key_filter = i32;
pub enum napi_key_filter {
napi_key_all_properties = 0, #[cfg(feature = "napi6")]
napi_key_writable = 1, pub mod KeyFilter {
napi_key_enumerable = 1 << 1, use super::napi_key_filter;
napi_key_configurable = 1 << 2,
napi_key_skip_strings = 1 << 3, pub const all_properties: napi_key_filter = 0;
napi_key_skip_symbols = 1 << 4, pub const writable: napi_key_filter = 1;
pub const enumerable: napi_key_filter = 1 << 1;
pub const configurable: napi_key_filter = 1 << 2;
pub const skip_strings: napi_key_filter = 1 << 3;
pub const skip_symbols: napi_key_filter = 1 << 4;
} }
#[repr(C)] #[cfg(feature = "napi6")]
#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub type napi_key_conversion = i32;
pub enum napi_key_conversion {
napi_key_keep_numbers, #[cfg(feature = "napi6")]
napi_key_numbers_to_strings, pub mod KeyConversion {
use super::napi_key_conversion;
pub const keep_numbers: napi_key_conversion = 0;
pub const numbers_to_strings: napi_key_conversion = 1;
} }
#[cfg(feature = "napi8")] #[cfg(feature = "napi8")]
@ -834,6 +847,11 @@ extern "C" {
pub fn napi_object_seal(env: napi_env, object: napi_value) -> napi_status; pub fn napi_object_seal(env: napi_env, object: napi_value) -> napi_status;
} }
#[cfg(feature = "experimental")]
extern "C" {
pub fn node_api_get_module_file_name(env: napi_env, result: *mut *const c_char) -> napi_status;
}
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct napi_callback_scope__ { pub struct napi_callback_scope__ {
@ -864,19 +882,24 @@ pub struct napi_threadsafe_function__ {
pub type napi_threadsafe_function = *mut napi_threadsafe_function__; pub type napi_threadsafe_function = *mut napi_threadsafe_function__;
#[cfg(feature = "napi4")] #[cfg(feature = "napi4")]
#[repr(C)] pub type napi_threadsafe_function_release_mode = i32;
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum napi_threadsafe_function_release_mode { #[cfg(feature = "napi4")]
napi_tsfn_release, pub mod ThreadsafeFunctionReleaseMode {
napi_tsfn_abort, use super::napi_threadsafe_function_release_mode;
pub const release: napi_threadsafe_function_release_mode = 0;
pub const abort: napi_threadsafe_function_release_mode = 1;
} }
#[cfg(feature = "napi4")] #[cfg(feature = "napi4")]
#[repr(C)] pub type napi_threadsafe_function_call_mode = i32;
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum napi_threadsafe_function_call_mode { #[cfg(feature = "napi4")]
napi_tsfn_nonblocking, pub mod ThreadsafeFunctionCallMode {
napi_tsfn_blocking, use super::napi_threadsafe_function_call_mode;
pub const nonblocking: napi_threadsafe_function_call_mode = 0;
pub const blocking: napi_threadsafe_function_call_mode = 1;
} }
pub type napi_async_execute_callback = pub type napi_async_execute_callback =