From 2ff8c5e2b4d9cbd3f07b97bb24e0cabfa7e88190 Mon Sep 17 00:00:00 2001 From: InfiniteCoder Date: Sat, 10 Feb 2024 14:31:29 +0300 Subject: [PATCH 1/2] Wrapper sources. Solution by @marcospb19 --- src/source.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/source.rs b/src/source.rs index 4cd09e08..bd01abe0 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]`. From ad63cddf0f75b38219eb52e63eef57385fa38b51 Mon Sep 17 00:00:00 2001 From: InfiniteCoder Date: Sat, 10 Feb 2024 14:45:03 +0300 Subject: [PATCH 2/2] Formatted --- src/source.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/source.rs b/src/source.rs index bd01abe0..7ba5a16b 100644 --- a/src/source.rs +++ b/src/source.rs @@ -209,7 +209,6 @@ impl Source for [u8] { } } - #[cfg(feature = "std")] use std::ops::Deref; @@ -235,7 +234,8 @@ where unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk where - Chunk: self::Chunk<'a> { + Chunk: self::Chunk<'a>, + { self.deref().read_unchecked(offset) }