Skip to content

Commit

Permalink
♻️ use &[T]::split_at_checked
Browse files Browse the repository at this point in the history
`&[T]::split_at_checked` was stabilized on rust 1.80
  • Loading branch information
ChanTsune committed Oct 16, 2024
1 parent 653fa0b commit 59ae520
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/src/archive/read/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::borrow::Cow;
use std::io;

fn read_header_from_slice(bytes: &[u8]) -> io::Result<&[u8]> {
// TODO: use split_at_checked instead
let (header, body) = bytes.split_at(PNA_HEADER.len());
let (header, body) = bytes
.split_at_checked(PNA_HEADER.len())
.ok_or(io::ErrorKind::UnexpectedEof)?;
if header != PNA_HEADER {
return Err(io::Error::new(io::ErrorKind::InvalidData, "It's not PNA"));
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/chunk/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ pub(crate) fn read_chunk_from_slice(bytes: &[u8]) -> io::Result<(RawChunk<&[u8]>
crc_hasher.update(&ty[..]);

// read chunk data
// TODO: use split_at_checked instead
let (data, r) = r.split_at(length as usize);
let (data, r) = r
.split_at_checked(length as usize)
.ok_or(io::ErrorKind::UnexpectedEof)?;
crc_hasher.update(data);

// read crc sum
Expand Down
5 changes: 3 additions & 2 deletions lib/src/entry/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ impl ExtendedAttribute {
.split_first_chunk::<{ mem::size_of::<u32>() }>()
.ok_or(io::ErrorKind::UnexpectedEof)?;
let len = u32::from_be_bytes(*len) as usize;
// TODO: use split_at_checked instead
let (name, value) = value.split_at(len);
let (name, value) = value
.split_at_checked(len)
.ok_or(io::ErrorKind::UnexpectedEof)?;
let name = String::from_utf8(name.to_vec()).map_err(|_| io::ErrorKind::InvalidData)?;

let (len, value) = value
Expand Down

0 comments on commit 59ae520

Please sign in to comment.