denoland / deno_bindgen

Write high-level Deno FFI libraries in Rust.
MIT License
274 stars 28 forks source link

Result<T>,Option<T> & Exceptions #139

Open nearest-river opened 9 months ago

nearest-river commented 9 months ago

It would be great if we could throw exception from rust..

#[deno_bindgen]
pub fn div(a: i32,b: i32)-> i32 {
  if b==0 {
    deno_bindgen::throw("cannot divide by zero")
  } else {
    a/b
  }
}

And an Exception trait would be great just like wasm_bindgen

trait Exception<T> {
  fn unrwap_or_throw(self)-> T;
}

impl<T,E> Exception<T> for Result<T,E> {...}
impl<T> Exception<T> for Option<T> {...}

 #[deno_bindgen]
pub fn read(path: &str)-> String {
  std::fs::read_to_string(path).unwrap_or_throw()
}