From 0427c9f9666222084cb4494aabbd3e7dc5cdb789 Mon Sep 17 00:00:00 2001 From: Sean Olson Date: Mon, 30 Aug 2021 18:41:27 -0700 Subject: [PATCH] feat(source): Remove bound `T: Clone` from `Source` implementation for `Cow`. (#42) This change removes the bound `T: Clone` in the implementation of `Source` for `Cow<'_, T>`. This expands the implementation of `Source` to include types like `Cow<'_, str>`. Importantly, `Cow` always requires the bound `T: ToOwned` and uses `ToOwned` to implement `Clone` rather than forwarding to `T::clone`. --- src/source_impls.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/source_impls.rs b/src/source_impls.rs index 74101fac..8e5c8f70 100644 --- a/src/source_impls.rs +++ b/src/source_impls.rs @@ -1,7 +1,11 @@ /*! Default trait implementations for [Source]. */ -use std::{borrow::Cow, sync::Arc}; +use std::{ + borrow::{Cow, ToOwned}, + fmt::Debug, + sync::Arc, +}; use crate::{MietteError, MietteSpanContents, Source, SourceSpan, SpanContents}; @@ -84,7 +88,12 @@ impl Source for Arc { } } -impl Source for Cow<'_, T> { +impl Source for Cow<'_, T> +where + // The minimal bounds are used here. `T::Owned` need not be `Source`, because `&T` can always + // be obtained from `Cow<'_, T>`. + T::Owned: Debug + Send + Sync, +{ fn read_span<'a>( &'a self, span: &SourceSpan,