Remove attribute type from SpannedText

This commit is contained in:
Alexandre Bury 2018-02-16 15:34:16 -08:00
parent 7218b6c879
commit c220cc679a

View File

@ -3,7 +3,6 @@
//! This module defines various structs describing a span of text from a //! This module defines various structs describing a span of text from a
//! larger string. //! larger string.
use std::borrow::Cow; use std::borrow::Cow;
use std::marker::PhantomData;
/// A string with associated spans. /// A string with associated spans.
/// ///
@ -25,32 +24,33 @@ where
} }
/// Describes an object that appears like a `SpannedStr`. /// Describes an object that appears like a `SpannedStr`.
pub trait SpannedText<T> { pub trait SpannedText {
/// Type of span returned by `SpannedText::spans()`.
type S: AsRef<IndexedCow>;
/// Returns the source text. /// Returns the source text.
fn source(&self) -> &str; fn source(&self) -> &str;
/// Returns the spans for this text. /// Returns the spans for this text.
fn spans(&self) -> &[IndexedSpan<T>]; fn spans(&self) -> &[Self::S];
/// Returns a `SpannedText` by reference. /// Returns a `SpannedText` by reference.
fn as_ref<'a>(&'a self) -> SpannedTextRef<'a, T, Self> { fn as_ref<'a>(&'a self) -> SpannedTextRef<'a, Self> {
SpannedTextRef { SpannedTextRef { r: self }
r: self,
_phantom: PhantomData,
}
} }
} }
/// A reference to another `SpannedText`. /// A reference to another `SpannedText`.
pub struct SpannedTextRef<'a, T, C> pub struct SpannedTextRef<'a, C>
where where
C: 'a + SpannedText<T> + ?Sized, C: 'a + SpannedText + ?Sized,
{ {
r: &'a C, r: &'a C,
_phantom: PhantomData<T>,
} }
impl<'a, T> SpannedText<T> for &'a SpannedString<T> { impl<'a, T> SpannedText for &'a SpannedString<T> {
type S = IndexedSpan<T>;
fn source(&self) -> &str { fn source(&self) -> &str {
&self.source &self.source
} }
@ -60,23 +60,27 @@ impl<'a, T> SpannedText<T> for &'a SpannedString<T> {
} }
} }
impl<'a, T, C> SpannedText<T> for SpannedTextRef<'a, T, C> impl<'a, C> SpannedText for SpannedTextRef<'a, C>
where where
C: 'a + SpannedText<T> + ?Sized, C: 'a + SpannedText + ?Sized,
{ {
type S = C::S;
fn source(&self) -> &str { fn source(&self) -> &str {
self.r.source() self.r.source()
} }
fn spans(&self) -> &[IndexedSpan<T>] { fn spans(&self) -> &[C::S] {
self.r.spans() self.r.spans()
} }
} }
impl<'a, T> SpannedText<T> for SpannedStr<'a, T> impl<'a, T> SpannedText for SpannedStr<'a, T>
where where
T: 'a, T: 'a,
{ {
type S = IndexedSpan<T>;
fn source(&self) -> &str { fn source(&self) -> &str {
self.source self.source
} }
@ -265,6 +269,12 @@ pub struct IndexedSpan<T> {
pub attr: T, pub attr: T,
} }
impl<T> AsRef<IndexedCow> for IndexedSpan<T> {
fn as_ref(&self) -> &IndexedCow {
&self.content
}
}
/// A resolved span borrowing its source string. /// A resolved span borrowing its source string.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Span<'a, T: 'a> { pub struct Span<'a, T: 'a> {