From 62225cf91f91bfeea22a3f0c23b120486e5fb803 Mon Sep 17 00:00:00 2001 From: YOSHIOKA Takuma Date: Sat, 15 Feb 2020 04:51:37 +0900 Subject: [PATCH] Reuse `AsRef` and `AsMut` as possible Some trait implementations share the same codes which contain `unsafe` block, but they can be shared to reduce duplicates. --- src/inline_string.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/inline_string.rs b/src/inline_string.rs index 7d4cf8d..0df23c5 100644 --- a/src/inline_string.rs +++ b/src/inline_string.rs @@ -177,8 +177,7 @@ impl ops::Index for InlineString { #[inline] fn index(&self, _index: ops::RangeFull) -> &str { - self.assert_sanity(); - unsafe { str::from_utf8_unchecked(&self.bytes[..self.len()]) } + self.as_ref() } } @@ -209,9 +208,7 @@ impl ops::IndexMut> for InlineString { impl ops::IndexMut for InlineString { #[inline] fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str { - self.assert_sanity(); - let length = self.len(); - unsafe { str::from_utf8_unchecked_mut(&mut self.bytes[..length]) } + self.as_mut() } } @@ -220,17 +217,14 @@ impl ops::Deref for InlineString { #[inline] fn deref(&self) -> &str { - self.assert_sanity(); - unsafe { str::from_utf8_unchecked(&self.bytes[..self.len()]) } + self.as_ref() } } impl ops::DerefMut for InlineString { #[inline] fn deref_mut(&mut self) -> &mut str { - self.assert_sanity(); - let length = self.len(); - unsafe { str::from_utf8_unchecked_mut(&mut self.bytes[..length]) } + self.as_mut() } }