Skip to content

Commit

Permalink
Remove unsafe annotations for the options API.
Browse files Browse the repository at this point in the history
These used to have safety considerations although none do anymore, but
they require validation or there could be runtime panics.

- Addresses #100
- Addresses #138
  • Loading branch information
Alexhuszagh committed Sep 15, 2024
1 parent aeab322 commit c032641
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lexical-util/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::num::Integer;
/// performance isn't the highest consideration here.
#[inline(always)]
#[cfg(feature = "write")]
pub fn copy_to_dst<Bytes: AsRef<[u8]>>(dst: &mut [u8], src: Bytes) -> usize {
pub fn copy_to_dst<T: Copy, Bytes: AsRef<[T]>>(dst: &mut [T], src: Bytes) -> usize {
let src = src.as_ref();
dst[..src.len()].clone_from_slice(src);
dst[..src.len()].copy_from_slice(src);

src.len()
}
Expand Down
10 changes: 3 additions & 7 deletions lexical-util/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ pub unsafe trait BytesIter<'a>: Iterator<Item = &'a u8> + Buffer<'a> {
/// does not set the cursor within skipped characters.
unsafe fn set_cursor(&mut self, index: usize);

/// Set the cursor to the start of the buffer.
#[inline(always)]
fn seek_start(&mut self) {
// SAFETY: 0 is alwatys <= any usize value.
unsafe { self.set_cursor(0) };
}

/// Get a slice to the full buffer, which may or may not be the same as
/// `as_slice`.
fn as_full_slice(&self) -> &'a [u8];
Expand Down Expand Up @@ -157,6 +150,9 @@ pub unsafe trait BytesIter<'a>: Iterator<Item = &'a u8> + Buffer<'a> {
/// expected one.
#[inline(always)]
fn read_if<Pred: FnOnce(&u8) -> bool>(&mut self, pred: Pred) -> Option<Self::Item> {
// NOTE: This was implemented to remove usage of unsafe throughout to code
// base, however, performance was really not up to scratch. I'm not sure
// the cause of this.
if let Some(peeked) = self.peek() {
if pred(peeked) {
// SAFETY: the slice cannot be empty because we peeked a value.
Expand Down

0 comments on commit c032641

Please sign in to comment.