|
| 1 | +//! NIFs that intentionally return errors, panic, and raise exceptions. |
| 2 | +//! |
| 3 | +//! These are intended for testing the async message passing code and require the |
| 4 | +//! `testing-nifs` feature flag to be enabled. |
| 5 | +
|
| 6 | +use rustler::{Env, Error}; |
| 7 | + |
| 8 | +use crate::async_reply::{AsyncReply, try_reply_async}; |
| 9 | + |
| 10 | +#[rustler::nif] |
| 11 | +pub fn async_panic(env: Env, msg: Option<String>) -> AsyncReply { |
| 12 | + try_reply_async(env, async move { |
| 13 | + if let Some(msg) = msg { |
| 14 | + panic!("{msg}"); |
| 15 | + } else { |
| 16 | + panic!() |
| 17 | + } |
| 18 | + |
| 19 | + // Needed to indicate return type |
| 20 | + #[allow(unreachable_code)] |
| 21 | + Ok(()) |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +#[rustler::nif] |
| 26 | +pub fn async_error<'e>(env: Env<'e>, s: String, atom: bool) -> AsyncReply<'e> { |
| 27 | + try_reply_async(env, async move { |
| 28 | + Result::<(), _>::Err(if atom { |
| 29 | + Error::Atom(String::leak(s)) |
| 30 | + } else { |
| 31 | + Error::Term(Box::new(s)) |
| 32 | + }) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +#[rustler::nif] |
| 37 | +pub fn async_raise<'e>(env: Env<'e>, s: String, atom: bool) -> AsyncReply<'e> { |
| 38 | + try_reply_async(env, async move { |
| 39 | + Result::<(), _>::Err(if atom { |
| 40 | + Error::RaiseAtom(String::leak(s)) |
| 41 | + } else { |
| 42 | + Error::RaiseTerm(Box::new(s)) |
| 43 | + }) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +#[rustler::nif] |
| 48 | +pub fn async_badarg<'e>(env: Env<'e>) -> AsyncReply<'e> { |
| 49 | + try_reply_async(env, async move { Result::<(), _>::Err(Error::BadArg) }) |
| 50 | +} |
0 commit comments