Skip to content

Commit 80e7574

Browse files
committed
shorten get_field_types() when there are no fields
1 parent 88b15ae commit 80e7574

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

capnp-rpc/src/rpc_twoparty_capnp.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,7 @@ pub mod recipient_id {
598598
::capnp::word(0, 0, 0, 0, 1, 0, 1, 0),
599599
];
600600
pub fn get_field_types(index: u16) -> ::capnp::introspect::Type {
601-
match index {
602-
_ => panic!("invalid field index {}", index),
603-
}
601+
panic!("invalid field index {}", index)
604602
}
605603
pub fn get_annotation_types(child_index: Option<u16>, index: u32) -> ::capnp::introspect::Type {
606604
panic!("invalid annotation indices ({:?}, {}) ", child_index, index)
@@ -763,9 +761,7 @@ pub mod third_party_cap_id {
763761
::capnp::word(0, 0, 0, 0, 1, 0, 1, 0),
764762
];
765763
pub fn get_field_types(index: u16) -> ::capnp::introspect::Type {
766-
match index {
767-
_ => panic!("invalid field index {}", index),
768-
}
764+
panic!("invalid field index {}", index)
769765
}
770766
pub fn get_annotation_types(child_index: Option<u16>, index: u32) -> ::capnp::introspect::Type {
771767
panic!("invalid annotation indices ({:?}, {}) ", child_index, index)

capnpc/src/codegen.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1524,20 +1524,25 @@ fn generate_get_field_types(
15241524
}
15251525
}
15261526
}
1527-
branches.push(Line(
1528-
"_ => panic!(\"invalid field index {}\", index),".into(),
1529-
));
1527+
let body = if branches.is_empty() {
1528+
Line("panic!(\"invalid field index {}\", index)".into())
1529+
} else {
1530+
branches.push(Line(
1531+
"_ => panic!(\"invalid field index {}\", index),".into(),
1532+
));
1533+
Branch(vec![
1534+
Line("match index {".into()),
1535+
Indent(Box::new(Branch(branches))),
1536+
Line("}".into()),
1537+
])
1538+
};
15301539
if !node_reader.get_is_generic() {
15311540
Ok(Branch(vec![
15321541
Line(fmt!(
15331542
ctx,
15341543
"pub fn get_field_types(index: u16) -> {capnp}::introspect::Type {{"
15351544
)),
1536-
Indent(Box::new(Branch(vec![
1537-
Line("match index {".into()),
1538-
Indent(Box::new(Branch(branches))),
1539-
Line("}".into()),
1540-
]))),
1545+
Indent(Box::new(body)),
15411546
Line("}".into()),
15421547
]))
15431548
} else {
@@ -1549,11 +1554,7 @@ fn generate_get_field_types(
15491554
params.params,
15501555
params.where_clause
15511556
)),
1552-
Indent(Box::new(Branch(vec![
1553-
Line("match index {".into()),
1554-
Indent(Box::new(Branch(branches))),
1555-
Line("}".into()),
1556-
]))),
1557+
Indent(Box::new(body)),
15571558
Line("}".into()),
15581559
]))
15591560
}

0 commit comments

Comments
 (0)