Skip to content

Commit

Permalink
Use Result::unwrap() instead of assert_eq!
Browse files Browse the repository at this point in the history
  • Loading branch information
akonradi-signal committed Sep 11, 2024
1 parent 9b869ce commit 29b92e7
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 353 deletions.
20 changes: 9 additions & 11 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,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`
Expand Down
10 changes: 2 additions & 8 deletions crates/neon/src/sys/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
39 changes: 15 additions & 24 deletions crates/neon/src/sys/arraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ pub unsafe fn new(env: Env, len: usize) -> Result<Local, napi::Status> {
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())
}
Expand All @@ -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::<T>),
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::<T>),
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
)
.unwrap();

result.assume_init()
}
Expand All @@ -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 [];
Expand All @@ -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
}
26 changes: 12 additions & 14 deletions crates/neon/src/sys/async_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,24 @@ pub unsafe fn schedule<I, O, D>(
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::<I, O, D>),
Some(call_complete::<I, O, D>),
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::<I, O, D>),
Some(call_complete::<I, O, D>),
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()
}
}
}
Expand Down
39 changes: 15 additions & 24 deletions crates/neon/src/sys/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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::<T>),
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::<T>),
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
)
.unwrap();

result.assume_init()
}
Expand All @@ -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 [];
Expand All @@ -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
}
54 changes: 24 additions & 30 deletions crates/neon/src/sys/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ impl Arguments {
pub unsafe fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool {
let mut target: MaybeUninit<Local> = 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();
Expand All @@ -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
}

Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions crates/neon/src/sys/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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
}
Loading

0 comments on commit 29b92e7

Please sign in to comment.