[][src]Function rosy::protected

pub fn protected<F, O>(f: F) -> Result<O> where
    F: FnOnce() -> O, 

Calls f and returns its output or an exception if one is raised in f.

Examples

This is great for calling methods that may not exist:

use rosy::{Object, String, protected};

let string = String::from("¡Hola!");
let result = protected(|| unsafe { string.call("likes_pie?") });

assert!(result.is_err());

Calls can even be nested like so:

use rosy::{Object, String, protected};

let string = String::from("Hiii!!!");

let outer = protected(|| {
    protected(|| unsafe {
        string.call("likes_pie?")
    }).unwrap_err();
    string
});

assert_eq!(outer.unwrap(), string);