diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index 8c4a9a5..f60f05b 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -1183,7 +1183,7 @@ impl GraphicalReportHandler { &'a self, source: &'a dyn SourceCode, context_span: &'a SourceSpan, - ) -> Result<(Box + 'a>, Vec), fmt::Error> { + ) -> Result<(Box, Vec), fmt::Error> { let context_data = source .read_span(context_span, self.context_lines, self.context_lines) .map_err(|_| fmt::Error)?; diff --git a/src/handlers/narratable.rs b/src/handlers/narratable.rs index f8d36ae..76a2b81 100644 --- a/src/handlers/narratable.rs +++ b/src/handlers/narratable.rs @@ -165,7 +165,7 @@ impl NarratableReportHandler { .map(|label| { source.read_span(label.inner(), self.context_lines, self.context_lines) }) - .collect::>>, MietteError>>() + .collect::>, MietteError>>() .map_err(|_| fmt::Error)?; let mut contexts = Vec::new(); for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) { @@ -286,7 +286,7 @@ impl NarratableReportHandler { &'a self, source: &'a dyn SourceCode, context_span: &'a SourceSpan, - ) -> Result<(Box + 'a>, Vec), fmt::Error> { + ) -> Result<(Box, Vec), fmt::Error> { let context_data = source .read_span(context_span, self.context_lines, self.context_lines) .map_err(|_| fmt::Error)?; diff --git a/src/highlighters/blank.rs b/src/highlighters/blank.rs index 50a9c65..8c79a76 100644 --- a/src/highlighters/blank.rs +++ b/src/highlighters/blank.rs @@ -12,7 +12,7 @@ pub struct BlankHighlighter; impl Highlighter for BlankHighlighter { fn start_highlighter_state<'h>( &'h self, - _source: &dyn SpanContents<'_>, + _source: &(dyn SpanContents + 'h), ) -> Box { Box::new(BlankHighlighterState) } diff --git a/src/highlighters/mod.rs b/src/highlighters/mod.rs index 0af1aa2..202bcad 100644 --- a/src/highlighters/mod.rs +++ b/src/highlighters/mod.rs @@ -39,7 +39,7 @@ pub trait Highlighter { /// responsible for the actual rendering. fn start_highlighter_state<'h>( &'h self, - source: &dyn SpanContents<'_>, + source: &(dyn SpanContents + 'h), ) -> Box; } diff --git a/src/highlighters/syntect.rs b/src/highlighters/syntect.rs index 538124c..bd4402f 100644 --- a/src/highlighters/syntect.rs +++ b/src/highlighters/syntect.rs @@ -42,7 +42,7 @@ impl Default for SyntectHighlighter { impl Highlighter for SyntectHighlighter { fn start_highlighter_state<'h>( &'h self, - source: &dyn SpanContents<'_>, + source: &(dyn SpanContents + 'h), ) -> Box { if let Some(syntax) = self.detect_syntax(source) { let highlighter = syntect::Highlighter::new(&self.theme); @@ -82,7 +82,7 @@ impl SyntectHighlighter { } /// Determine syntect [`SyntaxReference`] to use for given [`SpanContents`]. - fn detect_syntax(&self, contents: &dyn SpanContents<'_>) -> Option<&syntect::SyntaxReference> { + fn detect_syntax(&self, contents: &dyn SpanContents) -> Option<&syntect::SyntaxReference> { // use language if given if let Some(language) = contents.language() { return self.syntax_set.find_syntax_by_name(language); diff --git a/src/named_source.rs b/src/named_source.rs index ea11cd2..0633fb7 100644 --- a/src/named_source.rs +++ b/src/named_source.rs @@ -1,4 +1,4 @@ -use crate::{MietteError, MietteSpanContents, SourceCode, SpanContents}; +use crate::{MietteError, SourceCode, SpanContents}; /// Utility struct for when you have a regular [`SourceCode`] type that doesn't /// implement `name`. For example [`String`]. Or if you want to override the @@ -51,6 +51,43 @@ impl NamedSource { self } } +/// Utility struct used by [`NamedSource`] to attach a file name to an inner [`SpanContents`] value +#[derive(Debug)] +pub struct NamedSpanContents { + inner: Box, + name: Box, + language: Option>, +} +impl SpanContents for NamedSpanContents { + #[inline] + fn data(&self) -> &[u8] { + self.inner.data() + } + #[inline] + fn span(&self) -> &crate::SourceSpan { + self.inner.span() + } + #[inline] + fn line(&self) -> usize { + self.inner.line() + } + #[inline] + fn column(&self) -> usize { + self.inner.column() + } + #[inline] + fn line_count(&self) -> usize { + self.inner.line_count() + } + #[inline] + fn name(&self) -> Option<&str> { + Some(&self.name) + } + #[inline] + fn language(&self) -> Option<&str> { + self.language.as_deref() + } +} impl SourceCode for NamedSource { fn read_span<'a>( @@ -58,21 +95,14 @@ impl SourceCode for NamedSource { span: &crate::SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { let inner_contents = self.inner() .read_span(span, context_lines_before, context_lines_after)?; - let mut contents = MietteSpanContents::new_named( - self.name.clone(), - inner_contents.data(), - *inner_contents.span(), - inner_contents.line(), - inner_contents.column(), - inner_contents.line_count(), - ); - if let Some(language) = &self.language { - contents = contents.with_language(language); - } - Ok(Box::new(contents)) + Ok(Box::new(NamedSpanContents { + inner: inner_contents, + name: self.name.clone().into_boxed_str(), + language: self.language.as_ref().map(|v| v.clone().into_boxed_str()), + })) } } diff --git a/src/protocol.rs b/src/protocol.rs index 7f09d4d..ee13750 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -252,7 +252,7 @@ pub trait SourceCode: Send + Sync { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError>; + ) -> Result, MietteError>; } /// A labeled [`SourceSpan`]. @@ -434,9 +434,9 @@ Contents of a [`SourceCode`] covered by [`SourceSpan`]. Includes line and column information to optimize highlight calculations. */ -pub trait SpanContents<'a> { +pub trait SpanContents { /// Reference to the data inside the associated span, in bytes. - fn data(&self) -> &'a [u8]; + fn data(&self) -> &[u8]; /// [`SourceSpan`] representing the span covered by this `SpanContents`. fn span(&self) -> &SourceSpan; /// An optional (file?) name for the container of this `SpanContents`. @@ -530,8 +530,8 @@ impl<'a> MietteSpanContents<'a> { } } -impl<'a> SpanContents<'a> for MietteSpanContents<'a> { - fn data(&self) -> &'a [u8] { +impl<'a> SpanContents for MietteSpanContents<'a> { + fn data(&self) -> &[u8] { self.data } fn span(&self) -> &SourceSpan { diff --git a/src/source_impls.rs b/src/source_impls.rs index e362b4a..46de5e5 100644 --- a/src/source_impls.rs +++ b/src/source_impls.rs @@ -98,7 +98,7 @@ impl SourceCode for [u8] { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { let contents = context_info(self, span, context_lines_before, context_lines_after)?; Ok(Box::new(contents)) } @@ -110,7 +110,7 @@ impl<'src> SourceCode for &'src [u8] { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { <[u8] as SourceCode>::read_span(self, span, context_lines_before, context_lines_after) } } @@ -121,7 +121,7 @@ impl SourceCode for Vec { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { <[u8] as SourceCode>::read_span(self, span, context_lines_before, context_lines_after) } } @@ -132,7 +132,7 @@ impl SourceCode for str { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { <[u8] as SourceCode>::read_span( self.as_bytes(), span, @@ -149,7 +149,7 @@ impl<'s> SourceCode for &'s str { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { ::read_span(self, span, context_lines_before, context_lines_after) } } @@ -160,7 +160,7 @@ impl SourceCode for String { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { ::read_span(self, span, context_lines_before, context_lines_after) } } @@ -171,7 +171,7 @@ impl SourceCode for Arc { span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { self.as_ref() .read_span(span, context_lines_before, context_lines_after) } @@ -191,7 +191,7 @@ where span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, - ) -> Result + 'a>, MietteError> { + ) -> Result, MietteError> { self.as_ref() .read_span(span, context_lines_before, context_lines_after) }