From: Ian Jackson Date: Sun, 4 Feb 2024 14:20:45 +0000 (+0000) Subject: coloured_string: Make ColouredString common blanket impl for & X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=a9b5c77dbc26c6ff939e0a60a80a5980740171cf;p=mastodonochrome.git coloured_string: Make ColouredString common blanket impl for & This also avoids repeating the (trivial) field accessor bodies. --- diff --git a/src/coloured_string.rs b/src/coloured_string.rs index c35094a..0c4e9fa 100644 --- a/src/coloured_string.rs +++ b/src/coloured_string.rs @@ -73,6 +73,15 @@ pub trait ColouredStringCommon { } } +impl ColouredStringCommon for &'_ T { + fn text(&self) -> &str { + ::text(self) + } + fn colours(&self) -> &str { + ::colours(self) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct ColouredString { text: String, @@ -88,15 +97,6 @@ impl ColouredStringCommon for ColouredString { } } -impl<'a> ColouredStringCommon for &'a ColouredString { - fn text(&self) -> &str { - &self.text - } - fn colours(&self) -> &str { - &self.colours - } -} - // I'd have liked here to write // impl From for ColouredString { ... } // on the basis that this code can sensibly make an owned ColouredString @@ -175,15 +175,6 @@ impl<'a> ColouredStringCommon for ColouredStringSlice<'a> { } } -impl<'a> ColouredStringCommon for &ColouredStringSlice<'a> { - fn text(&self) -> &str { - self.text - } - fn colours(&self) -> &str { - self.colours - } -} - impl<'a> ColouredStringSlice<'a> { pub fn general(text: &'a str, colours: &'a str) -> Self { assert_eq!(