diff --git a/crates/neon/src/context/mod.rs b/crates/neon/src/context/mod.rs index 0bb9b80a2..e8a4d6d62 100644 --- a/crates/neon/src/context/mod.rs +++ b/crates/neon/src/context/mod.rs @@ -336,17 +336,15 @@ impl CallbackInfo<'_> { // * Node-API fills empty slots with `undefined` // * `Handle` and `JsValue` are transparent wrappers around a raw pointer unsafe { - assert_eq!( - sys::get_cb_info( - cx.env().to_raw(), - self.info, - &mut argc, - argv.as_mut_ptr().cast(), - ptr::null_mut(), - ptr::null_mut(), - ), - Ok(()) - ); + sys::get_cb_info( + cx.env().to_raw(), + self.info, + &mut argc, + argv.as_mut_ptr().cast(), + ptr::null_mut(), + ptr::null_mut(), + ) + .unwrap(); } // Empty values will be filled with `undefined` diff --git a/crates/neon/src/sys/array.rs b/crates/neon/src/sys/array.rs index 0f7d8cecb..773fc195f 100644 --- a/crates/neon/src/sys/array.rs +++ b/crates/neon/src/sys/array.rs @@ -6,10 +6,7 @@ use super::{ }; pub unsafe fn new(out: &mut Local, env: Env, length: usize) { - assert_eq!( - napi::create_array_with_length(env, length, out as *mut _), - Ok(()) - ); + let () = napi::create_array_with_length(env, length, out as *mut _).unwrap(); } /// Gets the length of a `napi_value` containing a JavaScript Array. @@ -19,9 +16,6 @@ pub unsafe fn new(out: &mut Local, env: Env, length: usize) { /// exception. pub unsafe fn len(env: Env, array: Local) -> u32 { let mut len = 0; - assert_eq!( - napi::get_array_length(env, array, &mut len as *mut _), - Ok(()) - ); + let () = napi::get_array_length(env, array, &mut len as *mut _).unwrap(); len } diff --git a/crates/neon/src/sys/arraybuffer.rs b/crates/neon/src/sys/arraybuffer.rs index af8a95150..a3a306f91 100644 --- a/crates/neon/src/sys/arraybuffer.rs +++ b/crates/neon/src/sys/arraybuffer.rs @@ -11,11 +11,10 @@ pub unsafe fn new(env: Env, len: usize) -> Result { let mut buf = MaybeUninit::uninit(); let status = napi::create_arraybuffer(env, len, null_mut(), buf.as_mut_ptr()); - if status == Err(napi::Status::PendingException) { - return Err(napi::Status::PendingException); - } - - assert_eq!(status, Ok(())); + let () = match status { + Err(err @ napi::Status::PendingException) => return Err(err), + status => status.unwrap(), + }; Ok(buf.assume_init()) } @@ -31,17 +30,15 @@ where let length = buf.len(); let mut result = MaybeUninit::uninit(); - assert_eq!( - napi::create_external_arraybuffer( - env, - buf.as_mut_ptr() as *mut _, - length, - Some(drop_external::), - Box::into_raw(data) as *mut _, - result.as_mut_ptr(), - ), - Ok(()) - ); + let () = napi::create_external_arraybuffer( + env, + buf.as_mut_ptr() as *mut _, + length, + Some(drop_external::), + Box::into_raw(data) as *mut _, + result.as_mut_ptr(), + ) + .unwrap(); result.assume_init() } @@ -58,10 +55,7 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] { let mut data = MaybeUninit::uninit(); let mut size = 0usize; - assert_eq!( - napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _), - Ok(()) - ); + let () = napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap(); if size == 0 { return &mut []; @@ -76,10 +70,7 @@ pub unsafe fn size(env: Env, buf: Local) -> usize { let mut data = MaybeUninit::uninit(); let mut size = 0usize; - assert_eq!( - napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _), - Ok(()) - ); + let () = napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap(); size } diff --git a/crates/neon/src/sys/async_work.rs b/crates/neon/src/sys/async_work.rs index d1e7ceed6..710c3633f 100644 --- a/crates/neon/src/sys/async_work.rs +++ b/crates/neon/src/sys/async_work.rs @@ -58,26 +58,24 @@ pub unsafe fn schedule( let work = &mut data.work as *mut _; // Create the `async_work` - assert_eq!( - napi::create_async_work( - env, - ptr::null_mut(), - super::string(env, "neon_async_work"), - Some(call_execute::), - Some(call_complete::), - Box::into_raw(data).cast(), - work, - ), - Ok(()) - ); + let () = napi::create_async_work( + env, + ptr::null_mut(), + super::string(env, "neon_async_work"), + Some(call_execute::), + Some(call_complete::), + Box::into_raw(data).cast(), + work, + ) + .unwrap(); // Queue the work match napi::queue_async_work(env, *work) { Ok(()) => {} - Err(status) => { + status => { // If queueing failed, delete the work to prevent a leak napi::delete_async_work(env, *work); - assert_eq!(status, napi::Status::Ok); + status.unwrap() } } } diff --git a/crates/neon/src/sys/buffer.rs b/crates/neon/src/sys/buffer.rs index f698d746e..945de65eb 100644 --- a/crates/neon/src/sys/buffer.rs +++ b/crates/neon/src/sys/buffer.rs @@ -20,11 +20,10 @@ pub unsafe fn uninitialized(env: Env, len: usize) -> Result<(Local, *mut u8), na let mut bytes = MaybeUninit::uninit(); let status = napi::create_buffer(env, len, bytes.as_mut_ptr(), buf.as_mut_ptr()); - if status == Err(napi::Status::PendingException) { - return Err(napi::Status::PendingException); - } - - assert_eq!(status, Ok(())); + let () = match status { + Err(err @ napi::Status::PendingException) => return Err(err), + status => status.unwrap(), + }; Ok((buf.assume_init(), bytes.assume_init().cast())) } @@ -40,17 +39,15 @@ where let length = buf.len(); let mut result = MaybeUninit::uninit(); - assert_eq!( - napi::create_external_buffer( - env, - length, - buf.as_mut_ptr() as *mut _, - Some(drop_external::), - Box::into_raw(data) as *mut _, - result.as_mut_ptr(), - ), - Ok(()) - ); + napi::create_external_buffer( + env, + length, + buf.as_mut_ptr() as *mut _, + Some(drop_external::), + Box::into_raw(data) as *mut _, + result.as_mut_ptr(), + ) + .unwrap(); result.assume_init() } @@ -67,10 +64,7 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] { let mut data = MaybeUninit::uninit(); let mut size = 0usize; - assert_eq!( - napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _), - Ok(()) - ); + let () = napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap(); if size == 0 { return &mut []; @@ -85,10 +79,7 @@ pub unsafe fn size(env: Env, buf: Local) -> usize { let mut data = MaybeUninit::uninit(); let mut size = 0usize; - assert_eq!( - napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _), - Ok(()) - ); + let () = napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap(); size } diff --git a/crates/neon/src/sys/call.rs b/crates/neon/src/sys/call.rs index 1c7e0ba2a..de68aa62f 100644 --- a/crates/neon/src/sys/call.rs +++ b/crates/neon/src/sys/call.rs @@ -33,9 +33,7 @@ impl Arguments { pub unsafe fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool { let mut target: MaybeUninit = MaybeUninit::zeroed(); - let status = napi::get_new_target(env, info, target.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::get_new_target(env, info, target.as_mut_ptr()).unwrap(); // get_new_target is guaranteed to assign to target, so it's initialized. let target: Local = target.assume_init(); @@ -47,23 +45,23 @@ pub unsafe fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool { } pub unsafe fn this(env: Env, info: FunctionCallbackInfo, out: &mut Local) { - let status = napi::get_cb_info(env, info, null_mut(), null_mut(), out as *mut _, null_mut()); - assert_eq!(status, Ok(())); + let () = + napi::get_cb_info(env, info, null_mut(), null_mut(), out as *mut _, null_mut()).unwrap(); } /// Gets the number of arguments passed to the function. // TODO: Remove this when `FunctionContext` is refactored to get call info upfront. pub unsafe fn len(env: Env, info: FunctionCallbackInfo) -> usize { let mut argc = 0usize; - let status = napi::get_cb_info( + let () = napi::get_cb_info( env, info, &mut argc as *mut _, null_mut(), null_mut(), null_mut(), - ); - assert_eq!(status, Ok(())); + ) + .unwrap(); argc } @@ -75,34 +73,30 @@ pub unsafe fn argv(env: Env, info: FunctionCallbackInfo) -> Arguments { // Starts as the size allocated; after `get_cb_info` it is the number of arguments let mut argc = ARGV_SIZE; - assert_eq!( - napi::get_cb_info( - env, - info, - &mut argc as *mut _, - argv.as_mut_ptr().cast(), - null_mut(), - null_mut(), - ), - Ok(()), - ); + let () = napi::get_cb_info( + env, + info, + &mut argc as *mut _, + argv.as_mut_ptr().cast(), + null_mut(), + null_mut(), + ) + .unwrap(); // We did not allocate enough space; allocate on the heap and try again let argv = if argc > ARGV_SIZE { // We know exactly how much space to reserve let mut argv = Vec::with_capacity(argc); - assert_eq!( - napi::get_cb_info( - env, - info, - &mut argc as *mut _, - argv.as_mut_ptr(), - null_mut(), - null_mut(), - ), - Ok(()), - ); + let () = napi::get_cb_info( + env, + info, + &mut argc as *mut _, + argv.as_mut_ptr(), + null_mut(), + null_mut(), + ) + .unwrap(); // Set the size of `argv` to the number of initialized elements argv.set_len(argc); diff --git a/crates/neon/src/sys/date.rs b/crates/neon/src/sys/date.rs index 2a2584f5a..ed0b62b47 100644 --- a/crates/neon/src/sys/date.rs +++ b/crates/neon/src/sys/date.rs @@ -12,8 +12,7 @@ use super::{ /// `env` is a raw pointer. Please ensure it points to a napi_env that is valid for the current context. pub unsafe fn new_date(env: Env, value: f64) -> Local { let mut local = MaybeUninit::zeroed(); - let status = napi::create_date(env, value, local.as_mut_ptr()); - assert_eq!(status, Ok(())); + let () = napi::create_date(env, value, local.as_mut_ptr()).unwrap(); local.assume_init() } @@ -25,7 +24,6 @@ pub unsafe fn new_date(env: Env, value: f64) -> Local { /// `Local` must be an NAPI value associated with the given `Env` pub unsafe fn value(env: Env, p: Local) -> f64 { let mut value = 0.0; - let status = napi::get_date_value(env, p, &mut value as *mut _); - assert_eq!(status, Ok(())); + let () = napi::get_date_value(env, p, &mut value as *mut _).unwrap(); value } diff --git a/crates/neon/src/sys/error.rs b/crates/neon/src/sys/error.rs index e7c6a2e99..8a9559d95 100644 --- a/crates/neon/src/sys/error.rs +++ b/crates/neon/src/sys/error.rs @@ -8,9 +8,7 @@ use super::{ pub unsafe fn is_throwing(env: Env) -> bool { let mut b: MaybeUninit = MaybeUninit::zeroed(); - let status = napi::is_exception_pending(env, b.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::is_exception_pending(env, b.as_mut_ptr()).unwrap(); b.assume_init() } @@ -20,72 +18,55 @@ pub unsafe fn catch_error(env: Env, error: *mut Local) -> bool { return false; } - let status = napi::get_and_clear_last_exception(env, error); - - assert_eq!(status, Ok(())); + let () = napi::get_and_clear_last_exception(env, error).unwrap(); true } pub unsafe fn clear_exception(env: Env) { let mut result = MaybeUninit::uninit(); - let status = napi::is_exception_pending(env, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::is_exception_pending(env, result.as_mut_ptr()).unwrap(); if !result.assume_init() { return; } let mut result = MaybeUninit::uninit(); - let status = napi::get_and_clear_last_exception(env, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::get_and_clear_last_exception(env, result.as_mut_ptr()).unwrap(); } pub unsafe fn throw(env: Env, val: Local) { - let status = napi::throw(env, val); - - assert_eq!(status, Ok(())); + let () = napi::throw(env, val).unwrap(); } pub unsafe fn new_error(env: Env, out: &mut Local, msg: Local) { let mut result = MaybeUninit::uninit(); - let status = napi::create_error(env, ptr::null_mut(), msg, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::create_error(env, ptr::null_mut(), msg, result.as_mut_ptr()).unwrap(); *out = result.assume_init(); } pub unsafe fn new_type_error(env: Env, out: &mut Local, msg: Local) { let mut result = MaybeUninit::uninit(); - let status = napi::create_type_error(env, ptr::null_mut(), msg, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::create_type_error(env, ptr::null_mut(), msg, result.as_mut_ptr()).unwrap(); *out = result.assume_init(); } pub unsafe fn new_range_error(env: Env, out: &mut Local, msg: Local) { let mut result = MaybeUninit::uninit(); - let status = napi::create_range_error(env, ptr::null_mut(), msg, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::create_range_error(env, ptr::null_mut(), msg, result.as_mut_ptr()).unwrap(); *out = result.assume_init(); } pub unsafe fn throw_error_from_utf8(env: Env, msg: *const u8, len: i32) { let mut out = MaybeUninit::uninit(); - let status = napi::create_string_utf8(env, msg as *const _, len as usize, out.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = + napi::create_string_utf8(env, msg as *const _, len as usize, out.as_mut_ptr()).unwrap(); let mut err = MaybeUninit::uninit(); - let status = napi::create_error(env, ptr::null_mut(), out.assume_init(), err.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::create_error(env, ptr::null_mut(), out.assume_init(), err.as_mut_ptr()).unwrap(); throw(env, err.assume_init()); } diff --git a/crates/neon/src/sys/external.rs b/crates/neon/src/sys/external.rs index b390d51b5..607b6cf42 100644 --- a/crates/neon/src/sys/external.rs +++ b/crates/neon/src/sys/external.rs @@ -30,9 +30,7 @@ extern "C" fn finalize_external( /// pub unsafe fn deref(env: Env, local: Local) -> Option<*const T> { let mut result = MaybeUninit::uninit(); - let status = napi::typeof_value(env, local, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::typeof_value(env, local, result.as_mut_ptr()).unwrap(); let result = result.assume_init(); @@ -50,9 +48,7 @@ pub unsafe fn deref(env: Env, local: Local) -> Option<*const T> { } let mut result = MaybeUninit::uninit(); - let status = napi::get_value_external(env, local, result.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::get_value_external(env, local, result.as_mut_ptr()).unwrap(); let v = result.assume_init(); let v = &**v.cast_const().cast::>() as *const T; @@ -77,7 +73,7 @@ pub unsafe fn create(env: Env, v: T, finalizer: fn(Env, T)) -> Local // `napi_create_external` will only fail if the VM is in a throwing state // or shutting down. - assert_eq!(status, Ok(())); + let () = status.unwrap(); let external = result.assume_init(); diff --git a/crates/neon/src/sys/fun.rs b/crates/neon/src/sys/fun.rs index 420451bc2..6723c9231 100644 --- a/crates/neon/src/sys/fun.rs +++ b/crates/neon/src/sys/fun.rs @@ -22,13 +22,14 @@ where out.as_mut_ptr(), ); - if status == Err(napi::Status::PendingException) { - drop(Box::from_raw(data)); + let () = match status { + Err(err @ napi::Status::PendingException) => { + drop(Box::from_raw(data)); - return Err(napi::Status::PendingException); - } - - assert_eq!(status, Ok(())); + return Err(err); + } + status => status.unwrap(), + }; let out = out.assume_init(); @@ -54,7 +55,7 @@ where // If adding the finalizer fails the closure will leak, but it would // be unsafe to drop it because there's no guarantee V8 won't use the // pointer. - assert_eq!(status, Ok(())); + let () = status.unwrap(); } Ok(out) @@ -67,16 +68,15 @@ where F: Fn(Env, napi::CallbackInfo) -> Local + 'static, { let mut data = MaybeUninit::uninit(); - let status = napi::get_cb_info( + let () = napi::get_cb_info( env, info, ptr::null_mut(), ptr::null_mut(), ptr::null_mut(), data.as_mut_ptr(), - ); - - assert_eq!(status, Ok(())); + ) + .unwrap(); let callback = &*data.assume_init().cast::(); diff --git a/crates/neon/src/sys/lifecycle.rs b/crates/neon/src/sys/lifecycle.rs index 01e8bdeb4..40dbf06d5 100644 --- a/crates/neon/src/sys/lifecycle.rs +++ b/crates/neon/src/sys/lifecycle.rs @@ -17,10 +17,8 @@ use super::{bindings as napi, raw::Env}; pub unsafe fn set_instance_data(env: Env, data: T) -> *mut T { let data = Box::into_raw(Box::new(data)); - assert_eq!( - napi::set_instance_data(env, data.cast(), Some(drop_box::), ptr::null_mut(),), - Ok(()), - ); + let () = + napi::set_instance_data(env, data.cast(), Some(drop_box::), ptr::null_mut()).unwrap(); data } @@ -33,7 +31,7 @@ pub unsafe fn set_instance_data(env: Env, data: T) -> *mut T pub unsafe fn get_instance_data(env: Env) -> *mut T { let mut data = MaybeUninit::uninit(); - assert_eq!(napi::get_instance_data(env, data.as_mut_ptr(),), Ok(()),); + let () = napi::get_instance_data(env, data.as_mut_ptr()).unwrap(); data.assume_init().cast() } diff --git a/crates/neon/src/sys/mem.rs b/crates/neon/src/sys/mem.rs index 8f3a7b6c3..43af99b78 100644 --- a/crates/neon/src/sys/mem.rs +++ b/crates/neon/src/sys/mem.rs @@ -5,9 +5,6 @@ use super::{ pub unsafe fn strict_equals(env: Env, lhs: Local, rhs: Local) -> bool { let mut result = false; - assert_eq!( - napi::strict_equals(env, lhs, rhs, &mut result as *mut _), - Ok(()) - ); + let () = napi::strict_equals(env, lhs, rhs, &mut result as *mut _).unwrap(); result } diff --git a/crates/neon/src/sys/mod.rs b/crates/neon/src/sys/mod.rs index 967156999..5dd161a8f 100644 --- a/crates/neon/src/sys/mod.rs +++ b/crates/neon/src/sys/mod.rs @@ -113,15 +113,13 @@ unsafe fn string(env: Env, s: impl AsRef) -> raw::Local { let s = s.as_ref(); let mut result = MaybeUninit::uninit(); - assert_eq!( - create_string_utf8( - env, - s.as_bytes().as_ptr() as *const _, - s.len(), - result.as_mut_ptr(), - ), - Ok(()) - ); + let () = create_string_utf8( + env, + s.as_bytes().as_ptr() as *const _, + s.len(), + result.as_mut_ptr(), + ) + .unwrap(); result.assume_init() } diff --git a/crates/neon/src/sys/primitive.rs b/crates/neon/src/sys/primitive.rs index cfbcd73c7..2a5b25436 100644 --- a/crates/neon/src/sys/primitive.rs +++ b/crates/neon/src/sys/primitive.rs @@ -5,17 +5,17 @@ use super::{ /// Mutates the `out` argument provided to refer to the global `undefined` object. pub unsafe fn undefined(out: &mut Local, env: Env) { - napi::get_undefined(env, out as *mut Local); + let () = napi::get_undefined(env, out as *mut Local).unwrap(); } /// Mutates the `out` argument provided to refer to the global `null` object. pub unsafe fn null(out: &mut Local, env: Env) { - napi::get_null(env, out as *mut Local); + let () = napi::get_null(env, out as *mut Local).unwrap(); } /// Mutates the `out` argument provided to refer to one of the global `true` or `false` objects. pub unsafe fn boolean(out: &mut Local, env: Env, b: bool) { - napi::get_boolean(env, b, out as *mut Local); + let () = napi::get_boolean(env, b, out as *mut Local).unwrap(); } /// Get the boolean value out of a `Local` object. If the `Local` object does not contain a @@ -32,16 +32,13 @@ pub unsafe fn boolean_value(env: Env, p: Local) -> bool { /// Mutates the `out` argument provided to refer to a newly created `Local` containing a /// JavaScript number. pub unsafe fn number(out: &mut Local, env: Env, v: f64) { - napi::create_double(env, v, out as *mut Local); + let () = napi::create_double(env, v, out as *mut Local).unwrap(); } /// Gets the underlying value of an `Local` object containing a JavaScript number. Panics if /// the given `Local` is not a number. pub unsafe fn number_value(env: Env, p: Local) -> f64 { let mut value = 0.0; - assert_eq!( - napi::get_value_double(env, p, &mut value as *mut f64), - Ok(()) - ); + let () = napi::get_value_double(env, p, &mut value as *mut f64).unwrap(); value } diff --git a/crates/neon/src/sys/promise.rs b/crates/neon/src/sys/promise.rs index 7a0b80f58..c054045d6 100644 --- a/crates/neon/src/sys/promise.rs +++ b/crates/neon/src/sys/promise.rs @@ -15,10 +15,7 @@ pub unsafe fn create(env: Env) -> (napi::Deferred, napi::Value) { let mut deferred = MaybeUninit::uninit(); let mut promise = MaybeUninit::uninit(); - assert_eq!( - napi::create_promise(env, deferred.as_mut_ptr(), promise.as_mut_ptr()), - Ok(()), - ); + let () = napi::create_promise(env, deferred.as_mut_ptr(), promise.as_mut_ptr()).unwrap(); (deferred.assume_init(), promise.assume_init()) } @@ -29,7 +26,7 @@ pub unsafe fn create(env: Env) -> (napi::Deferred, napi::Value) { /// * `env` is a valid `napi_env` for the current thread /// * `resolution` is a valid `napi::Value` pub unsafe fn resolve(env: Env, deferred: napi::Deferred, resolution: napi::Value) { - assert_eq!(napi::resolve_deferred(env, deferred, resolution), Ok(()),); + let () = napi::resolve_deferred(env, deferred, resolution).unwrap(); } /// Rejects a promise from a `napi::Deferred` handle @@ -38,7 +35,7 @@ pub unsafe fn resolve(env: Env, deferred: napi::Deferred, resolution: napi::Valu /// * `env` is a valid `napi_env` for the current thread /// * `rejection` is a valid `napi::Value` pub unsafe fn reject(env: Env, deferred: napi::Deferred, rejection: napi::Value) { - assert_eq!(napi::reject_deferred(env, deferred, rejection), Ok(()),); + let () = napi::reject_deferred(env, deferred, rejection).unwrap(); } #[cfg(feature = "napi-6")] @@ -50,10 +47,7 @@ pub unsafe fn reject_err_message(env: Env, deferred: napi::Deferred, msg: impl A let msg = super::string(env, msg); let mut err = MaybeUninit::uninit(); - assert_eq!( - napi::create_error(env, std::ptr::null_mut(), msg, err.as_mut_ptr()), - Ok(()), - ); + let () = napi::create_error(env, std::ptr::null_mut(), msg, err.as_mut_ptr()).unwrap(); reject(env, deferred, err.assume_init()); } diff --git a/crates/neon/src/sys/reference.rs b/crates/neon/src/sys/reference.rs index b80425b07..ab2e9d678 100644 --- a/crates/neon/src/sys/reference.rs +++ b/crates/neon/src/sys/reference.rs @@ -8,10 +8,7 @@ use super::{ pub unsafe fn new(env: Env, value: Local) -> napi::Ref { let mut result = MaybeUninit::uninit(); - assert_eq!( - napi::create_reference(env, value, 1, result.as_mut_ptr()), - Ok(()), - ); + let () = napi::create_reference(env, value, 1, result.as_mut_ptr()).unwrap(); result.assume_init() } @@ -21,7 +18,7 @@ pub unsafe fn new(env: Env, value: Local) -> napi::Ref { pub unsafe fn reference(env: Env, value: napi::Ref) -> usize { let mut result = MaybeUninit::uninit(); - assert_eq!(napi::reference_ref(env, value, result.as_mut_ptr()), Ok(()),); + let () = napi::reference_ref(env, value, result.as_mut_ptr()).unwrap(); result.assume_init() as usize } @@ -31,10 +28,7 @@ pub unsafe fn reference(env: Env, value: napi::Ref) -> usize { pub unsafe fn unreference(env: Env, value: napi::Ref) { let mut result = MaybeUninit::uninit(); - assert_eq!( - napi::reference_unref(env, value, result.as_mut_ptr()), - Ok(()), - ); + let () = napi::reference_unref(env, value, result.as_mut_ptr()).unwrap(); if result.assume_init() == 0 { assert_eq!(napi::delete_reference(env, value), Ok(())); @@ -46,10 +40,7 @@ pub unsafe fn unreference(env: Env, value: napi::Ref) { pub unsafe fn get(env: Env, value: napi::Ref) -> Local { let mut result = MaybeUninit::uninit(); - assert_eq!( - napi::get_reference_value(env, value, result.as_mut_ptr()), - Ok(()), - ); + let () = napi::get_reference_value(env, value, result.as_mut_ptr()).unwrap(); result.assume_init() } diff --git a/crates/neon/src/sys/scope.rs b/crates/neon/src/sys/scope.rs index ea23fc585..c7d24fec9 100644 --- a/crates/neon/src/sys/scope.rs +++ b/crates/neon/src/sys/scope.rs @@ -14,7 +14,7 @@ impl HandleScope { pub(crate) unsafe fn new(env: Env) -> Self { let mut scope = MaybeUninit::uninit(); - assert_eq!(napi::open_handle_scope(env, scope.as_mut_ptr()), Ok(()),); + let () = napi::open_handle_scope(env, scope.as_mut_ptr()).unwrap(); Self { env, @@ -42,10 +42,7 @@ impl EscapableHandleScope { pub(crate) unsafe fn new(env: Env) -> Self { let mut scope = MaybeUninit::uninit(); - assert_eq!( - napi::open_escapable_handle_scope(env, scope.as_mut_ptr()), - Ok(()), - ); + let () = napi::open_escapable_handle_scope(env, scope.as_mut_ptr()).unwrap(); Self { env, @@ -56,10 +53,7 @@ impl EscapableHandleScope { pub(crate) unsafe fn escape(&self, value: napi::Value) -> napi::Value { let mut escapee = MaybeUninit::uninit(); - assert_eq!( - napi::escape_handle(self.env, self.scope, value, escapee.as_mut_ptr()), - Ok(()), - ); + let () = napi::escape_handle(self.env, self.scope, value, escapee.as_mut_ptr()).unwrap(); escapee.assume_init() } @@ -76,5 +70,5 @@ impl Drop for EscapableHandleScope { } pub unsafe fn get_global(env: Env, out: &mut Local) { - assert_eq!(super::get_global(env, out as *mut _), Ok(())); + let () = super::get_global(env, out as *mut _).unwrap(); } diff --git a/crates/neon/src/sys/string.rs b/crates/neon/src/sys/string.rs index a747c50ab..6edcf7756 100644 --- a/crates/neon/src/sys/string.rs +++ b/crates/neon/src/sys/string.rs @@ -13,36 +13,30 @@ pub unsafe fn new(out: &mut Local, env: Env, data: *const u8, len: i32) -> bool pub unsafe fn utf8_len(env: Env, value: Local) -> usize { let mut len = MaybeUninit::uninit(); - let status = napi::get_value_string_utf8(env, value, ptr::null_mut(), 0, len.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::get_value_string_utf8(env, value, ptr::null_mut(), 0, len.as_mut_ptr()).unwrap(); len.assume_init() } pub unsafe fn data(env: Env, out: *mut u8, len: usize, value: Local) -> usize { let mut read = MaybeUninit::uninit(); - let status = napi::get_value_string_utf8(env, value, out as *mut _, len, read.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = + napi::get_value_string_utf8(env, value, out as *mut _, len, read.as_mut_ptr()).unwrap(); read.assume_init() } pub unsafe fn utf16_len(env: Env, value: Local) -> usize { let mut len = MaybeUninit::uninit(); - let status = napi::get_value_string_utf16(env, value, ptr::null_mut(), 0, len.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = + napi::get_value_string_utf16(env, value, ptr::null_mut(), 0, len.as_mut_ptr()).unwrap(); len.assume_init() } pub unsafe fn data_utf16(env: Env, out: *mut u16, len: usize, value: Local) -> usize { let mut read = MaybeUninit::uninit(); - let status = napi::get_value_string_utf16(env, value, out, len, read.as_mut_ptr()); - - assert_eq!(status, Ok(())); + let () = napi::get_value_string_utf16(env, value, out, len, read.as_mut_ptr()).unwrap(); read.assume_init() } diff --git a/crates/neon/src/sys/tag.rs b/crates/neon/src/sys/tag.rs index 7d0cfd78e..6152ccc5d 100644 --- a/crates/neon/src/sys/tag.rs +++ b/crates/neon/src/sys/tag.rs @@ -6,7 +6,7 @@ use super::{ /// Return true if an `napi_value` `val` has the expected value type. unsafe fn is_type(env: Env, val: Local, expect: napi::ValueType) -> bool { let mut actual = napi::ValueType::Undefined; - assert_eq!(napi::typeof_value(env, val, &mut actual as *mut _), Ok(())); + let () = napi::typeof_value(env, val, &mut actual as *mut _).unwrap(); actual == expect } @@ -49,38 +49,35 @@ pub unsafe fn is_function(env: Env, val: Local) -> bool { pub unsafe fn is_error(env: Env, val: Local) -> bool { let mut result = false; - assert_eq!(napi::is_error(env, val, &mut result as *mut _), Ok(())); + let () = napi::is_error(env, val, &mut result as *mut _).unwrap(); result } /// Is `val` a Node.js Buffer instance? pub unsafe fn is_buffer(env: Env, val: Local) -> bool { let mut result = false; - assert_eq!(napi::is_buffer(env, val, &mut result as *mut _), Ok(())); + let () = napi::is_buffer(env, val, &mut result as *mut _).unwrap(); result } /// Is `val` an ArrayBuffer instance? pub unsafe fn is_arraybuffer(env: Env, val: Local) -> bool { let mut result = false; - assert_eq!( - napi::is_arraybuffer(env, val, &mut result as *mut _), - Ok(()) - ); + let () = napi::is_arraybuffer(env, val, &mut result as *mut _).unwrap(); result } /// Is `val` a TypedArray instance? pub unsafe fn is_typedarray(env: Env, val: Local) -> bool { let mut result = false; - assert_eq!(napi::is_typedarray(env, val, &mut result as *mut _), Ok(())); + let () = napi::is_typedarray(env, val, &mut result as *mut _).unwrap(); result } #[cfg(feature = "napi-5")] pub unsafe fn is_date(env: Env, val: Local) -> bool { let mut result = false; - assert_eq!(napi::is_date(env, val, &mut result as *mut _), Ok(())); + let () = napi::is_date(env, val, &mut result as *mut _).unwrap(); result } @@ -90,22 +87,20 @@ pub unsafe fn is_date(env: Env, val: Local) -> bool { /// * `env` is a valid `napi_env` for the current thread pub unsafe fn is_promise(env: Env, val: Local) -> bool { let mut result = false; - assert_eq!(napi::is_promise(env, val, &mut result as *mut _), Ok(())); + let () = napi::is_promise(env, val, &mut result as *mut _).unwrap(); result } #[cfg(feature = "napi-8")] pub unsafe fn type_tag_object(env: Env, object: Local, tag: &super::TypeTag) { - assert_eq!(napi::type_tag_object(env, object, tag as *const _), Ok(())); + let () = napi::type_tag_object(env, object, tag as *const _).unwrap(); } #[cfg(feature = "napi-8")] pub unsafe fn check_object_type_tag(env: Env, object: Local, tag: &super::TypeTag) -> bool { let mut result = false; - assert_eq!( - napi::check_object_type_tag(env, object, tag as *const _, &mut result as *mut _), - Ok(()) - ); + let () = + napi::check_object_type_tag(env, object, tag as *const _, &mut result as *mut _).unwrap(); result } diff --git a/crates/neon/src/sys/tsfn.rs b/crates/neon/src/sys/tsfn.rs index 8a79e9f29..5562a810e 100644 --- a/crates/neon/src/sys/tsfn.rs +++ b/crates/neon/src/sys/tsfn.rs @@ -129,13 +129,13 @@ impl ThreadsafeFunction { /// References a threadsafe function to prevent exiting the event loop until it has been dropped. (Default) /// Safety: `Env` must be valid for the current thread pub unsafe fn reference(&self, env: Env) { - assert_eq!(napi::ref_threadsafe_function(env, self.tsfn.0), Ok(()),); + let () = napi::ref_threadsafe_function(env, self.tsfn.0).unwrap(); } /// Unreferences a threadsafe function to allow exiting the event loop before it has been dropped. /// Safety: `Env` must be valid for the current thread pub unsafe fn unref(&self, env: Env) { - assert_eq!(napi::unref_threadsafe_function(env, self.tsfn.0), Ok(()),); + let () = napi::unref_threadsafe_function(env, self.tsfn.0).unwrap(); } // Provides a C ABI wrapper for a napi callback notifying us about tsfn diff --git a/crates/neon/src/sys/typedarray.rs b/crates/neon/src/sys/typedarray.rs index 0393dc398..7565ade42 100644 --- a/crates/neon/src/sys/typedarray.rs +++ b/crates/neon/src/sys/typedarray.rs @@ -50,11 +50,10 @@ pub unsafe fn new( let mut array = MaybeUninit::uninit(); let status = napi::create_typedarray(env, typ, len, buffer, offset, array.as_mut_ptr()); - if status == Err(napi::Status::PendingException) { - return Err(napi::Status::PendingException); - } - - assert_eq!(status, Ok(())); + let () = match status { + Err(err @ napi::Status::PendingException) => return Err(err), + status => status.unwrap(), + }; Ok(array.assume_init()) } diff --git a/crates/neon/src/types_impl/bigint.rs b/crates/neon/src/types_impl/bigint.rs index a86e3c427..6d7d7d754 100644 --- a/crates/neon/src/types_impl/bigint.rs +++ b/crates/neon/src/types_impl/bigint.rs @@ -76,10 +76,7 @@ impl JsBigInt { { let mut v = MaybeUninit::uninit(); let v = unsafe { - assert_eq!( - sys::create_bigint_int64(cx.env().to_raw(), n, v.as_mut_ptr(),), - Ok(()), - ); + let () = sys::create_bigint_int64(cx.env().to_raw(), n, v.as_mut_ptr()).unwrap(); v.assume_init() }; @@ -104,10 +101,7 @@ impl JsBigInt { { let mut v = MaybeUninit::uninit(); let v = unsafe { - assert_eq!( - sys::create_bigint_uint64(cx.env().to_raw(), n, v.as_mut_ptr(),), - Ok(()) - ); + let () = sys::create_bigint_uint64(cx.env().to_raw(), n, v.as_mut_ptr()).unwrap(); v.assume_init() }; @@ -202,16 +196,14 @@ impl JsBigInt { let mut v = MaybeUninit::uninit(); let v = unsafe { - assert_eq!( - sys::create_bigint_words( - cx.env().to_raw(), - sign_bit, - digits.len(), - digits.as_ptr(), - v.as_mut_ptr(), - ), - Ok(()) - ); + let () = sys::create_bigint_words( + cx.env().to_raw(), + sign_bit, + digits.len(), + digits.as_ptr(), + v.as_mut_ptr(), + ) + .unwrap(); v.assume_init() }; @@ -234,10 +226,8 @@ impl JsBigInt { let mut lossless = false; unsafe { - assert_eq!( - sys::get_value_bigint_int64(cx.env().to_raw(), self.0, &mut n, &mut lossless), - Ok(()) - ); + let () = sys::get_value_bigint_int64(cx.env().to_raw(), self.0, &mut n, &mut lossless) + .unwrap(); } if lossless { @@ -258,10 +248,8 @@ impl JsBigInt { let mut lossless = false; unsafe { - assert_eq!( - sys::get_value_bigint_uint64(cx.env().to_raw(), self.0, &mut n, &mut lossless), - Ok(()) - ); + let () = sys::get_value_bigint_uint64(cx.env().to_raw(), self.0, &mut n, &mut lossless) + .unwrap(); } if lossless { @@ -386,16 +374,14 @@ impl JsBigInt { let mut word_count = digits.len(); unsafe { - assert_eq!( - sys::get_value_bigint_words( - cx.env().to_raw(), - self.0, - &mut sign_bit, - &mut word_count, - digits.as_mut_ptr(), - ), - Ok(()) - ); + let () = sys::get_value_bigint_words( + cx.env().to_raw(), + self.0, + &mut sign_bit, + &mut word_count, + digits.as_mut_ptr(), + ) + .unwrap(); } let sign = if sign_bit == 0 { diff --git a/crates/neon/src/types_impl/extract/try_from_js.rs b/crates/neon/src/types_impl/extract/try_from_js.rs index b8d67107d..1b5e6d58b 100644 --- a/crates/neon/src/types_impl/extract/try_from_js.rs +++ b/crates/neon/src/types_impl/extract/try_from_js.rs @@ -81,11 +81,11 @@ impl<'cx> TryFromJs<'cx> for f64 { let mut n = 0f64; unsafe { - match sys::get_value_double(cx.env().to_raw(), v.to_local(), &mut n) { + let () = match sys::get_value_double(cx.env().to_raw(), v.to_local(), &mut n) { Err(sys::Status::NumberExpected) => return Ok(Err(TypeExpected::new())), Err(sys::Status::PendingException) => return Err(Throw::new()), - status => assert_eq!(status, Ok(())), - } + status => status.unwrap(), + }; } Ok(Ok(n)) @@ -102,11 +102,11 @@ impl<'cx> TryFromJs<'cx> for bool { let mut b = false; unsafe { - match sys::get_value_bool(cx.env().to_raw(), v.to_local(), &mut b) { + let () = match sys::get_value_bool(cx.env().to_raw(), v.to_local(), &mut b) { Err(sys::Status::BooleanExpected) => return Ok(Err(TypeExpected::new())), Err(sys::Status::PendingException) => return Err(Throw::new()), - status => assert_eq!(status, Ok(())), - } + status => status.unwrap(), + }; } Ok(Ok(b)) @@ -125,11 +125,11 @@ impl<'cx> TryFromJs<'cx> for String { let mut len = 0usize; unsafe { - match sys::get_value_string_utf8(env, v, ptr::null_mut(), 0, &mut len) { + let () = match sys::get_value_string_utf8(env, v, ptr::null_mut(), 0, &mut len) { Err(sys::Status::StringExpected) => return Ok(Err(TypeExpected::new())), Err(sys::Status::PendingException) => return Err(Throw::new()), - status => assert_eq!(status, Ok(())), - } + status => status.unwrap(), + }; } // Make room for null terminator to avoid losing a character @@ -168,11 +168,11 @@ impl<'cx> TryFromJs<'cx> for Date { let mut d = 0f64; unsafe { - match sys::get_date_value(cx.env().to_raw(), v.to_local(), &mut d) { + let () = match sys::get_date_value(cx.env().to_raw(), v.to_local(), &mut d) { Err(sys::Status::DateExpected) => return Ok(Err(TypeExpected::new())), Err(sys::Status::PendingException) => return Err(Throw::new()), - status => assert_eq!(status, Ok(())), - } + status => status.unwrap(), + }; } Ok(Ok(Date(d))) @@ -259,10 +259,10 @@ where let mut ty = sys::ValueType::Object; unsafe { - match sys::typeof_value(cx.env().to_raw(), v.to_local(), &mut ty) { + let () = match sys::typeof_value(cx.env().to_raw(), v.to_local(), &mut ty) { Err(sys::Status::PendingException) => return Err(Throw::new()), - status => assert_eq!(status, Ok(())), - } + status => status.unwrap(), + }; } Ok(matches!( diff --git a/crates/neon/src/types_impl/private.rs b/crates/neon/src/types_impl/private.rs index 3eeada40a..b64eddcac 100644 --- a/crates/neon/src/types_impl/private.rs +++ b/crates/neon/src/types_impl/private.rs @@ -121,9 +121,7 @@ unsafe fn check_call_status<'a, C: Context<'a>>( Err(sys::Status::PendingException) => { return Err(Throw::new()); } - status => { - assert_eq!(status, Ok(())); - } + status => status.unwrap(), } Ok(())