From d33fb39cd91f4fdfe4b14a4352b61ca3138576d3 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 16 Feb 2018 17:16:01 -0800 Subject: [PATCH] Replace AsSpannedStr with Into --- src/utils/lines/spans/row.rs | 8 ++++---- src/utils/lines/spans/segment.rs | 5 ++--- src/utils/span.rs | 23 +++++------------------ 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/utils/lines/spans/row.rs b/src/utils/lines/spans/row.rs index 817fd59..79a8757 100644 --- a/src/utils/lines/spans/row.rs +++ b/src/utils/lines/spans/row.rs @@ -1,5 +1,5 @@ use super::Segment; -use utils::span::{AsSpannedStr, IndexedCow, Span}; +use utils::span::{SpannedStr, IndexedCow, Span}; /// A list of segments representing a row of text #[derive(Debug, Clone, PartialEq, Eq)] @@ -14,13 +14,13 @@ impl Row { /// Resolve the row indices into string slices and attributes. pub fn resolve<'a, T, S>(&self, source: S) -> Vec> where - S: 'a + AsSpannedStr<'a, T>, + S: Into> { - let source = source.as_spanned_str(); + let source = source.into(); self.segments .iter() - .map(|seg| seg.resolve(source.clone())) + .map(|seg| seg.resolve(&source)) .collect() } diff --git a/src/utils/lines/spans/segment.rs b/src/utils/lines/spans/segment.rs index 0a9d9cf..2e5048a 100644 --- a/src/utils/lines/spans/segment.rs +++ b/src/utils/lines/spans/segment.rs @@ -17,7 +17,7 @@ pub struct Segment { impl Segment { /// 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 content = span.content.resolve(source.source()); @@ -37,9 +37,8 @@ impl Segment { let span = &source.spans()[self.span_id]; 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. diff --git a/src/utils/span.rs b/src/utils/span.rs index ddd14d8..81d2418 100644 --- a/src/utils/span.rs +++ b/src/utils/span.rs @@ -23,12 +23,6 @@ where spans: &'a [IndexedSpan], } -/// 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`. pub trait SpannedText { /// Type of span returned by `SpannedText::spans()`. @@ -41,6 +35,7 @@ pub trait SpannedText { fn spans(&self) -> &[Self::S]; /// Returns a `SpannedText` by reference. + #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] fn as_ref<'a>(&'a self) -> SpannedTextRef<'a, Self> { SpannedTextRef { r: self } } @@ -116,8 +111,7 @@ where } /// Gives access to the parsed styled spans. - #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] - pub fn spans<'b>(&self) -> Vec> { + pub fn spans(&self) -> Vec> { self.spans .iter() .map(|span| span.resolve(self.source)) @@ -260,19 +254,12 @@ impl SpannedString { } } -impl<'a, T> AsSpannedStr<'a, T> for &'a SpannedString { - fn as_spanned_str(&self) -> SpannedStr<'a, T> { - SpannedStr::new(&self.source, &self.spans) +impl <'a, T> From<&'a SpannedString> for SpannedStr<'a, T> { + fn from(other: &'a SpannedString) -> Self { + 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. #[derive(Debug, Clone, PartialEq, Eq)] pub struct IndexedSpan {