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`.
This commit is contained in:
Sean Olson 2021-08-30 18:41:27 -07:00 committed by GitHub
parent 84219f6c80
commit 0427c9f966
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -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<T: Source> Source for Arc<T> {
}
}
impl<T: Source + Clone> Source for Cow<'_, T> {
impl<T: ?Sized + Source + ToOwned> 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,