diff --git a/src/source.rs b/src/source.rs index 4cd09e08..7ba5a16b 100644 --- a/src/source.rs +++ b/src/source.rs @@ -1,7 +1,7 @@ //! This module contains a bunch of traits necessary for processing byte strings. //! //! Most notable are: -//! * `Source` - implemented by default for `&str` and `&[u8]`, used by the `Lexer`. +//! * `Source` - implemented by default for `&str`, `&[u8]` and wrapper types, used by the `Lexer`. //! * `Slice` - slices of `Source`, returned by `Lexer::slice`. use std::fmt::Debug; @@ -209,6 +209,53 @@ impl Source for [u8] { } } +#[cfg(feature = "std")] +use std::ops::Deref; + +#[cfg(feature = "std")] +impl Source for T +where + T: Deref, + ::Target: Source, +{ + type Slice<'a> = ::Slice<'a> + where T: 'a; + + fn len(&self) -> usize { + self.deref().len() + } + + fn read<'a, Chunk>(&'a self, offset: usize) -> Option + where + Chunk: self::Chunk<'a>, + { + 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) + } + + fn slice(&self, range: Range) -> Option> { + self.deref().slice(range) + } + + unsafe fn slice_unchecked(&self, range: Range) -> Self::Slice<'_> { + self.deref().slice_unchecked(range) + } + + fn is_boundary(&self, index: usize) -> bool { + self.deref().is_boundary(index) + } + + fn find_boundary(&self, index: usize) -> usize { + self.deref().find_boundary(index) + } +} + /// A fixed, statically sized chunk of data that can be read from the `Source`. /// /// This is implemented for `u8`, as well as byte arrays `&[u8; 1]` to `&[u8; 32]`.