Skip to content

Commit 60b4067

Browse files
itsjunetimevinxv
authored andcommitted
Don't short-circuit in ffi_try (messense#115)
1 parent bc440c9 commit 60b4067

24 files changed

+354
-605
lines changed

src/bitmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub struct Bitmap {
1515

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

2222
/// Width of the region in pixels.

src/buffer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ impl FromStr for Buffer {
2020
type Err = Error;
2121
fn from_str(s: &str) -> Result<Self, Self::Err> {
2222
let c_str = CString::new(s)?;
23-
let inner = unsafe { ffi_try!(mupdf_buffer_from_str(context(), c_str.as_ptr())) };
24-
Ok(Self { inner, offset: 0 })
23+
unsafe { ffi_try!(mupdf_buffer_from_str(context(), c_str.as_ptr())) }
24+
.map(|inner| Self { inner, offset: 0 })
2525
}
2626
}
2727

@@ -39,8 +39,8 @@ impl Buffer {
3939

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

4646
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
@@ -78,7 +78,7 @@ impl Buffer {
7878
buf.as_mut_ptr(),
7979
len
8080
))
81-
};
81+
}?;
8282
self.offset += read_len as usize;
8383
Ok(read_len)
8484
}
@@ -92,7 +92,7 @@ impl Buffer {
9292
buf.as_ptr(),
9393
len
9494
))
95-
};
95+
}?;
9696
Ok(len)
9797
}
9898
}

src/colorspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Colorspace {
109109
out.as_mut_ptr(),
110110
via,
111111
params.into()
112-
));
112+
))?;
113113
out.set_len(to_n);
114114
Ok(out)
115115
}
@@ -138,7 +138,7 @@ impl Colorspace {
138138
out.as_mut_ptr(),
139139
via,
140140
params.into()
141-
));
141+
))?;
142142
Ok(n)
143143
}
144144
}

src/cookie.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ pub struct Cookie {
1313

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

2019
/// Abort rendering

src/device.rs

Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -187,34 +187,34 @@ impl Device {
187187
}
188188

189189
pub fn from_pixmap_with_clip(pixmap: &Pixmap, clip: IRect) -> Result<Self, Error> {
190-
let dev = unsafe { ffi_try!(mupdf_new_draw_device(context(), pixmap.inner, clip.into())) };
191-
Ok(Self {
192-
dev,
193-
list: ptr::null_mut(),
194-
})
190+
unsafe { ffi_try!(mupdf_new_draw_device(context(), pixmap.inner, clip.into())) }.map(
191+
|dev| Self {
192+
dev,
193+
list: ptr::null_mut(),
194+
},
195+
)
195196
}
196197

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

201202
pub fn from_display_list(list: &DisplayList) -> Result<Self, Error> {
202-
let dev = unsafe { ffi_try!(mupdf_new_display_list_device(context(), list.inner)) };
203-
Ok(Self {
203+
unsafe { ffi_try!(mupdf_new_display_list_device(context(), list.inner)) }.map(|dev| Self {
204204
dev,
205205
list: list.inner,
206206
})
207207
}
208208

209209
pub fn from_text_page(page: &TextPage, opts: TextPageOptions) -> Result<Self, Error> {
210-
let dev = unsafe {
210+
unsafe {
211211
ffi_try!(mupdf_new_stext_device(
212212
context(),
213213
page.inner,
214214
opts.bits() as _
215215
))
216-
};
217-
Ok(Self {
216+
}
217+
.map(|dev| Self {
218218
dev,
219219
list: ptr::null_mut(),
220220
})
@@ -242,9 +242,8 @@ impl Device {
242242
color.as_ptr(),
243243
alpha,
244244
cp.into()
245-
));
245+
))
246246
}
247-
Ok(())
248247
}
249248

250249
#[allow(clippy::too_many_arguments)]
@@ -269,9 +268,8 @@ impl Device {
269268
color.as_ptr(),
270269
alpha,
271270
cp.into()
272-
));
271+
))
273272
}
274-
Ok(())
275273
}
276274

277275
pub fn clip_path(&self, path: &Path, even_odd: bool, ctm: &Matrix) -> Result<(), Error> {
@@ -282,9 +280,8 @@ impl Device {
282280
path.inner,
283281
even_odd,
284282
ctm.into()
285-
));
283+
))
286284
}
287-
Ok(())
288285
}
289286

290287
pub fn clip_stroke_path(
@@ -300,9 +297,8 @@ impl Device {
300297
path.inner,
301298
stroke.inner,
302299
ctm.into()
303-
));
300+
))
304301
}
305-
Ok(())
306302
}
307303

308304
pub fn fill_text(
@@ -324,9 +320,8 @@ impl Device {
324320
color.as_ptr(),
325321
alpha,
326322
cp.into()
327-
));
323+
))
328324
}
329-
Ok(())
330325
}
331326

332327
#[allow(clippy::too_many_arguments)]
@@ -351,14 +346,12 @@ impl Device {
351346
color.as_ptr(),
352347
alpha,
353348
cp.into()
354-
));
349+
))
355350
}
356-
Ok(())
357351
}
358352

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

364357
pub fn clip_stroke_text(
@@ -374,9 +367,8 @@ impl Device {
374367
text.inner,
375368
stroke.inner,
376369
ctm.into()
377-
));
370+
))
378371
}
379-
Ok(())
380372
}
381373

382374
pub fn ignore_text(&self, text: &Text, ctm: &Matrix) -> Result<(), Error> {
@@ -386,9 +378,8 @@ impl Device {
386378
self.dev,
387379
text.inner,
388380
ctm.into()
389-
));
381+
))
390382
}
391-
Ok(())
392383
}
393384

394385
pub fn fill_shade(
@@ -406,9 +397,8 @@ impl Device {
406397
ctm.into(),
407398
alpha,
408399
cp.into()
409-
));
400+
))
410401
}
411-
Ok(())
412402
}
413403

414404
pub fn fill_image(
@@ -426,9 +416,8 @@ impl Device {
426416
ctm.into(),
427417
alpha,
428418
cp.into()
429-
));
419+
))
430420
}
431-
Ok(())
432421
}
433422

434423
pub fn fill_image_mask(
@@ -450,9 +439,8 @@ impl Device {
450439
color.as_ptr(),
451440
alpha,
452441
cp.into()
453-
));
442+
))
454443
}
455-
Ok(())
456444
}
457445

458446
pub fn clip_image_mask(&self, image: &Image, ctm: &Matrix) -> Result<(), Error> {
@@ -462,16 +450,12 @@ impl Device {
462450
self.dev,
463451
image.inner,
464452
ctm.into()
465-
));
453+
))
466454
}
467-
Ok(())
468455
}
469456

470457
pub fn pop_clip(&self) -> Result<(), Error> {
471-
unsafe {
472-
ffi_try!(mupdf_pop_clip(context(), self.dev));
473-
}
474-
Ok(())
458+
unsafe { ffi_try!(mupdf_pop_clip(context(), self.dev)) }
475459
}
476460

477461
pub fn begin_mask(
@@ -491,9 +475,8 @@ impl Device {
491475
cs.inner,
492476
bc.as_ptr(),
493477
cp.into()
494-
));
478+
))
495479
}
496-
Ok(())
497480
}
498481

499482
pub fn end_mask(&self, f: Option<&Function>) -> Result<(), Error> {
@@ -502,9 +485,8 @@ impl Device {
502485
context(),
503486
self.dev,
504487
f.map_or(ptr::null_mut(), |f| f.inner)
505-
));
488+
))
506489
}
507-
Ok(())
508490
}
509491

510492
pub fn begin_group(
@@ -526,16 +508,12 @@ impl Device {
526508
knockout,
527509
blend_mode as _,
528510
alpha
529-
));
511+
))
530512
}
531-
Ok(())
532513
}
533514

534515
pub fn end_group(&self) -> Result<(), Error> {
535-
unsafe {
536-
ffi_try!(mupdf_end_group(context(), self.dev));
537-
}
538-
Ok(())
516+
unsafe { ffi_try!(mupdf_end_group(context(), self.dev)) }
539517
}
540518

541519
pub fn begin_tile(
@@ -547,7 +525,7 @@ impl Device {
547525
ctm: &Matrix,
548526
id: Option<NonZero<i32>>,
549527
) -> Result<Option<NonZero<i32>>, Error> {
550-
let i = unsafe {
528+
unsafe {
551529
ffi_try!(mupdf_begin_tile(
552530
context(),
553531
self.dev,
@@ -558,30 +536,21 @@ impl Device {
558536
ctm.into(),
559537
id.map_or(0, NonZero::get)
560538
))
561-
};
562-
Ok(NonZero::new(i))
539+
}
540+
.map(NonZero::new)
563541
}
564542

565543
pub fn end_tile(&self) -> Result<(), Error> {
566-
unsafe {
567-
ffi_try!(mupdf_end_tile(context(), self.dev));
568-
}
569-
Ok(())
544+
unsafe { ffi_try!(mupdf_end_tile(context(), self.dev)) }
570545
}
571546

572547
pub fn begin_layer(&self, name: &str) -> Result<(), Error> {
573548
let c_name = CString::new(name)?;
574-
unsafe {
575-
ffi_try!(mupdf_begin_layer(context(), self.dev, c_name.as_ptr()));
576-
}
577-
Ok(())
549+
unsafe { ffi_try!(mupdf_begin_layer(context(), self.dev, c_name.as_ptr())) }
578550
}
579551

580552
pub fn end_layer(&self) -> Result<(), Error> {
581-
unsafe {
582-
ffi_try!(mupdf_end_layer(context(), self.dev));
583-
}
584-
Ok(())
553+
unsafe { ffi_try!(mupdf_end_layer(context(), self.dev)) }
585554
}
586555

587556
pub fn begin_structure(&self, standard: Structure, raw: &str, idx: i32) -> Result<(), Error> {
@@ -593,16 +562,12 @@ impl Device {
593562
standard as _,
594563
c_raw.as_ptr(),
595564
idx as _
596-
));
565+
))
597566
}
598-
Ok(())
599567
}
600568

601569
pub fn end_structure(&self) -> Result<(), Error> {
602-
unsafe {
603-
ffi_try!(mupdf_end_structure(context(), self.dev));
604-
}
605-
Ok(())
570+
unsafe { ffi_try!(mupdf_end_structure(context(), self.dev)) }
606571
}
607572

608573
pub fn begin_metatext(&self, meta: Metatext, text: &str) -> Result<(), Error> {
@@ -613,16 +578,12 @@ impl Device {
613578
self.dev,
614579
meta as _,
615580
c_text.as_ptr()
616-
));
581+
))
617582
}
618-
Ok(())
619583
}
620584

621585
pub fn end_metatext(&self) -> Result<(), Error> {
622-
unsafe {
623-
ffi_try!(mupdf_end_metatext(context(), self.dev));
624-
}
625-
Ok(())
586+
unsafe { ffi_try!(mupdf_end_metatext(context(), self.dev)) }
626587
}
627588
}
628589

0 commit comments

Comments
 (0)