Skip to content

Commit 20dcc50

Browse files
authored
Let-else reformatting (#23)
* Use let-else in App::post_rendering * Use let-else in diff::reloc_eq * Use let-else in diff::diff_objs * Use let-else in views::data_diff::data_diff_ui * Use let-else in views::function_diff::function_diff_ui * Use let-else in views::function_diff::asm_row_ui * Use let-else in views::jobs::jobs_ui * Update rust-version in Cargo.toml
1 parent c7b6ec8 commit 20dcc50

File tree

6 files changed

+344
-362
lines changed

6 files changed

+344
-362
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "objdiff"
33
version = "0.2.3"
44
edition = "2021"
5-
rust-version = "1.62"
5+
rust-version = "1.65"
66
authors = ["Luke Street <luke@street.dev>"]
77
license = "MIT OR Apache-2.0"
88
repository = "https://github.com/encounter/objdiff"

src/app.rs

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -398,69 +398,64 @@ impl eframe::App for App {
398398

399399
fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &eframe::Frame) {
400400
for job in &mut self.view_state.jobs {
401-
if let Some(handle) = &job.handle {
402-
if !handle.is_finished() {
403-
continue;
404-
}
405-
match job.handle.take().unwrap().join() {
406-
Ok(result) => {
407-
log::info!("Job {} finished", job.id);
408-
match result {
409-
JobResult::None => {
410-
if let Some(err) = &job.status.read().unwrap().error {
411-
log::error!("{:?}", err);
412-
}
413-
}
414-
JobResult::ObjDiff(state) => {
415-
self.view_state.build = Some(state);
416-
}
417-
JobResult::BinDiff(state) => {
418-
self.view_state.build = Some(Box::new(ObjDiffResult {
419-
first_status: BuildStatus {
420-
success: true,
421-
log: "".to_string(),
422-
},
423-
second_status: BuildStatus {
424-
success: true,
425-
log: "".to_string(),
426-
},
427-
first_obj: Some(state.first_obj),
428-
second_obj: Some(state.second_obj),
429-
time: OffsetDateTime::now_utc(),
430-
}));
431-
}
432-
JobResult::CheckUpdate(state) => {
433-
self.view_state.check_update = Some(state);
434-
}
435-
JobResult::Update(state) => {
436-
if let Ok(mut guard) = self.relaunch_path.lock() {
437-
*guard = Some(state.exe_path);
438-
}
439-
self.should_relaunch = true;
401+
let Some(handle) = &job.handle else {
402+
continue;
403+
};
404+
if !handle.is_finished() {
405+
continue;
406+
}
407+
match job.handle.take().unwrap().join() {
408+
Ok(result) => {
409+
log::info!("Job {} finished", job.id);
410+
match result {
411+
JobResult::None => {
412+
if let Some(err) = &job.status.read().unwrap().error {
413+
log::error!("{:?}", err);
440414
}
441415
}
442-
}
443-
Err(err) => {
444-
let err = if let Some(msg) = err.downcast_ref::<&'static str>() {
445-
anyhow::Error::msg(*msg)
446-
} else if let Some(msg) = err.downcast_ref::<String>() {
447-
anyhow::Error::msg(msg.clone())
448-
} else {
449-
anyhow::Error::msg("Thread panicked")
450-
};
451-
let result = job.status.write();
452-
if let Ok(mut guard) = result {
453-
guard.error = Some(err);
454-
} else {
455-
drop(result);
456-
job.status = Arc::new(RwLock::new(JobStatus {
457-
title: "Error".to_string(),
458-
progress_percent: 0.0,
459-
progress_items: None,
460-
status: "".to_string(),
461-
error: Some(err),
416+
JobResult::ObjDiff(state) => {
417+
self.view_state.build = Some(state);
418+
}
419+
JobResult::BinDiff(state) => {
420+
self.view_state.build = Some(Box::new(ObjDiffResult {
421+
first_status: BuildStatus { success: true, log: "".to_string() },
422+
second_status: BuildStatus { success: true, log: "".to_string() },
423+
first_obj: Some(state.first_obj),
424+
second_obj: Some(state.second_obj),
425+
time: OffsetDateTime::now_utc(),
462426
}));
463427
}
428+
JobResult::CheckUpdate(state) => {
429+
self.view_state.check_update = Some(state);
430+
}
431+
JobResult::Update(state) => {
432+
if let Ok(mut guard) = self.relaunch_path.lock() {
433+
*guard = Some(state.exe_path);
434+
}
435+
self.should_relaunch = true;
436+
}
437+
}
438+
}
439+
Err(err) => {
440+
let err = if let Some(msg) = err.downcast_ref::<&'static str>() {
441+
anyhow::Error::msg(*msg)
442+
} else if let Some(msg) = err.downcast_ref::<String>() {
443+
anyhow::Error::msg(msg.clone())
444+
} else {
445+
anyhow::Error::msg("Thread panicked")
446+
};
447+
let result = job.status.write();
448+
if let Ok(mut guard) = result {
449+
guard.error = Some(err);
450+
} else {
451+
drop(result);
452+
job.status = Arc::new(RwLock::new(JobStatus {
453+
title: "Error".to_string(),
454+
progress_percent: 0.0,
455+
progress_items: None,
456+
status: "".to_string(),
457+
error: Some(err),
458+
}));
464459
}
465460
}
466461
}

src/diff.rs

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,25 @@ fn address_eq(left: &ObjSymbol, right: &ObjSymbol) -> bool {
211211
}
212212

213213
fn reloc_eq(left_reloc: Option<&ObjReloc>, right_reloc: Option<&ObjReloc>) -> bool {
214-
if let (Some(left), Some(right)) = (left_reloc, right_reloc) {
215-
if left.kind != right.kind {
216-
return false;
214+
let (Some(left), Some(right)) = (left_reloc, right_reloc) else {
215+
return false;
216+
};
217+
if left.kind != right.kind {
218+
return false;
219+
}
220+
221+
let name_matches = left.target.name == right.target.name;
222+
match (&left.target_section, &right.target_section) {
223+
(Some(sl), Some(sr)) => {
224+
// Match if section and name or address match
225+
sl == sr && (name_matches || address_eq(&left.target, &right.target))
217226
}
218-
let name_matches = left.target.name == right.target.name;
219-
match (&left.target_section, &right.target_section) {
220-
(Some(sl), Some(sr)) => {
221-
// Match if section and name or address match
222-
sl == sr && (name_matches || address_eq(&left.target, &right.target))
223-
}
224-
(Some(_), None) => false,
225-
(None, Some(_)) => {
226-
// Match if possibly stripped weak symbol
227-
name_matches && right.target.flags.0.contains(ObjSymbolFlags::Weak)
228-
}
229-
(None, None) => name_matches,
227+
(Some(_), None) => false,
228+
(None, Some(_)) => {
229+
// Match if possibly stripped weak symbol
230+
name_matches && right.target.flags.0.contains(ObjSymbolFlags::Weak)
230231
}
231-
} else {
232-
false
232+
(None, None) => name_matches,
233233
}
234234
}
235235

@@ -363,48 +363,49 @@ fn find_symbol<'a>(symbols: &'a mut [ObjSymbol], name: &str) -> Option<&'a mut O
363363

364364
pub fn diff_objs(left: &mut ObjInfo, right: &mut ObjInfo, _diff_config: &DiffConfig) -> Result<()> {
365365
for left_section in &mut left.sections {
366-
if let Some(right_section) = find_section(right, &left_section.name) {
367-
if left_section.kind == ObjSectionKind::Code {
368-
for left_symbol in &mut left_section.symbols {
369-
if let Some(right_symbol) =
370-
find_symbol(&mut right_section.symbols, &left_symbol.name)
371-
{
372-
left_symbol.diff_symbol = Some(right_symbol.name.clone());
373-
right_symbol.diff_symbol = Some(left_symbol.name.clone());
374-
diff_code(
375-
left.architecture,
376-
&left_section.data,
377-
&right_section.data,
378-
left_symbol,
379-
right_symbol,
380-
&left_section.relocations,
381-
&right_section.relocations,
382-
)?;
383-
} else {
384-
no_diff_code(
385-
left.architecture,
386-
&left_section.data,
387-
left_symbol,
388-
&left_section.relocations,
389-
)?;
390-
}
366+
let Some(right_section) = find_section(right, &left_section.name) else {
367+
continue;
368+
};
369+
if left_section.kind == ObjSectionKind::Code {
370+
for left_symbol in &mut left_section.symbols {
371+
if let Some(right_symbol) =
372+
find_symbol(&mut right_section.symbols, &left_symbol.name)
373+
{
374+
left_symbol.diff_symbol = Some(right_symbol.name.clone());
375+
right_symbol.diff_symbol = Some(left_symbol.name.clone());
376+
diff_code(
377+
left.architecture,
378+
&left_section.data,
379+
&right_section.data,
380+
left_symbol,
381+
right_symbol,
382+
&left_section.relocations,
383+
&right_section.relocations,
384+
)?;
385+
} else {
386+
no_diff_code(
387+
left.architecture,
388+
&left_section.data,
389+
left_symbol,
390+
&left_section.relocations,
391+
)?;
391392
}
392-
for right_symbol in &mut right_section.symbols {
393-
if right_symbol.instructions.is_empty() {
394-
no_diff_code(
395-
left.architecture,
396-
&right_section.data,
397-
right_symbol,
398-
&right_section.relocations,
399-
)?;
400-
}
393+
}
394+
for right_symbol in &mut right_section.symbols {
395+
if right_symbol.instructions.is_empty() {
396+
no_diff_code(
397+
left.architecture,
398+
&right_section.data,
399+
right_symbol,
400+
&right_section.relocations,
401+
)?;
401402
}
402-
} else if left_section.kind == ObjSectionKind::Data {
403-
diff_data(left_section, right_section);
404-
// diff_data_symbols(left_section, right_section)?;
405-
} else if left_section.kind == ObjSectionKind::Bss {
406-
diff_bss_symbols(&mut left_section.symbols, &mut right_section.symbols)?;
407403
}
404+
} else if left_section.kind == ObjSectionKind::Data {
405+
diff_data(left_section, right_section);
406+
// diff_data_symbols(left_section, right_section)?;
407+
} else if left_section.kind == ObjSectionKind::Bss {
408+
diff_bss_symbols(&mut left_section.symbols, &mut right_section.symbols)?;
408409
}
409410
}
410411
diff_bss_symbols(&mut left.common, &mut right.common)?;

0 commit comments

Comments
 (0)