Skip to content

Commit

Permalink
Merge pull request #340 from InfiniteCoder01/rc-arc-sources
Browse files Browse the repository at this point in the history
Add Rc<T> and Arc<T> sources
  • Loading branch information
jeertmans authored Feb 10, 2024
2 parents f6de1d7 + ad63cdd commit ba69cc3
Showing 1 changed file with 48 additions and 1 deletion.
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

0 comments on commit ba69cc3

Please sign in to comment.