-
-
Notifications
You must be signed in to change notification settings - Fork 58
cairo_surface_create*() can return errors #141
Comments
Seems like so. Good suggestion! |
Are API-breaking PRs accepted? :) |
Yup, we already have a few. The next release will be a "middle" one. |
I have a question about possible APIs. Let's say you do
Cairo will return a surface with a status of CAIRO_STATUS_INVALID_SIZE, as pixman only supports sizes up to 32767. For a valid size, it could return CAIRO_STATUS_NO_MEMORY. Also, At first I was thinking that the cairo-rs functions that create surfaces could simply return
But then I thought, that's kind of asymmetrical. What if we instead had
Note that both Ok() and Err() have an ImageSurface inside them. The guarantee then would be that if Ok(surf), then the surface's status is Success, and if Err(surf), then it has an error status and the surface is essentially unusable (you can call Cairo methods on it, but they won't do anything). As before, Opinions are appreciated :) |
The API is looking like this: federicomenaquintero@7f79123 |
API look good, but not sure if it works good with all surfaces. |
After talking with @GuillaumeGomez on IRC, I pushed an updated version to my repository. This one returns Result<ImageSurface, Status>. The code is at https://github.com/federicomenaquintero/cairo/tree/image-surface-error |
LGFM. |
Pull request for cairo - #146 Pull request for lgpl-docs - gtk-rs/lgpl-docs#30 |
Closing now that this is in place. |
…esult Cairo's surface creation functions never return NULL; instead they always return a surface, but it may be in an error state. In gtk-rs#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 gtk-rs#251
…esult Cairo's surface creation functions never return NULL; instead they always return a surface, but it may be in an error state. In gtk-rs#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() Device.surface_create() Device.surface_create_for_target() PdfSurface::new() RecordingSurface::create() RecordingSurface::from_raw_full() SvgSurface::new() XCBSurface::create() XCBSurface::create_for_bitmap() XCBSurface::create_with_xrender_format() From macro for_stream_constructors!: *::for_stream() *::for_raw_stream() 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 gtk-rs#251
Cairo's surface creation functions can return errors in the form of a valid cairo_surface_t* but whose cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS.
For example, cairo_image_surface_create() can return CAIRO_STATUS_INVALID_SIZE if the passed size is greater than what Pixman supports, or it can return CAIRO_STATUS_NO_MEMORY if allocation fails.
Cairo-rs similarly returns a valid Surface (or ImageSurface) whose status must be checked before proceeding.
Should we return a Result<Surface, Status> (or the corresponding Result<ImageSurface, Status>) instead?
The text was updated successfully, but these errors were encountered: