diff options
author | Josh Holmer <joshua.holmer@nextgearcapital.com> | 2017-06-28 16:06:25 +0300 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2017-07-21 15:04:52 +0300 |
commit | d28c399997fabee2c6821c83ae23b7e0f87b2d5f (patch) | |
tree | b8c520cd0f62e57a74ebce13eb34ec12ae04b5a4 | |
parent | 3cc8e9ecc9b9524087aad2e8579896e010c3eac9 (diff) |
Document why unwraps won't fail
-rw-r--r-- | src/lib.rs | 12 |
1 files changed, 9 insertions, 3 deletions
@@ -587,7 +587,8 @@ impl Iterator for IntoIter { self.pop(); continue; } - match self.stack_list.last_mut().unwrap().next() { + // Unwrap is safe here because we've verified above that `self.stack_list` is not empty + match self.stack_list.last_mut().expect("This is a bug in walkdir").next() { None => self.pop(), Some(Err(err)) => return Some(Err(err)), Some(Ok(dent)) => { @@ -731,7 +732,9 @@ impl IntoIter { fn get_deferred_dir(&mut self) -> Option<DirEntry> { if self.opts.contents_first { if self.depth < self.deferred_dirs.len() { - let deferred : DirEntry = self.deferred_dirs.pop().unwrap(); + // Unwrap is safe here because we've guaranteed that + // `self.deferred_dirs.len()` can never be less than 1 + let deferred : DirEntry = self.deferred_dirs.pop().expect("This is a bug in walkdir"); if !self.skippable() { return Some(deferred); } @@ -742,8 +745,11 @@ impl IntoIter { fn push(&mut self, dent: &DirEntry) { // Make room for another open file descriptor if we've hit the max. - if self.stack_list.len() - self.oldest_opened == self.opts.max_open { + if self.stack_list.len().checked_sub(self.oldest_opened).unwrap() == self.opts.max_open { self.stack_list[self.oldest_opened].close(); + // Unwrap is safe here because self.oldest_opened is guaranteed to never be + // greater than `self.stack_list.len()`, which implies that the subtraction won't + // underflow and that adding 1 will never overflow. self.oldest_opened = self.oldest_opened.checked_add(1).unwrap(); } // Open a handle to reading the directory's entries. |