From 2e0925627e1a65e8a7b3bc3ef676c7ae4ced175f Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 22 Apr 2022 13:23:53 -0400 Subject: [PATCH] tamer: diagnose::Label: Introduce lifetime and inner Cow I did not initially introduce lifetimes because I wasn't sure how the system was going to evolve, but now lifetimes are going to be needed in a number of contexts. The core of TAMER is able to avoid lifetimes in most instances because of its internment system, but its use is not appropriate for the diagnostic system's buffers (beyond sourcing strings from already-interned data). DEV-12151 --- tamer/src/diagnose.rs | 42 +++++++++++-------- tamer/src/diagnose/report/test/integration.rs | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/tamer/src/diagnose.rs b/tamer/src/diagnose.rs index 6f31ba11..424a8f97 100644 --- a/tamer/src/diagnose.rs +++ b/tamer/src/diagnose.rs @@ -30,7 +30,7 @@ pub use report::{Reporter, VisualReporter}; pub use resolver::*; use core::fmt; -use std::{error::Error, fmt::Display}; +use std::{borrow::Cow, error::Error, fmt::Display}; use crate::span::Span; @@ -92,23 +92,23 @@ impl Display for Level { /// /// See [`AnnotatedSpan`]. #[derive(Debug, PartialEq, Eq, Clone)] -pub struct Label(String); +pub struct Label<'a>(Cow<'a, str>); -impl Display for Label { +impl<'a> Display for Label<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Display::fmt(&self.0, f) } } -impl From for Label { +impl<'a> From for Label<'a> { fn from(s: String) -> Self { - Self(s) + Self(Cow::Owned(s)) } } -impl From<&str> for Label { - fn from(s: &str) -> Self { - String::from(s).into() +impl<'a> From<&'a str> for Label<'a> { + fn from(s: &'a str) -> Self { + Self(Cow::Borrowed(s)) } } @@ -118,17 +118,20 @@ impl From<&str> for Label { /// diagnostic message by describing important source locations that /// contribute to a given diagnostic event. #[derive(Debug, PartialEq, Eq, Clone)] -pub struct AnnotatedSpan(Span, Level, Option