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

Commit d40cd7c

Browse files
Add a Status.to_result() and use it throughout instead of match{}
Thanks to @EPashkin for the suggestion.
1 parent a4e7c3f commit d40cd7c

File tree

4 files changed

+20
-42
lines changed

4 files changed

+20
-42
lines changed

src/enums.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ impl Status {
274274
panic!("Cairo error {:?}", self)
275275
}
276276
}
277+
278+
pub(crate) fn to_result<T>(self, obj: T) -> Result<T, Self> {
279+
if self == Status::Success {
280+
Ok(obj)
281+
} else {
282+
Err(self)
283+
}
284+
}
277285
}
278286

279287
#[cfg(feature = "use_glib")]

src/pdf.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,14 @@ impl PdfSurface {
6363
unsafe {
6464
ffi::cairo_pdf_surface_restrict_to_version(self.inner.to_raw_none(), version.into());
6565
}
66-
match self.status() {
67-
Status::Success => Ok(()),
68-
s => Err(s),
69-
}
66+
self.status().to_result(())
7067
}
7168

7269
pub fn set_size(&self, width: f64, height: f64) -> Result<(), Status> {
7370
unsafe {
7471
ffi::cairo_pdf_surface_set_size(self.inner.to_raw_none(), width, height);
7572
}
76-
match self.status() {
77-
Status::Success => Ok(()),
78-
s => Err(s),
79-
}
73+
self.status().to_result(())
8074
}
8175

8276
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
@@ -89,10 +83,7 @@ impl PdfSurface {
8983
value.as_ptr(),
9084
);
9185
}
92-
match self.status() {
93-
Status::Success => Ok(()),
94-
s => Err(s),
95-
}
86+
self.status().to_result(())
9687
}
9788

9889
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
@@ -101,10 +92,7 @@ impl PdfSurface {
10192
unsafe {
10293
ffi::cairo_pdf_surface_set_page_label(self.inner.to_raw_none(), label.as_ptr());
10394
}
104-
match self.status() {
105-
Status::Success => Ok(()),
106-
s => Err(s),
107-
}
95+
self.status().to_result(())
10896
}
10997

11098
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
@@ -116,10 +104,7 @@ impl PdfSurface {
116104
height as _,
117105
);
118106
}
119-
match self.status() {
120-
Status::Success => Ok(()),
121-
s => Err(s),
122-
}
107+
self.status().to_result(())
123108
}
124109

125110
#[cfg(any(all(feature = "pdf", feature = "v1_16"), feature = "dox"))]
@@ -143,10 +128,7 @@ impl PdfSurface {
143128
) as _
144129
};
145130

146-
match self.status() {
147-
Status::Success => Ok(res),
148-
s => Err(s),
149-
}
131+
self.status().to_result(res)
150132
}
151133
}
152134

src/surface.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ impl Surface {
3535
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Result<Surface, Status> {
3636
assert!(!ptr.is_null());
3737
let status = Status::from(ffi::cairo_surface_status(ptr));
38-
match status {
39-
Status::Success => Ok(Surface(ptr, false)),
40-
_ => Err(status),
41-
}
38+
status.to_result(Surface(ptr, false))
4239
}
4340

4441
pub fn to_raw_none(&self) -> *mut ffi::cairo_surface_t {
@@ -120,20 +117,17 @@ impl Surface {
120117

121118
let status = unsafe {
122119
let mime_type = CString::new(mime_type).unwrap();
123-
ffi::cairo_surface_set_mime_data(
120+
Status::from(ffi::cairo_surface_set_mime_data(
124121
self.to_raw_none(),
125122
mime_type.as_ptr(),
126123
data,
127124
size as c_ulong,
128125
Some(unbox::<T>),
129126
user_data as *mut _,
130-
)
127+
))
131128
};
132129

133-
match Status::from(status) {
134-
Status::Success => Ok(()),
135-
x => Err(x),
136-
}
130+
status.to_result(())
137131
}
138132

139133
pub fn supports_mime_type(&self, mime_type: &str) -> bool {

src/xcb.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,7 @@ impl XCBSurface {
342342

343343
pub fn set_size(&self, width: i32, height: i32) -> Result<(), Status> {
344344
unsafe { ffi::cairo_xcb_surface_set_size(self.to_raw_none(), width, height) }
345-
match self.status() {
346-
Status::Success => Ok(()),
347-
s => Err(s),
348-
}
345+
self.status().to_result(())
349346
}
350347

351348
pub fn set_drawable(
@@ -362,10 +359,7 @@ impl XCBSurface {
362359
height,
363360
)
364361
}
365-
match self.status() {
366-
Status::Success => Ok(()),
367-
s => Err(s),
368-
}
362+
self.status().to_result(())
369363
}
370364
}
371365

0 commit comments

Comments
 (0)