From f40ea26aef725ae45abb1287fa733d57eb24d547 Mon Sep 17 00:00:00 2001 From: The-Minecraft-Scientist Date: Thu, 21 Nov 2024 22:45:04 -0800 Subject: [PATCH] Remove lifetime parameter from SpanContents --- src/handlers/narratable.rs | 4 +-- src/named_source.rs | 58 +++++++++++++++++++++++++++++--------- src/protocol.rs | 10 +++---- src/source_impls.rs | 16 +++++------ 4 files changed, 59 insertions(+), 29 deletions(-) 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/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) }