Skip to content

Reuse AsRef and AsMut as possible to reduce unsafe #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/inline_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ impl ops::Index<ops::RangeFull> 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()
}
}

Expand Down Expand Up @@ -209,9 +208,7 @@ impl ops::IndexMut<ops::RangeFrom<usize>> for InlineString {
impl ops::IndexMut<ops::RangeFull> 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()
}
}

Expand All @@ -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()
}
}

Expand Down