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

Commit f4997e6

Browse files
(#251): Surface::create_similar() and friends should return a Result
Cairo's surface creation functions never return NULL; instead they always return a surface, but it may be in an error state. In #141 we started making the binding functions return Result for this; some returned the plain FooSurface type, some others Option<FooSurface>. This makes the following functions return Result<FooSurface, Status> Surface::create_similar() Surface::create_similar_image() RecordingSurface::create() Device.surface_create() Device.surface_create_for_target() The foundation for all of this is that Surface::from_raw_full() now also returns Result<FooSurface, Status>. This is to make things consistent with ImageSurface::from_raw_full(). Analogously, we now have RecordingSurface::from_raw_full() that also returns Result. Fixes #251
1 parent ce6d24b commit f4997e6

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed

src/device.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,28 @@ impl Device {
6767
unsafe { ffi::cairo_script_set_mode(self.to_raw_none(), mode.into()) }
6868
}
6969

70-
pub fn surface_create(&self, content: Content, width: f64, height: f64) -> Option<Surface> {
70+
pub fn surface_create(
71+
&self,
72+
content: Content,
73+
width: f64,
74+
height: f64,
75+
) -> Result<Surface, Status> {
7176
unsafe {
72-
let p =
73-
ffi::cairo_script_surface_create(self.to_raw_none(), content.into(), width, height);
74-
if p.is_null() {
75-
None
76-
} else {
77-
Some(Surface::from_raw_full(p))
78-
}
77+
Ok(Surface::from_raw_full(ffi::cairo_script_surface_create(
78+
self.to_raw_none(),
79+
content.into(),
80+
width,
81+
height,
82+
))?)
7983
}
8084
}
8185

82-
pub fn surface_create_for_target(&self, target: &Surface) -> Option<Surface> {
86+
pub fn surface_create_for_target(&self, target: &Surface) -> Result<Surface, Status> {
8387
unsafe {
84-
let p = ffi::cairo_script_surface_create_for_target(
88+
Ok(Surface::from_raw_full(ffi::cairo_script_surface_create_for_target(
8589
self.to_raw_none(),
8690
target.to_raw_none(),
87-
);
88-
if p.is_null() {
89-
None
90-
} else {
91-
Some(Surface::from_raw_full(p))
92-
}
91+
))?)
9392
}
9493
}
9594

src/image_surface.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ impl TryFrom<Surface> for ImageSurface {
3434

3535
impl ImageSurface {
3636
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Result<ImageSurface, Status> {
37-
let surface = Self::try_from(Surface::from_raw_full(ptr)).unwrap();
38-
let status = surface.status();
39-
match status {
40-
Status::Success => Ok(surface),
41-
_ => Err(status),
42-
}
37+
let surface = Surface::from_raw_full(ptr)?;
38+
Self::try_from(surface).map_err(|_| Status::SurfaceTypeMismatch)
4339
}
4440

4541
pub fn create(format: Format, width: i32, height: i32) -> Result<ImageSurface, Status> {

src/recording_surface.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::convert::TryFrom;
66
use std::fmt;
77
use std::ops::Deref;
88

9-
use enums::{Content, SurfaceType};
9+
use enums::{Content, Status, SurfaceType};
1010
use ffi;
1111
#[cfg(feature = "use_glib")]
1212
use glib::translate::*;
@@ -30,22 +30,28 @@ impl TryFrom<Surface> for RecordingSurface {
3030
}
3131

3232
impl RecordingSurface {
33+
pub unsafe fn from_raw_full(
34+
ptr: *mut ffi::cairo_surface_t,
35+
) -> Result<RecordingSurface, Status> {
36+
let surface = Surface::from_raw_full(ptr)?;
37+
Self::try_from(surface).map_err(|_| Status::SurfaceTypeMismatch)
38+
}
39+
3340
pub fn create<T: Into<Option<Rectangle>>>(
3441
content: Content,
3542
extends: T,
36-
) -> Option<RecordingSurface> {
43+
) -> Result<RecordingSurface, Status> {
3744
unsafe {
3845
let extends = extends.into();
3946
let extends = match extends {
4047
Some(c) => c.to_raw_none(),
4148
None => ::std::ptr::null(),
4249
};
43-
let p = ffi::cairo_recording_surface_create(content.into(), extends);
44-
if p.is_null() {
45-
None
46-
} else {
47-
Some(RecordingSurface(Surface::from_raw_full(p)))
48-
}
50+
51+
Ok(Self::from_raw_full(ffi::cairo_recording_surface_create(
52+
content.into(),
53+
extends,
54+
))?)
4955
}
5056
}
5157

@@ -101,7 +107,7 @@ impl FromGlibPtrBorrow<*mut ffi::cairo_surface_t> for RecordingSurface {
101107
impl FromGlibPtrFull<*mut ffi::cairo_surface_t> for RecordingSurface {
102108
#[inline]
103109
unsafe fn from_glib_full(ptr: *mut ffi::cairo_surface_t) -> RecordingSurface {
104-
RecordingSurface(Surface::from_raw_full(ptr))
110+
Self::from_raw_full(ptr).unwrap()
105111
}
106112
}
107113

src/surface.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,25 @@ impl Surface {
3232
Surface(ptr, true)
3333
}
3434

35-
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Surface {
35+
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Result<Surface, Status> {
3636
assert!(!ptr.is_null());
37-
Surface(ptr, false)
37+
let status = Status::from(ffi::cairo_surface_status(ptr));
38+
match status {
39+
Status::Success => Ok(Surface(ptr, false)),
40+
_ => Err(status),
41+
}
3842
}
3943

4044
pub fn to_raw_none(&self) -> *mut ffi::cairo_surface_t {
4145
self.0
4246
}
4347

44-
pub fn create_similar(&self, content: Content, width: i32, height: i32) -> Surface {
48+
pub fn create_similar(
49+
&self,
50+
content: Content,
51+
width: i32,
52+
height: i32
53+
) -> Result<Surface, Status> {
4554
unsafe {
4655
Self::from_raw_full(ffi::cairo_surface_create_similar(
4756
self.0,
@@ -185,19 +194,14 @@ impl Surface {
185194
(x_pixels_per_inch, y_pixels_per_inch)
186195
}
187196

188-
pub fn create_similar_image(&self, format: Format, width: i32, height: i32) -> Option<Surface> {
197+
pub fn create_similar_image(&self, format: Format, width: i32, height: i32) -> Result<Surface, Status> {
189198
unsafe {
190-
let p = ffi::cairo_surface_create_similar_image(
199+
Self::from_raw_full(ffi::cairo_surface_create_similar_image(
191200
self.to_raw_none(),
192201
format.into(),
193202
width,
194203
height,
195-
);
196-
if p.is_null() {
197-
None
198-
} else {
199-
Some(Self::from_raw_full(p))
200-
}
204+
))
201205
}
202206
}
203207

@@ -258,7 +262,7 @@ impl FromGlibPtrBorrow<*mut ffi::cairo_surface_t> for Surface {
258262
impl FromGlibPtrFull<*mut ffi::cairo_surface_t> for Surface {
259263
#[inline]
260264
unsafe fn from_glib_full(ptr: *mut ffi::cairo_surface_t) -> Surface {
261-
Self::from_raw_full(ptr)
265+
Self::from_raw_full(ptr).unwrap()
262266
}
263267
}
264268

0 commit comments

Comments
 (0)