Skip to content

Commit

Permalink
refactor: Missed some places on the last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Nov 26, 2024
1 parent ee3cfb3 commit 4b9ead7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
39 changes: 9 additions & 30 deletions src/types/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,7 @@ impl FromStr for JanetBuffer<'_> {
}
}

impl Index<i32> for JanetBuffer<'_> {
impl Index<usize> for JanetBuffer<'_> {
type Output = u8;

/// Get a reference to the byte of the buffer at the `index`.
Expand All @@ -2806,26 +2806,17 @@ impl Index<i32> for JanetBuffer<'_> {
///
/// [`bytes`]: #method.bytes.html
#[inline]
fn index(&self, index: i32) -> &Self::Output {
if index < 0 {
fn index(&self, index: usize) -> &Self::Output {
self.as_bytes().get(index).unwrap_or_else(|| {
crate::jpanic!(
"index out of bounds: the len is {} but the index is {}",
"index out of bounds: the len is {} but the index is {index}",
self.len(),
index
)
}

self.as_bytes().get(index as usize).unwrap_or_else(|| {
crate::jpanic!(
"index out of bounds: the len is {} but the index is {}",
self.len(),
index
)
})
}
}

impl IndexMut<i32> for JanetBuffer<'_> {
impl IndexMut<usize> for JanetBuffer<'_> {
/// Get a exclusive reference to the byte of the string at the `index`.
///
/// It is more idiomatic to use [`bytes_mut`] method.
Expand All @@ -2835,24 +2826,12 @@ impl IndexMut<i32> for JanetBuffer<'_> {
///
/// [`bytes_mut`]: #method.bytes_mut.html
#[inline]
fn index_mut(&mut self, index: i32) -> &mut Self::Output {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let len = self.len();
if index < 0 {
crate::jpanic!(
"index out of bounds: the index ({}) is negative and must be positive",
index
)
}

self.as_bytes_mut()
.get_mut(index as usize)
.unwrap_or_else(|| {
crate::jpanic!(
"index out of bounds: the len is {} but the index is {}",
len,
index
)
})
self.as_bytes_mut().get_mut(index).unwrap_or_else(|| {
crate::jpanic!("index out of bounds: the len is {len} but the index is {index}",)
})
}
}

Expand Down
13 changes: 3 additions & 10 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,7 @@ impl FromStr for JanetString<'_> {
}
}

impl Index<i32> for JanetString<'_> {
impl Index<usize> for JanetString<'_> {
type Output = u8;

/// Get a reference to the byte of the string at the `index`.
Expand All @@ -2434,15 +2434,8 @@ impl Index<i32> for JanetString<'_> {
///
/// [`bytes`]: #method.bytes.html
#[inline]
fn index(&self, index: i32) -> &Self::Output {
if index < 0 {
crate::jpanic!(
"index out of bounds: the index ({}) is negative and must be positive",
index
)
}

self.as_bytes().get(index as usize).unwrap_or_else(|| {
fn index(&self, index: usize) -> &Self::Output {
self.as_bytes().get(index).unwrap_or_else(|| {
crate::jpanic!(
"index out of bounds: the len is {} but the index is {}",
self.len(),
Expand Down

0 comments on commit 4b9ead7

Please sign in to comment.