Skip to content

Commit 5ccab70

Browse files
nagisaDavid Wood
authored andcommitted
Use get_or_insert_with in generate_append_for
The `expect` in there looked quite out-of-place and I was going to replace it with a `match` over `self.$name`, before realizing this was “just” `get_or_insert_with`.
1 parent 0973521 commit 5ccab70

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

thorin/src/package.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,11 @@ macro_rules! generate_append_for {
298298
return None;
299299
}
300300

301-
let id = if self.$name.is_none() {
302-
let id = self.obj.add_section(
303-
Vec::new(),
304-
Vec::from($section_name),
305-
SectionKind::Debug,
306-
);
307-
self.$name = Some(id);
308-
id
309-
} else {
310-
self.$name.expect("`generate_append_for` is broken")
311-
};
301+
let id = *self.$name.get_or_insert_with(|| self.obj.add_section(
302+
Vec::new(),
303+
Vec::from($section_name),
304+
SectionKind::Debug,
305+
));
312306

313307
// FIXME: correct alignment
314308
let offset = self.obj.append_section_data(id, data, 1);

0 commit comments

Comments
 (0)