Skip to content

Commit

Permalink
Merge pull request #332 from kmicklas/gat-slice
Browse files Browse the repository at this point in the history
Use lifetime GAT for Source::Slice
  • Loading branch information
jeertmans authored Aug 17, 2023
2 parents 82776c7 + eeb983b commit ea31a43
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion logos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords = ["lexer", "lexical", "tokenizer", "parser", "no_std"]
categories = ["parsing", "text-processing"]
readme = "../README.md"
edition = "2021"
rust-version = "1.62.1"
rust-version = "1.65.0"

[dependencies]
logos-derive = { version = "0.13.0", path = "../logos-derive", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions logos/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ impl<'source, Token: Logos<'source>> Lexer<'source, Token> {

/// Get a string slice of the current token.
#[inline]
pub fn slice(&self) -> &'source <Token::Source as Source>::Slice {
pub fn slice(&self) -> <Token::Source as Source>::Slice<'source> {
unsafe { self.source.slice_unchecked(self.span()) }
}

/// Get a slice of remaining source, starting at the end of current token.
#[inline]
pub fn remainder(&self) -> &'source <Token::Source as Source>::Slice {
pub fn remainder(&self) -> <Token::Source as Source>::Slice<'source> {
unsafe {
self.source
.slice_unchecked(self.token_end..self.source.len())
Expand Down
12 changes: 7 additions & 5 deletions logos/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use std::ops::Range;
#[allow(clippy::len_without_is_empty)]
pub trait Source {
/// A type this `Source` can be sliced into.
type Slice: ?Sized + PartialEq + Eq + Debug;
type Slice<'a>: PartialEq + Eq + Debug
where
Self: 'a;

/// Length of the source
fn len(&self) -> usize;
Expand Down Expand Up @@ -59,7 +61,7 @@ pub trait Source {
/// let foo = "It was the year when they finally immanentized the Eschaton.";
/// assert_eq!(<str as Source>::slice(&foo, 51..59), Some("Eschaton"));
/// ```
fn slice(&self, range: Range<usize>) -> Option<&Self::Slice>;
fn slice(&self, range: Range<usize>) -> Option<Self::Slice<'_>>;

/// Get a slice of the source at given range. This is analogous to
/// `slice::get_unchecked(range)`.
Expand All @@ -77,7 +79,7 @@ pub trait Source {
/// assert_eq!(<str as Source>::slice_unchecked(&foo, 51..59), "Eschaton");
/// }
/// ```
unsafe fn slice_unchecked(&self, range: Range<usize>) -> &Self::Slice;
unsafe fn slice_unchecked(&self, range: Range<usize>) -> Self::Slice<'_>;

/// For `&str` sources attempts to find the closest `char` boundary at which source
/// can be sliced, starting from `index`.
Expand All @@ -96,7 +98,7 @@ pub trait Source {
}

impl Source for str {
type Slice = str;
type Slice<'a> = &'a str;

#[inline]
fn len(&self) -> usize {
Expand Down Expand Up @@ -157,7 +159,7 @@ impl Source for str {
}

impl Source for [u8] {
type Slice = [u8];
type Slice<'a> = &'a [u8];

#[inline]
fn len(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn assert_lex<'a, Token>(
source: &'a Token::Source,
tokens: &[(
Result<Token, Token::Error>,
&'a <Token::Source as Source>::Slice,
<Token::Source as Source>::Slice<'a>,
Range<usize>,
)],
) where
Expand Down

0 comments on commit ea31a43

Please sign in to comment.