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

Commit e72b056

Browse files
Merge pull request #287 from federicomenaquintero/surface-create-similar-result
(#251): Surface::create_similar() and friends should return a Result
2 parents 7837ef6 + 1794024 commit e72b056

15 files changed

+271
-674
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ matrix:
1515
rust: beta
1616
env: GTK=3.24 FEATURES=
1717
- os: linux
18-
rust: 1.36.0
18+
rust: 1.39.0
1919
env: GTK=3.14 FEATURES=
2020
- os: linux
21-
rust: 1.36.0
21+
rust: 1.39.0
2222
env: GTK=3.24 FEATURES=
2323
- os: osx
2424
rust: nightly

src/device.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,30 @@ 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(
85-
self.to_raw_none(),
86-
target.to_raw_none(),
87-
);
88-
if p.is_null() {
89-
None
90-
} else {
91-
Some(Surface::from_raw_full(p))
92-
}
88+
Ok(Surface::from_raw_full(
89+
ffi::cairo_script_surface_create_for_target(
90+
self.to_raw_none(),
91+
target.to_raw_none(),
92+
),
93+
)?)
9394
}
9495
}
9596

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/image_surface.rs

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,9 @@ use surface::Surface;
1717
use BorrowError;
1818
use Status;
1919

20-
#[derive(Debug)]
21-
pub struct ImageSurface(Surface);
22-
23-
impl TryFrom<Surface> for ImageSurface {
24-
type Error = Surface;
25-
26-
fn try_from(surface: Surface) -> Result<ImageSurface, Surface> {
27-
if surface.get_type() == SurfaceType::Image {
28-
Ok(ImageSurface(surface))
29-
} else {
30-
Err(surface)
31-
}
32-
}
33-
}
20+
declare_surface!(ImageSurface, SurfaceType::Image);
3421

3522
impl ImageSurface {
36-
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-
}
43-
}
44-
4523
pub fn create(format: Format, width: i32, height: i32) -> Result<ImageSurface, Status> {
4624
unsafe {
4725
Self::from_raw_full(ffi::cairo_image_surface_create(
@@ -119,73 +97,6 @@ impl ImageSurface {
11997
}
12098
}
12199

122-
#[cfg(feature = "use_glib")]
123-
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for ImageSurface {
124-
type Storage = &'a Surface;
125-
126-
#[inline]
127-
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_surface_t, Self> {
128-
let stash = self.0.to_glib_none();
129-
Stash(stash.0, stash.1)
130-
}
131-
132-
#[inline]
133-
fn to_glib_full(&self) -> *mut ffi::cairo_surface_t {
134-
unsafe { ffi::cairo_surface_reference(self.0.to_glib_none().0) }
135-
}
136-
}
137-
138-
#[cfg(feature = "use_glib")]
139-
impl FromGlibPtrNone<*mut ffi::cairo_surface_t> for ImageSurface {
140-
#[inline]
141-
unsafe fn from_glib_none(ptr: *mut ffi::cairo_surface_t) -> ImageSurface {
142-
Self::try_from(from_glib_none::<_, Surface>(ptr)).unwrap()
143-
}
144-
}
145-
146-
#[cfg(feature = "use_glib")]
147-
impl FromGlibPtrBorrow<*mut ffi::cairo_surface_t> for ImageSurface {
148-
#[inline]
149-
unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_surface_t) -> ImageSurface {
150-
Self::try_from(from_glib_borrow::<_, Surface>(ptr)).unwrap()
151-
}
152-
}
153-
154-
#[cfg(feature = "use_glib")]
155-
impl FromGlibPtrFull<*mut ffi::cairo_surface_t> for ImageSurface {
156-
#[inline]
157-
unsafe fn from_glib_full(ptr: *mut ffi::cairo_surface_t) -> ImageSurface {
158-
Self::from_raw_full(ptr).unwrap()
159-
}
160-
}
161-
162-
#[cfg(feature = "use_glib")]
163-
gvalue_impl!(
164-
ImageSurface,
165-
ffi::cairo_surface_t,
166-
ffi::gobject::cairo_gobject_surface_get_type
167-
);
168-
169-
impl Deref for ImageSurface {
170-
type Target = Surface;
171-
172-
fn deref(&self) -> &Surface {
173-
&self.0
174-
}
175-
}
176-
177-
impl Clone for ImageSurface {
178-
fn clone(&self) -> ImageSurface {
179-
ImageSurface(self.0.clone())
180-
}
181-
}
182-
183-
impl fmt::Display for ImageSurface {
184-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185-
write!(f, "ImageSurface")
186-
}
187-
}
188-
189100
#[derive(Debug)]
190101
pub struct ImageSurfaceData<'a> {
191102
surface: &'a mut ImageSurface,

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ pub use xcb::{
158158
XCBVisualType,
159159
};
160160

161+
#[macro_use]
162+
mod surface_macros;
161163
#[macro_use]
162164
mod user_data;
163165
mod constants;

0 commit comments

Comments
 (0)