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

Add Rc<T> and Arc<T> sources #340

Merged
merged 2 commits into from
Feb 10, 2024
Merged
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
49 changes: 48 additions & 1 deletion src/source.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -209,6 +209,53 @@ impl Source for [u8] {
}
}

#[cfg(feature = "std")]
use std::ops::Deref;

#[cfg(feature = "std")]
impl<T> Source for T
where
T: Deref,
<T as Deref>::Target: Source,
{
type Slice<'a> = <T::Target as Source>::Slice<'a>
where T: 'a;

fn len(&self) -> usize {
self.deref().len()
}

fn read<'a, Chunk>(&'a self, offset: usize) -> Option<Chunk>
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<usize>) -> Option<Self::Slice<'_>> {
self.deref().slice(range)
}

unsafe fn slice_unchecked(&self, range: Range<usize>) -> 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]`.
Expand Down
Loading