Skip to content

Commit eff444c

Browse files
committed
transpile: simplify dynamic argument parsing
Instead of `it.next().unwrap().parse().unwrap()` chains use slice patterns and a single `expect` call.
1 parent b7dd946 commit eff444c

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

c2rust-transpile/src/translator/builtins.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -646,16 +646,9 @@ impl<'c> Translation<'c> {
646646
) -> TranslationResult<WithStmts<Box<Expr>>> {
647647
let args = self.convert_exprs(ctx.used(), args)?;
648648
args.and_then(|args| {
649-
let mut args = args.into_iter();
650-
let a = args
651-
.next()
652-
.ok_or("Missing first argument to convert_overflow_arith")?;
653-
let b = args
654-
.next()
655-
.ok_or("Missing second argument to convert_overflow_arith")?;
656-
let c = args
657-
.next()
658-
.ok_or("Missing third argument to convert_overflow_arith")?;
649+
let [a, b, c]: [_; 3] = args
650+
.try_into()
651+
.map_err(|_| "`convert_overflow_arith` must have exactly 3 arguments")?;
659652
let overflowing = mk().method_call_expr(a, method_name, vec![b]);
660653
let sum_name = self.renamer.borrow_mut().fresh();
661654
let over_name = self.renamer.borrow_mut().fresh();
@@ -691,16 +684,9 @@ impl<'c> Translation<'c> {
691684
let mem = mk().path_expr(vec!["libc", name]);
692685
let args = self.convert_exprs(ctx.used(), args)?;
693686
args.and_then(|args| {
694-
let mut args = args.into_iter();
695-
let dst = args
696-
.next()
697-
.ok_or("Missing dst argument to convert_libc_fns")?;
698-
let c = args
699-
.next()
700-
.ok_or("Missing c argument to convert_libc_fns")?;
701-
let len = args
702-
.next()
703-
.ok_or("Missing len argument to convert_libc_fns")?;
687+
let [dst, c, len]: [_; 3] = args
688+
.try_into()
689+
.map_err(|_| "`convert_libc_fns` must have exactly 3 arguments: [dst, c, len]")?;
704690
let size_t = mk().path_ty(vec!["libc", "size_t"]);
705691
let len1 = mk().cast_expr(len, size_t);
706692
let mem_expr = mk().call_expr(mem, vec![dst, c, len1]);

c2rust-transpile/src/translator/simd.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,9 @@ impl<'c> Translation<'c> {
428428
let param_translation =
429429
self.convert_exprs(ctx.used(), &[first_expr_id, second_expr_id, mask_expr_id])?;
430430
param_translation.and_then(|params| {
431-
let mut params = params.into_iter();
432-
let first = params
433-
.next()
434-
.ok_or("Missing first param in convert_shuffle_vector")?;
435-
let second = params
436-
.next()
437-
.ok_or("Missing second param in convert_shuffle_vector")?;
438-
let third = params
439-
.next()
440-
.ok_or("Missing third param in convert_shuffle_vector")?;
431+
let [first, second, third]: [_; 3] = params
432+
.try_into()
433+
.map_err(|_| "`convert_shuffle_vector` must have exactly 3 parameters")?;
441434
let mut new_params = vec![first];
442435

443436
// Some don't take a second param, but the expr is still there for some reason

0 commit comments

Comments
 (0)