Trait logos::Source

source ·
pub trait Source {
    type Slice;

    fn len(&self) -> usize;
    unsafe fn read(&self, offset: usize) -> u8;
    unsafe fn slice(&self, range: Range<usize>) -> Self::Slice;
}
Expand description

Trait for types the Lexer can read from.

Most notably this is implemented for &str. It is unlikely you will ever want to use this Trait yourself, unless implementing a new Source the Lexer can use.

Required Associated Types

Slice of this Source, for most types this will be &str with appropriate lifetime.

Required Methods

Length of the source

Read a single byte from source.

Implementors of this method must guarantee it to return 0 when offset is set to length of the Source (one byte after last)!

use logos::Source;

let foo = "foo";

unsafe {
    assert_eq!(foo.read(0), b'f');
    assert_eq!(foo.read(1), b'o');
    assert_eq!(foo.read(2), b'o');
    assert_eq!(foo.read(3), 0);
}

Get a slice of the source at given range. This is analogous for slice::get_unchecked(range).

use logos::Source;

let foo = "It was the year when they finally immanentized the Eschaton.";

unsafe {
    assert_eq!(foo.slice(51..59), "Eschaton");
}

Implementations on Foreign Types

Implementors