aboutsummaryrefslogtreecommitdiffstats
path: root/src/cell.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-14 03:49:13 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-14 04:05:54 -0500
commit2083eae2c5b480ccad672fb2fbb1b2bb5774a606 (patch)
tree9db649b8f73a35d23839f1e189d7e1599306193a /src/cell.rs
parent160942c40cbeb7503147c0895217644161fcc84b (diff)
downloadvt100-rust-2083eae2c5b480ccad672fb2fbb1b2bb5774a606.tar.gz
vt100-rust-2083eae2c5b480ccad672fb2fbb1b2bb5774a606.zip
replace all uses of unwrap(), expect(), and indexing with unreachable!()
and also document why they are unreachable
Diffstat (limited to 'src/cell.rs')
-rw-r--r--src/cell.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 1b44461..5f09ffb 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -19,7 +19,9 @@ impl PartialEq<Self> for Cell {
return false;
}
let len = self.len();
- self.contents[..len] == other.contents[..len]
+ // self.len() always returns a valid value
+ self.contents.get(..len).unwrap_or_else(|| unreachable!())
+ == other.contents.get(..len).unwrap_or_else(|| unreachable!())
}
}
@@ -40,15 +42,19 @@ impl Cell {
}
pub(crate) fn append(&mut self, c: char) {
- if self.len() >= CODEPOINTS_IN_CELL {
+ let len = self.len();
+ if len >= CODEPOINTS_IN_CELL {
return;
}
- if self.len() == 0 {
- self.contents[self.len()] = ' ';
+ if len == 0 {
+ // 0 is always less than 6
+ *self.contents.get_mut(0).unwrap_or_else(|| unreachable!()) = ' ';
self.len += 1;
}
- self.contents[self.len()] = c;
+ let len = self.len();
+ // we already checked that len < CODEPOINTS_IN_CELL
+ *self.contents.get_mut(len).unwrap_or_else(|| unreachable!()) = c;
self.len += 1;
}