Created
September 23, 2017 02:13
-
-
Save segfo/5b1677f6a81f2729090f1f913f14dc93 to your computer and use it in GitHub Desktop.
自作エラー(FFIで受け取ったり定義したり)とRustで定義されているエラーを混在させて"println!({},e)"する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::fmt::Display; | |
use std::fmt::Formatter; | |
// オレオレエラー列挙体 | |
#[derive(Debug)] | |
enum myError{ | |
RustError(io::Error), | |
FileError(myFileError), | |
SocketError(mySocketError) | |
} | |
// オレオレエラーコード定義その1(FFIでC言語から上がってくるint型のエラーとか) | |
#[derive(Debug)] | |
enum mySocketError{ | |
WriteError,ReadError | |
} | |
// オレオレエラーコード定義その2(FFIでC言語から上がってくるint型のエラーとか) | |
#[derive(Debug)] | |
enum myFileError{ | |
ReadError,WriteError | |
} | |
// オレオレエラー時に出力するメッセージを定義 | |
// どんなエラーかは書かずに、 | |
impl Display for myError{ | |
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result{ | |
match *self { | |
myError::RustError(ref e)=> print!("Rust {}",e), | |
myError::FileError(ref e) => print!("File {}",e), | |
myError::SocketError(ref e)=>print!("Socket {}",e) | |
} | |
Ok(()) | |
} | |
} | |
// Rustが持っているエラーをmyErrorへ変換する処理 | |
impl std::convert::From<io::Error> for myError{ | |
fn from(e: io::Error) -> Self { | |
myError::RustError(e) | |
} | |
} | |
// ----------------------------------- | |
// 独自エラーその1についての処理 | |
// メッセージ表示処理 | |
impl Display for myFileError{ | |
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result{ | |
match *self { | |
myFileError::ReadError => print!("read error"), | |
myFileError::WriteError => print!("write error"), | |
} | |
Ok(()) | |
} | |
} | |
// myErrorへ変換する処理 | |
impl std::convert::From<myFileError> for myError{ | |
fn from(e: myFileError) -> Self { | |
myError::FileError(e) | |
} | |
} | |
// ----------------------------------- | |
// 独自エラーその2についての処理 | |
// メッセージ表示処理 | |
impl Display for mySocketError{ | |
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result{ | |
match *self { | |
mySocketError::ReadError => print!("read error"), | |
mySocketError::WriteError => print!("write error"), | |
} | |
Ok(()) | |
} | |
} | |
// myErrorへ変換する処理 | |
impl std::convert::From<mySocketError> for myError{ | |
fn from(e: mySocketError) -> Self { | |
myError::SocketError(e) | |
} | |
} | |
// エラーの定義・変換処理はここまで | |
// ------------------------------------ | |
// orig_XXX_e は独自エラーを返却する関数 | |
fn orig_file_e()->Result<(),myFileError>{ | |
Err(myFileError::ReadError) | |
} | |
fn orig_sock_e()->Result<(),mySocketError>{ | |
Err(mySocketError::WriteError) | |
} | |
// Rustで定義されているエラーを返却する関数 | |
fn rust_e()->Result<(),io::Error>{ | |
Err(io::Error::last_os_error()) | |
} | |
// 独自エラーとRustが標準で持っているエラーを返却する関数を引数に応じて呼ぶ | |
fn f(r:u32)->Result<(),myError>{ | |
match r{ | |
0=> orig_file_e()?, | |
1=> orig_sock_e()?, | |
_=> rust_e()? | |
} | |
Ok(()) | |
} | |
// 各処理を呼んで見る | |
// File read error | |
// Socket write error | |
// Rust この操作を正しく終了しました。 (os error 0) | |
// | |
// ↑みたいに表示される | |
fn main() { | |
for i in 0..3{ | |
match f(i){ | |
Ok(_)=>{}, | |
Err(e)=>println!("error : {}",e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment