Skip to content

Don't short-circuit in ffi_try #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Bitmap {

impl Bitmap {
pub fn from_pixmap(pixmap: &Pixmap) -> Result<Self, Error> {
let inner = unsafe { ffi_try!(mupdf_new_bitmap_from_pixmap(context(), pixmap.inner)) };
Ok(Self { inner })
unsafe { ffi_try!(mupdf_new_bitmap_from_pixmap(context(), pixmap.inner)) }
.map(|inner| Self { inner })
}

/// Width of the region in pixels.
Expand Down
12 changes: 6 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl FromStr for Buffer {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let c_str = CString::new(s)?;
let inner = unsafe { ffi_try!(mupdf_buffer_from_str(context(), c_str.as_ptr())) };
Ok(Self { inner, offset: 0 })
unsafe { ffi_try!(mupdf_buffer_from_str(context(), c_str.as_ptr())) }
.map(|inner| Self { inner, offset: 0 })
}
}

Expand All @@ -39,8 +39,8 @@ impl Buffer {

pub fn from_base64(str: &str) -> Result<Self, Error> {
let c_str = CString::new(str)?;
let inner = unsafe { ffi_try!(mupdf_buffer_from_base64(context(), c_str.as_ptr())) };
Ok(Self { inner, offset: 0 })
unsafe { ffi_try!(mupdf_buffer_from_base64(context(), c_str.as_ptr())) }
.map(|inner| Self { inner, offset: 0 })
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Buffer {
buf.as_mut_ptr(),
len
))
};
}?;
self.offset += read_len as usize;
Ok(read_len)
}
Expand All @@ -92,7 +92,7 @@ impl Buffer {
buf.as_ptr(),
len
))
};
}?;
Ok(len)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/colorspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Colorspace {
out.as_mut_ptr(),
via,
params.into()
));
))?;
out.set_len(to_n);
Ok(out)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Colorspace {
out.as_mut_ptr(),
via,
params.into()
));
))?;
Ok(n)
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ pub struct Cookie {

impl Cookie {
pub fn new() -> Result<Self, Error> {
let inner = unsafe { ffi_try!(mupdf_new_cookie(context())) };
Ok(Self { inner })
unsafe { ffi_try!(mupdf_new_cookie(context())) }.map(|inner| Self { inner })
}

/// Abort rendering
Expand Down
113 changes: 37 additions & 76 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,34 +187,34 @@ impl Device {
}

pub fn from_pixmap_with_clip(pixmap: &Pixmap, clip: IRect) -> Result<Self, Error> {
let dev = unsafe { ffi_try!(mupdf_new_draw_device(context(), pixmap.inner, clip.into())) };
Ok(Self {
dev,
list: ptr::null_mut(),
})
unsafe { ffi_try!(mupdf_new_draw_device(context(), pixmap.inner, clip.into())) }.map(
|dev| Self {
dev,
list: ptr::null_mut(),
},
)
}

pub fn from_pixmap(pixmap: &Pixmap) -> Result<Self, Error> {
Self::from_pixmap_with_clip(pixmap, IRect::INF)
}

pub fn from_display_list(list: &DisplayList) -> Result<Self, Error> {
let dev = unsafe { ffi_try!(mupdf_new_display_list_device(context(), list.inner)) };
Ok(Self {
unsafe { ffi_try!(mupdf_new_display_list_device(context(), list.inner)) }.map(|dev| Self {
dev,
list: list.inner,
})
}

pub fn from_text_page(page: &TextPage, opts: TextPageOptions) -> Result<Self, Error> {
let dev = unsafe {
unsafe {
ffi_try!(mupdf_new_stext_device(
context(),
page.inner,
opts.bits() as _
))
};
Ok(Self {
}
.map(|dev| Self {
dev,
list: ptr::null_mut(),
})
Expand Down Expand Up @@ -242,9 +242,8 @@ impl Device {
color.as_ptr(),
alpha,
cp.into()
));
))
}
Ok(())
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -269,9 +268,8 @@ impl Device {
color.as_ptr(),
alpha,
cp.into()
));
))
}
Ok(())
}

pub fn clip_path(&self, path: &Path, even_odd: bool, ctm: &Matrix) -> Result<(), Error> {
Expand All @@ -282,9 +280,8 @@ impl Device {
path.inner,
even_odd,
ctm.into()
));
))
}
Ok(())
}

pub fn clip_stroke_path(
Expand All @@ -300,9 +297,8 @@ impl Device {
path.inner,
stroke.inner,
ctm.into()
));
))
}
Ok(())
}

pub fn fill_text(
Expand All @@ -324,9 +320,8 @@ impl Device {
color.as_ptr(),
alpha,
cp.into()
));
))
}
Ok(())
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -351,14 +346,12 @@ impl Device {
color.as_ptr(),
alpha,
cp.into()
));
))
}
Ok(())
}

pub fn clip_text(&self, text: &Text, ctm: &Matrix) -> Result<(), Error> {
unsafe { ffi_try!(mupdf_clip_text(context(), self.dev, text.inner, ctm.into())) }
Ok(())
}

pub fn clip_stroke_text(
Expand All @@ -374,9 +367,8 @@ impl Device {
text.inner,
stroke.inner,
ctm.into()
));
))
}
Ok(())
}

pub fn ignore_text(&self, text: &Text, ctm: &Matrix) -> Result<(), Error> {
Expand All @@ -386,9 +378,8 @@ impl Device {
self.dev,
text.inner,
ctm.into()
));
))
}
Ok(())
}

pub fn fill_shade(
Expand All @@ -406,9 +397,8 @@ impl Device {
ctm.into(),
alpha,
cp.into()
));
))
}
Ok(())
}

pub fn fill_image(
Expand All @@ -426,9 +416,8 @@ impl Device {
ctm.into(),
alpha,
cp.into()
));
))
}
Ok(())
}

pub fn fill_image_mask(
Expand All @@ -450,9 +439,8 @@ impl Device {
color.as_ptr(),
alpha,
cp.into()
));
))
}
Ok(())
}

pub fn clip_image_mask(&self, image: &Image, ctm: &Matrix) -> Result<(), Error> {
Expand All @@ -462,16 +450,12 @@ impl Device {
self.dev,
image.inner,
ctm.into()
));
))
}
Ok(())
}

pub fn pop_clip(&self) -> Result<(), Error> {
unsafe {
ffi_try!(mupdf_pop_clip(context(), self.dev));
}
Ok(())
unsafe { ffi_try!(mupdf_pop_clip(context(), self.dev)) }
}

pub fn begin_mask(
Expand All @@ -491,9 +475,8 @@ impl Device {
cs.inner,
bc.as_ptr(),
cp.into()
));
))
}
Ok(())
}

pub fn end_mask(&self, f: Option<&Function>) -> Result<(), Error> {
Expand All @@ -502,9 +485,8 @@ impl Device {
context(),
self.dev,
f.map_or(ptr::null_mut(), |f| f.inner)
));
))
}
Ok(())
}

pub fn begin_group(
Expand All @@ -526,16 +508,12 @@ impl Device {
knockout,
blend_mode as _,
alpha
));
))
}
Ok(())
}

pub fn end_group(&self) -> Result<(), Error> {
unsafe {
ffi_try!(mupdf_end_group(context(), self.dev));
}
Ok(())
unsafe { ffi_try!(mupdf_end_group(context(), self.dev)) }
}

pub fn begin_tile(
Expand All @@ -547,7 +525,7 @@ impl Device {
ctm: &Matrix,
id: Option<NonZero<i32>>,
) -> Result<Option<NonZero<i32>>, Error> {
let i = unsafe {
unsafe {
ffi_try!(mupdf_begin_tile(
context(),
self.dev,
Expand All @@ -558,30 +536,21 @@ impl Device {
ctm.into(),
id.map_or(0, NonZero::get)
))
};
Ok(NonZero::new(i))
}
.map(NonZero::new)
}

pub fn end_tile(&self) -> Result<(), Error> {
unsafe {
ffi_try!(mupdf_end_tile(context(), self.dev));
}
Ok(())
unsafe { ffi_try!(mupdf_end_tile(context(), self.dev)) }
}

pub fn begin_layer(&self, name: &str) -> Result<(), Error> {
let c_name = CString::new(name)?;
unsafe {
ffi_try!(mupdf_begin_layer(context(), self.dev, c_name.as_ptr()));
}
Ok(())
unsafe { ffi_try!(mupdf_begin_layer(context(), self.dev, c_name.as_ptr())) }
}

pub fn end_layer(&self) -> Result<(), Error> {
unsafe {
ffi_try!(mupdf_end_layer(context(), self.dev));
}
Ok(())
unsafe { ffi_try!(mupdf_end_layer(context(), self.dev)) }
}

pub fn begin_structure(&self, standard: Structure, raw: &str, idx: i32) -> Result<(), Error> {
Expand All @@ -593,16 +562,12 @@ impl Device {
standard as _,
c_raw.as_ptr(),
idx as _
));
))
}
Ok(())
}

pub fn end_structure(&self) -> Result<(), Error> {
unsafe {
ffi_try!(mupdf_end_structure(context(), self.dev));
}
Ok(())
unsafe { ffi_try!(mupdf_end_structure(context(), self.dev)) }
}

pub fn begin_metatext(&self, meta: Metatext, text: &str) -> Result<(), Error> {
Expand All @@ -613,16 +578,12 @@ impl Device {
self.dev,
meta as _,
c_text.as_ptr()
));
))
}
Ok(())
}

pub fn end_metatext(&self) -> Result<(), Error> {
unsafe {
ffi_try!(mupdf_end_metatext(context(), self.dev));
}
Ok(())
unsafe { ffi_try!(mupdf_end_metatext(context(), self.dev)) }
}
}

Expand Down
Loading