1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#![feature(termination_trait_lib)] #![feature(try_trait)] #![feature(specialization)] use std::process::Termination; use std::ops::Try; pub enum Exit<T> { Ok, Err(T) } pub trait ExitDisplay { fn display(&self) -> String; } impl<T: Into<i32>> Termination for Exit<T> { default fn report(self) -> i32 { match self { Exit::Ok => 0, Exit::Err(err) => { err.into() }, } } } impl<T: Into<i32> + ExitDisplay> Termination for Exit<T> { fn report(self) -> i32 { match self { Exit::Ok => 0, Exit::Err(err) => { eprintln!("{}", err.display()); err.into() }, } } } impl<T> Try for Exit<T> { type Ok = (); type Error = T; fn into_result(self) -> Result<<Self as Try>::Ok, Self::Error> { match self { Exit::Ok => Ok(()), Exit::Err(err) => Err(err) } } fn from_error(err: Self::Error) -> Self { Exit::Err(err) } fn from_ok(_: <Self as Try>::Ok) -> Self { Exit::Ok } }