diff options
| author | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2020-08-03 00:00:00 +0000 |
|---|---|---|
| committer | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2020-08-04 00:01:48 +0200 |
| commit | 427634b5037ba1c00b72b70b561ff20767ea97e2 (patch) | |
| tree | 2f302519d3796314812f9605df8f1b32c2b0fa81 | |
| parent | 829d69b9c6bfc535a92fc290ec9391a0d5af6c81 (diff) | |
| download | rust-427634b5037ba1c00b72b70b561ff20767ea97e2.tar.gz rust-427634b5037ba1c00b72b70b561ff20767ea97e2.zip | |
Avoid `unwrap_or_else` in str indexing
This provides a small reduction of generated LLVM IR, and leads to a simpler assembly code.
| -rw-r--r-- | library/core/src/str/mod.rs | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 9d7e38d0e18..eac4741cd26 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -1923,7 +1923,10 @@ mod traits { #[inline] fn index(self, slice: &str) -> &Self::Output { let (start, end) = (self.start, self.end); - self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end)) + match self.get(slice) { + Some(s) => s, + None => super::slice_error_fail(slice, start, end), + } } #[inline] fn index_mut(self, slice: &mut str) -> &mut Self::Output { @@ -1995,7 +1998,10 @@ mod traits { #[inline] fn index(self, slice: &str) -> &Self::Output { let end = self.end; - self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end)) + match self.get(slice) { + Some(s) => s, + None => super::slice_error_fail(slice, 0, end), + } } #[inline] fn index_mut(self, slice: &mut str) -> &mut Self::Output { @@ -2068,7 +2074,10 @@ mod traits { #[inline] fn index(self, slice: &str) -> &Self::Output { let (start, end) = (self.start, slice.len()); - self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end)) + match self.get(slice) { + Some(s) => s, + None => super::slice_error_fail(slice, start, end), + } } #[inline] fn index_mut(self, slice: &mut str) -> &mut Self::Output { |
