Skip to content
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

Replace uses of unsafe with safe alternatives #1714

Merged
merged 7 commits into from
May 5, 2024
Merged
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
44 changes: 23 additions & 21 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,11 @@
P: Fn(Self::Item) -> bool,
{
match self.find(predicate) {
// find() returns a byte index that is already in the slice at a char boundary
Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) },
// The position i is returned from str::find() which means it is within the bounds of the string
Some(i) => {
let (str1, str2) = self.split_at(i);
Ok((str2, str1))

Check warning on line 425 in src/traits.rs

View check run for this annotation

Codecov / codecov/patch

src/traits.rs#L423-L425

Added lines #L423 - L425 were not covered by tests
}
None => Err(Err::Incomplete(Needed::new(1))),
}
}
Expand All @@ -436,8 +439,11 @@
{
match self.find(predicate) {
Some(0) => Err(Err::Error(E::from_error_kind(self, e))),
// find() returns a byte index that is already in the slice at a char boundary
Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) },
// The position i is returned from str::find() which means it is within the bounds of the string
Some(i) => {
let (str1, str2) = self.split_at(i);
Ok((str2, str1))
}
None => Err(Err::Incomplete(Needed::new(1))),
}
}
Expand All @@ -451,15 +457,12 @@
P: Fn(Self::Item) -> bool,
{
match self.find(predicate) {
// find() returns a byte index that is already in the slice at a char boundary
Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) },
// the end of slice is a char boundary
None => unsafe {
Ok((
self.get_unchecked(self.len()..),
self.get_unchecked(..self.len()),
))
},
// The position i is returned from str::find() which means it is within the bounds of the string
Some(i) => {
let (str1, str2) = self.split_at(i);
Ok((str2, str1))
}
None => Ok(self.split_at(0)),
}
}

Expand All @@ -474,19 +477,18 @@
{
match self.find(predicate) {
Some(0) => Err(Err::Error(E::from_error_kind(self, e))),
// find() returns a byte index that is already in the slice at a char boundary
Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) },
// The position i is returned from str::find() which means it is within the bounds of the string
Some(i) => {
let (str1, str2) = self.split_at(i);
Ok((str2, str1))
}
None => {
if self.is_empty() {
Err(Err::Error(E::from_error_kind(self, e)))
} else {
// the end of slice is a char boundary
unsafe {
Ok((
self.get_unchecked(self.len()..),
self.get_unchecked(..self.len()),
))
}
let (str1, str2) = self.split_at(self.len());
Ok((str2, str1))
}
}
}
Expand Down
Loading