Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit d2d86ef

Browse files
Merge pull request #49 from gsingh93/error
Implemented Debug, Display, and Error for glib::Error
2 parents 4e7defe + da484a0 commit d2d86ef

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/error.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// See the COPYRIGHT file at the top-level directory of this distribution.
33
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
44

5+
use std::str;
6+
use std::ffi::CStr;
7+
use std::fmt::{self, Formatter, Debug, Display};
8+
use std::error;
59
use ffi::{self, GQuark};
610
use glib_container::GlibContainer;
711
use translate::ToGlibPtr;
@@ -37,7 +41,7 @@ impl Error {
3741

3842
/// Returns true if error matches domain and code , false otherwise. In particular, when error.pointer
3943
/// is NULL, false will be returned.
40-
///
44+
///
4145
/// If domain contains a FAILED (or otherwise generic) error code, you should generally not check
4246
/// for it explicitly, but should instead treat any not-explicitly-recognized error code as being
4347
/// equivalent to the FAILED code. This way, if the domain is extended in the future to provide a
@@ -59,12 +63,18 @@ impl Error {
5963

6064
/// If other.pointer is NULL, free self ; otherwise, moves self into other. The error variable
6165
/// other.pointer points to must be NULL.
62-
///
66+
///
6367
/// Note that self is no longer valid after this call. If you want to keep using the same Error
6468
/// struct, you need to set it to NULL after calling this function on it.
6569
pub fn propagate(&mut self, other: &Error) -> () {
6670
unsafe { ffi::g_propagate_error(&mut self.pointer, other.pointer) }
6771
}
72+
73+
/// Returns the error message stored in the wrapped GError
74+
pub fn message(&self) -> &str {
75+
let c_str = unsafe { CStr::from_ptr((*self.pointer).message) };
76+
str::from_utf8(c_str.to_bytes()).unwrap()
77+
}
6878
}
6979

7080
impl Clone for Error {
@@ -81,6 +91,24 @@ impl Clone for Error {
8191
}
8292
}
8393

94+
impl Debug for Error {
95+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
96+
write!(f, "{}", self.message())
97+
}
98+
}
99+
100+
impl Display for Error {
101+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
102+
write!(f, "{}", self.message())
103+
}
104+
}
105+
106+
impl error::Error for Error {
107+
fn description(&self) -> &str {
108+
self.message()
109+
}
110+
}
111+
84112
impl Drop for Error {
85113
fn drop(&mut self) {
86114
self.release();

0 commit comments

Comments
 (0)