Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mapping for Exception Types

Rust does not support exceptions. It primarily relies on monads to convey results of operations.

Each exception defined in IDL will be mapped to a struct, as described before. In addition, each exception type should have a typedef for std::result::Result<T, E> where E is the exception type.

All exceptions implement the Error and Display traits. The derived traits follow the same rules as structs, based on member type analysis. See Derived Traits for details.

// IDL
exception MyException {
    string what;
};
#![allow(unused)]
fn main() {
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct MyException {
    pub what: String,
}

impl MyException {
    pub fn new() -> Self {
        Self {
            what: String::new(),
        }
    }
}

impl Default for MyException {
    fn default() -> Self {
        Self::new()
    }
}

pub type MyExceptionResult<T> = Result<T, MyException>;

impl std::fmt::Display for MyException {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "MyException")
    }
}

impl std::error::Error for MyException {}
}