implement fmt::Display for Error

This commit is contained in:
Terence Lee 2017-10-05 00:11:19 -05:00
parent 37b2f2f3e9
commit aec4d726e1
No known key found for this signature in database
GPG Key ID: FB54F2C65BE915C7
1 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,5 @@
use super::{Class, ToRuby};
use std::{any};
use std::{any, fmt};
use sys::{VALUE, SPRINTF_TO_S, c_string, rb_eRuntimeError, rb_raise};
#[derive(Copy, Clone, Debug)]
@ -43,6 +43,21 @@ impl Error {
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.message {
ErrorMessage::Static(c_string) => {
use ::std::ffi::CStr;
write!(f, "{}", unsafe { CStr::from_ptr(c_string) }.to_str().unwrap())
},
ErrorMessage::Dynamic(value) => {
use super::FromRuby;
write!(f, "{}", String::from_ruby_unwrap(value))
}
}
}
}
unsafe impl Send for Error {}
unsafe impl Sync for Error {}