about summary refs log tree commit diff
path: root/library/core/src/slice
diff options
context:
space:
mode:
authorThe 8472 <git@infinite-source.de>2021-12-26 03:21:54 +0100
committerThe 8472 <git@infinite-source.de>2023-03-27 14:11:49 +0200
commit69db91b8b25de51633ac9f089cd7fb10a58c2b2a (patch)
tree2bb072195a1a0744f33624b96a1343d12a2452c2 /library/core/src/slice
parent7a0600714ab1a4cb2d1a88cd0660b9f9a2c07309 (diff)
Change advance(_back)_by to return `usize` instead of `Result<(), usize>`
A successful advance is now signalled by returning `0` and other values now represent the remaining number
of steps that couldn't be advanced as opposed to the amount of steps that have been advanced during a partial advance_by.

This simplifies adapters a bit, replacing some `match`/`if` with arithmetic. Whether this is beneficial overall depends
on whether `advance_by` is mostly used as a building-block for other iterator methods and adapters or whether
we also see uses by users where `Result` might be more useful.
Diffstat (limited to 'library/core/src/slice')
-rw-r--r--library/core/src/slice/iter/macros.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/library/core/src/slice/iter/macros.rs b/library/core/src/slice/iter/macros.rs
index a800da546b4..d3e9b9c2b22 100644
--- a/library/core/src/slice/iter/macros.rs
+++ b/library/core/src/slice/iter/macros.rs
@@ -176,11 +176,11 @@ macro_rules! iterator {
             }
 
             #[inline]
-            fn advance_by(&mut self, n: usize) -> Result<(), usize> {
+            fn advance_by(&mut self, n: usize) -> usize {
                 let advance = cmp::min(len!(self), n);
                 // SAFETY: By construction, `advance` does not exceed `self.len()`.
                 unsafe { self.post_inc_start(advance) };
-                if advance == n { Ok(()) } else { Err(advance) }
+                n - advance
             }
 
             #[inline]
@@ -371,11 +371,11 @@ macro_rules! iterator {
             }
 
             #[inline]
-            fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
+            fn advance_back_by(&mut self, n: usize) -> usize {
                 let advance = cmp::min(len!(self), n);
                 // SAFETY: By construction, `advance` does not exceed `self.len()`.
                 unsafe { self.pre_dec_end(advance) };
-                if advance == n { Ok(()) } else { Err(advance) }
+                n - advance
             }
         }