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

(#251): Surface::create_similar() and friends should return a Result #287

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ matrix:
rust: beta
env: GTK=3.24 FEATURES=
- os: linux
rust: 1.36.0
rust: 1.39.0
env: GTK=3.14 FEATURES=
- os: linux
rust: 1.36.0
rust: 1.39.0
env: GTK=3.24 FEATURES=
- os: osx
rust: nightly
Expand Down
37 changes: 19 additions & 18 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,30 @@ impl Device {
unsafe { ffi::cairo_script_set_mode(self.to_raw_none(), mode.into()) }
}

pub fn surface_create(&self, content: Content, width: f64, height: f64) -> Option<Surface> {
pub fn surface_create(
&self,
content: Content,
width: f64,
height: f64,
) -> Result<Surface, Status> {
unsafe {
let p =
ffi::cairo_script_surface_create(self.to_raw_none(), content.into(), width, height);
if p.is_null() {
None
} else {
Some(Surface::from_raw_full(p))
}
Ok(Surface::from_raw_full(ffi::cairo_script_surface_create(
self.to_raw_none(),
content.into(),
width,
height,
))?)
}
}

pub fn surface_create_for_target(&self, target: &Surface) -> Option<Surface> {
pub fn surface_create_for_target(&self, target: &Surface) -> Result<Surface, Status> {
unsafe {
let p = ffi::cairo_script_surface_create_for_target(
self.to_raw_none(),
target.to_raw_none(),
);
if p.is_null() {
None
} else {
Some(Surface::from_raw_full(p))
}
Ok(Surface::from_raw_full(
ffi::cairo_script_surface_create_for_target(
self.to_raw_none(),
target.to_raw_none(),
),
)?)
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ impl Status {
panic!("Cairo error {:?}", self)
}
}

pub(crate) fn to_result<T>(self, obj: T) -> Result<T, Self> {
if self == Status::Success {
Ok(obj)
} else {
Err(self)
}
}
}

#[cfg(feature = "use_glib")]
Expand Down
91 changes: 1 addition & 90 deletions src/image_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,9 @@ use surface::Surface;
use BorrowError;
use Status;

#[derive(Debug)]
pub struct ImageSurface(Surface);

impl TryFrom<Surface> for ImageSurface {
type Error = Surface;

fn try_from(surface: Surface) -> Result<ImageSurface, Surface> {
if surface.get_type() == SurfaceType::Image {
Ok(ImageSurface(surface))
} else {
Err(surface)
}
}
}
declare_surface!(ImageSurface, SurfaceType::Image);

impl ImageSurface {
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Result<ImageSurface, Status> {
let surface = Self::try_from(Surface::from_raw_full(ptr)).unwrap();
let status = surface.status();
match status {
Status::Success => Ok(surface),
_ => Err(status),
}
}

pub fn create(format: Format, width: i32, height: i32) -> Result<ImageSurface, Status> {
unsafe {
Self::from_raw_full(ffi::cairo_image_surface_create(
Expand Down Expand Up @@ -119,73 +97,6 @@ impl ImageSurface {
}
}

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for ImageSurface {
type Storage = &'a Surface;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_surface_t, Self> {
let stash = self.0.to_glib_none();
Stash(stash.0, stash.1)
}

#[inline]
fn to_glib_full(&self) -> *mut ffi::cairo_surface_t {
unsafe { ffi::cairo_surface_reference(self.0.to_glib_none().0) }
}
}

#[cfg(feature = "use_glib")]
impl FromGlibPtrNone<*mut ffi::cairo_surface_t> for ImageSurface {
#[inline]
unsafe fn from_glib_none(ptr: *mut ffi::cairo_surface_t) -> ImageSurface {
Self::try_from(from_glib_none::<_, Surface>(ptr)).unwrap()
}
}

#[cfg(feature = "use_glib")]
impl FromGlibPtrBorrow<*mut ffi::cairo_surface_t> for ImageSurface {
#[inline]
unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_surface_t) -> ImageSurface {
Self::try_from(from_glib_borrow::<_, Surface>(ptr)).unwrap()
}
}

#[cfg(feature = "use_glib")]
impl FromGlibPtrFull<*mut ffi::cairo_surface_t> for ImageSurface {
#[inline]
unsafe fn from_glib_full(ptr: *mut ffi::cairo_surface_t) -> ImageSurface {
Self::from_raw_full(ptr).unwrap()
}
}

#[cfg(feature = "use_glib")]
gvalue_impl!(
ImageSurface,
ffi::cairo_surface_t,
ffi::gobject::cairo_gobject_surface_get_type
);

impl Deref for ImageSurface {
type Target = Surface;

fn deref(&self) -> &Surface {
&self.0
}
}

impl Clone for ImageSurface {
fn clone(&self) -> ImageSurface {
ImageSurface(self.0.clone())
}
}

impl fmt::Display for ImageSurface {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ImageSurface")
}
}

#[derive(Debug)]
pub struct ImageSurfaceData<'a> {
surface: &'a mut ImageSurface,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub use enums::*;
pub use error::{BorrowError, IoError};

pub use patterns::{
Gradient, LinearGradient, Mesh, Pattern, RadialGradient, SolidPattern, SurfacePattern
Gradient, LinearGradient, Mesh, Pattern, RadialGradient, SolidPattern, SurfacePattern,
};

pub use font::{
Expand Down Expand Up @@ -120,6 +120,8 @@ pub use xcb::{
XCBVisualType,
};

#[macro_use]
mod surface_macros;
#[macro_use]
mod user_data;
mod constants;
Expand Down
1 change: 0 additions & 1 deletion src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,4 @@ mod tests {
],
);
}

}
Loading