Replace AsSpannedStr with Into<SpannedStr>

This commit is contained in:
Alexandre Bury 2018-02-16 17:16:01 -08:00
parent d0a186d01e
commit d33fb39cd9
3 changed files with 11 additions and 25 deletions

View File

@ -1,5 +1,5 @@
use super::Segment; use super::Segment;
use utils::span::{AsSpannedStr, IndexedCow, Span}; use utils::span::{SpannedStr, IndexedCow, Span};
/// A list of segments representing a row of text /// A list of segments representing a row of text
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -14,13 +14,13 @@ impl Row {
/// Resolve the row indices into string slices and attributes. /// Resolve the row indices into string slices and attributes.
pub fn resolve<'a, T, S>(&self, source: S) -> Vec<Span<'a, T>> pub fn resolve<'a, T, S>(&self, source: S) -> Vec<Span<'a, T>>
where where
S: 'a + AsSpannedStr<'a, T>, S: Into<SpannedStr<'a, T>>
{ {
let source = source.as_spanned_str(); let source = source.into();
self.segments self.segments
.iter() .iter()
.map(|seg| seg.resolve(source.clone())) .map(|seg| seg.resolve(&source))
.collect() .collect()
} }

View File

@ -17,7 +17,7 @@ pub struct Segment {
impl Segment { impl Segment {
/// Resolve this segment to a string slice and an attribute. /// Resolve this segment to a string slice and an attribute.
pub fn resolve<'a, T>(&self, source: SpannedStr<'a, T>) -> Span<'a, T> { pub fn resolve<'a, T>(&self, source: &SpannedStr<'a, T>) -> Span<'a, T> {
let span = &source.spans_raw()[self.span_id]; let span = &source.spans_raw()[self.span_id];
let content = span.content.resolve(source.source()); let content = span.content.resolve(source.source());
@ -37,9 +37,8 @@ impl Segment {
let span = &source.spans()[self.span_id]; let span = &source.spans()[self.span_id];
let content = span.as_ref().resolve(source.source()); let content = span.as_ref().resolve(source.source());
let content = &content[self.start..self.end];
content &content[self.start..self.end]
} }
/// Returns indices in the source string, if possible. /// Returns indices in the source string, if possible.

View File

@ -23,12 +23,6 @@ where
spans: &'a [IndexedSpan<T>], spans: &'a [IndexedSpan<T>],
} }
/// Represents a type that can be converted to a `SpannedStr`.
pub trait AsSpannedStr<'a, T> {
/// Returns a view of `self`
fn as_spanned_str(&self) -> SpannedStr<'a, T>;
}
/// Describes an object that appears like a `SpannedStr`. /// Describes an object that appears like a `SpannedStr`.
pub trait SpannedText { pub trait SpannedText {
/// Type of span returned by `SpannedText::spans()`. /// Type of span returned by `SpannedText::spans()`.
@ -41,6 +35,7 @@ pub trait SpannedText {
fn spans(&self) -> &[Self::S]; fn spans(&self) -> &[Self::S];
/// Returns a `SpannedText` by reference. /// Returns a `SpannedText` by reference.
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
fn as_ref<'a>(&'a self) -> SpannedTextRef<'a, Self> { fn as_ref<'a>(&'a self) -> SpannedTextRef<'a, Self> {
SpannedTextRef { r: self } SpannedTextRef { r: self }
} }
@ -116,8 +111,7 @@ where
} }
/// Gives access to the parsed styled spans. /// Gives access to the parsed styled spans.
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] pub fn spans(&self) -> Vec<Span<'a, T>> {
pub fn spans<'b>(&self) -> Vec<Span<'a, T>> {
self.spans self.spans
.iter() .iter()
.map(|span| span.resolve(self.source)) .map(|span| span.resolve(self.source))
@ -260,19 +254,12 @@ impl<T> SpannedString<T> {
} }
} }
impl<'a, T> AsSpannedStr<'a, T> for &'a SpannedString<T> { impl <'a, T> From<&'a SpannedString<T>> for SpannedStr<'a, T> {
fn as_spanned_str(&self) -> SpannedStr<'a, T> { fn from(other: &'a SpannedString<T>) -> Self {
SpannedStr::new(&self.source, &self.spans) SpannedStr::new(&other.source, &other.spans)
} }
} }
impl<'a, T> AsSpannedStr<'a, T> for SpannedStr<'a, T> {
fn as_spanned_str(&self) -> SpannedStr<'a, T> {
SpannedStr::new(&self.source, &self.spans)
}
}
/// An indexed span with an associated attribute. /// An indexed span with an associated attribute.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexedSpan<T> { pub struct IndexedSpan<T> {