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

Cleanup unused Source features #335

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 3 additions & 12 deletions logos-codegen/src/generator/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,12 @@ impl Context {
self.available.saturating_sub(self.at)
}

pub fn read_unchecked(&mut self, len: usize) -> TokenStream {
pub fn read_byte_unchecked(&mut self) -> TokenStream {
let at = self.at;

match len {
0 => {
self.advance(1);
self.advance(1);

quote!(lex.read_unchecked::<u8>(#at))
}
l => {
self.advance(l);

quote!(lex.read_unchecked::<&[u8; #l]>(#at))
}
}
quote!(lex.read_byte_unchecked(#at))
}

pub fn read(&mut self, len: usize) -> TokenStream {
Expand Down
2 changes: 1 addition & 1 deletion logos-codegen/src/generator/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'a> Generator<'a> {
let min_read = self.meta[this].min_read;

if ctx.remainder() >= max(min_read, 1) {
let read = ctx.read_unchecked(0);
let read = ctx.read_byte_unchecked();

return (quote!(byte), quote!(let byte = unsafe { #read };));
}
Expand Down
7 changes: 2 additions & 5 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ pub trait LexerInternal<'source> {
/// Read a chunk at current position, offset by `n`.
fn read_at<T: Chunk<'source>>(&self, n: usize) -> Option<T>;

/// Unchecked read a chunk at current position, offset by `n`.
unsafe fn read_unchecked<T: Chunk<'source>>(&self, n: usize) -> T;
/// Unchecked read a byte at current position, offset by `n`.
unsafe fn read_byte_unchecked(&self, n: usize) -> u8;

/// Test a chunk at current position with a closure.
fn test<T: Chunk<'source>, F: FnOnce(T) -> bool>(&self, test: F) -> bool;

/// Test a chunk at current position offset by `n` with a closure.
fn test_at<T: Chunk<'source>, F: FnOnce(T) -> bool>(&self, n: usize, test: F) -> bool;

/// Bump the position by `size`.
fn bump_unchecked(&mut self, size: usize);

Expand Down
20 changes: 2 additions & 18 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,8 @@ where
}

#[inline]
unsafe fn read_unchecked<Chunk>(&self, n: usize) -> Chunk
where
Chunk: source::Chunk<'source>,
{
self.source.read_unchecked(self.token_end + n)
unsafe fn read_byte_unchecked(&self, n: usize) -> u8 {
self.source.read_byte_unchecked(self.token_end + n)
}

/// Test a chunk at current position with a closure.
Expand All @@ -322,19 +319,6 @@ where
}
}

/// Test a chunk at current position offset by `n` with a closure.
#[inline]
fn test_at<T, F>(&self, n: usize, test: F) -> bool
where
T: source::Chunk<'source>,
F: FnOnce(T) -> bool,
{
match self.source.read::<T>(self.token_end + n) {
Some(chunk) => test(chunk),
None => false,
}
}

/// Bump the position `Lexer` is reading from by `size`.
#[inline]
fn bump_unchecked(&mut self, size: usize) {
Expand Down
23 changes: 6 additions & 17 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ pub trait Source {
where
Chunk: self::Chunk<'a>;

/// Read a chunk of bytes into an array without doing bounds checks.
/// Read a byte without doing bounds checks.
///
/// # Safety
///
/// Offset should not exceed bounds.
unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>;
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8;

/// Get a slice of the source at given range. This is analogous to
/// `slice::get(range)`.
Expand Down Expand Up @@ -119,10 +117,7 @@ impl Source for str {
}

#[inline]
unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>,
{
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
Chunk::from_ptr(self.as_ptr().add(offset))
}

Expand Down Expand Up @@ -179,10 +174,7 @@ impl Source for [u8] {
}

#[inline]
unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>,
{
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
Chunk::from_ptr(self.as_ptr().add(offset))
}

Expand Down Expand Up @@ -232,11 +224,8 @@ where
self.deref().read(offset)
}

unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>,
{
self.deref().read_unchecked(offset)
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
self.deref().read_byte_unchecked(offset)
}

fn slice(&self, range: Range<usize>) -> Option<Self::Slice<'_>> {
Expand Down
7 changes: 2 additions & 5 deletions tests/tests/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ impl<'s, S: ?Sized + Source> Source for RefSource<'s, S> {
self.0.read(offset)
}

unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: logos::source::Chunk<'a>,
{
self.0.read_unchecked(offset)
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
self.0.read_byte_unchecked(offset)
}

fn slice(&self, range: Range<usize>) -> Option<Self::Slice<'_>> {
Expand Down
Loading