Ban all unwrap usage in codebase

This commit is contained in:
topjohnwu
2025-12-08 23:31:04 -08:00
committed by John Wu
parent 200665c48a
commit ff4ca74cfe
24 changed files with 138 additions and 89 deletions
+32 -24
View File
@@ -484,10 +484,9 @@ impl Cpio {
};
for (name, entry) in &self.entries {
let p = "/".to_string() + name.as_str();
if !p.starts_with(&path) {
let Some(p) = p.strip_prefix(&path) else {
continue;
}
let p = p.strip_prefix(&path).unwrap();
};
if !p.is_empty() && !p.starts_with('/') {
continue;
}
@@ -614,8 +613,11 @@ impl Cpio {
o.rm(".backup", true);
self.rm(".backup", true);
let mut lhs = o.entries.into_iter().peekable();
let mut rhs = self.entries.iter().peekable();
let mut left_iter = o.entries.into_iter();
let mut right_iter = self.entries.iter();
let mut lhs = left_iter.next();
let mut rhs = right_iter.next();
loop {
enum Action<'a> {
@@ -623,32 +625,38 @@ impl Cpio {
Record(&'a String),
Noop,
}
let action = match (lhs.peek(), rhs.peek()) {
(Some((l, _)), Some((r, re))) => match l.as_str().cmp(r.as_str()) {
// Move the iterator forward if needed
if lhs.is_none() {
lhs = left_iter.next();
}
if rhs.is_none() {
rhs = right_iter.next();
}
let action = match (lhs.take(), rhs.take()) {
(Some((ln, le)), Some((rn, re))) => match ln.as_str().cmp(rn.as_str()) {
Ordering::Less => {
let (l, le) = lhs.next().unwrap();
Action::Backup(l, le)
// Put rhs back
rhs = Some((rn, re));
Action::Backup(ln, le)
}
Ordering::Greater => {
// Put lhs back
lhs = Some((ln, le));
Action::Record(rn)
}
Ordering::Greater => Action::Record(rhs.next().unwrap().0),
Ordering::Equal => {
let (l, le) = lhs.next().unwrap();
let action = if re.data != le.data {
Action::Backup(l, le)
if re.data != le.data {
Action::Backup(ln, le)
} else {
Action::Noop
};
rhs.next();
action
}
}
},
(Some(_), None) => {
let (l, le) = lhs.next().unwrap();
Action::Backup(l, le)
}
(None, Some(_)) => Action::Record(rhs.next().unwrap().0),
(None, None) => {
break;
}
(Some((ln, le)), None) => Action::Backup(ln, le),
(None, Some((rn, _))) => Action::Record(rn),
(None, None) => break,
};
match action {
Action::Backup(name, mut entry) => {