chiark / gitweb /
coloured_string: Make ColouredString common blanket impl for & wip.ref
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 4 Feb 2024 14:20:45 +0000 (14:20 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 4 Feb 2024 14:28:36 +0000 (14:28 +0000)
This also avoids repeating the (trivial) field accessor bodies.

src/coloured_string.rs

index c35094ae349f67727a629a5c11a94a9afc9d98f4..0c4e9fa01aa9817d1d5f5ff4f0f6747f6d1759ec 100644 (file)
@@ -73,6 +73,15 @@ pub trait ColouredStringCommon {
     }
 }
 
+impl<T: ColouredStringCommon> ColouredStringCommon for &'_ T {
+    fn text(&self) -> &str {
+        <T as ColouredStringCommon>::text(self)
+    }
+    fn colours(&self) -> &str {
+        <T as ColouredStringCommon>::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<T: ColouredStringCommon> From<T> 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!(