Skip to content

Commit

Permalink
feat(source): Remove bound T: Clone from Source implementation fo…
Browse files Browse the repository at this point in the history
…r `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`.
  • Loading branch information
olson-sean-k committed Aug 31, 2021
1 parent 84219f6 commit 0427c9f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/source_impls.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0427c9f

Please sign in to comment.