chiark / gitweb /
Mass change of .to_string() to .to_owned().
authorSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 10:20:58 +0000 (10:20 +0000)
committerSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 10:24:27 +0000 (10:24 +0000)
I've just figured out that the former means 'use the Display trait'
whereas the latter means 'change ownership and lifetime'. In the case
of &str, both do what I want, but .to_owned() is a clearer
specification of how _little_ I wanted.

src/coloured_string.rs
src/text.rs

index a6fae9828179e9c52605a205f106f9eb251b1570..6efdfc67b404575360a0868190adc9dd15e6a388 100644 (file)
@@ -16,13 +16,13 @@ pub struct ColouredStringSlice<'a> {
 impl ColouredString {
     pub fn plain(text: &str) -> Self {
         ColouredString {
-            text: text.to_string(),
+            text: text.to_owned(),
             colour: " ".repeat(text.chars().count()),
         }
     }
     pub fn uniform(text: &str, colour: char) -> Self {
         ColouredString {
-            text: text.to_string(),
+            text: text.to_owned(),
             colour: colour.to_string().repeat(text.chars().count()),
         }
     }
@@ -30,8 +30,8 @@ impl ColouredString {
         assert_eq!(text.chars().count(), colour.chars().count(),
                    "Mismatched lengths in ColouredString::general");
         ColouredString {
-            text: text.to_string(),
-            colour: colour.to_string(),
+            text: text.to_owned(),
+            colour: colour.to_owned(),
         }
     }
 
@@ -76,8 +76,8 @@ impl<'a> ColouredStringSlice<'a> {
 
     pub fn to_owned(&self) -> ColouredString {
         ColouredString {
-            text: self.text.to_string(),
-            colour: self.colour.to_string(),
+            text: self.text.to_owned(),
+            colour: self.colour.to_owned(),
         }
     }
 
@@ -124,8 +124,8 @@ impl std::ops::Add<ColouredString> for &ColouredString {
     type Output = ColouredString;
     fn add(self, rhs: ColouredString) -> ColouredString {
         ColouredString {
-            text: self.text.to_string() + &rhs.text,
-            colour: self.colour.to_string() + &rhs.colour,
+            text: self.text.to_owned() + &rhs.text,
+            colour: self.colour.to_owned() + &rhs.colour,
         }
     }
 }
@@ -134,8 +134,8 @@ impl std::ops::Add<ColouredStringSlice<'_>> for ColouredString {
     type Output = ColouredString;
     fn add(self, rhs: ColouredStringSlice<'_>) -> ColouredString {
         ColouredString {
-            text: self.text.to_string() + &rhs.text,
-            colour: self.colour.to_string() + &rhs.colour,
+            text: self.text.to_owned() + &rhs.text,
+            colour: self.colour.to_owned() + &rhs.colour,
         }
     }
 }
@@ -144,7 +144,7 @@ impl std::ops::Add<ColouredString> for &str {
     type Output = ColouredString;
     fn add(self, rhs: ColouredString) -> ColouredString {
         ColouredString {
-            text: self.to_string() + &rhs.text,
+            text: self.to_owned() + &rhs.text,
             colour: (" ".repeat(self.chars().count())) + &rhs.colour,
         }
     }
index 651ffd4e957f14fcb0600f553f5be17be93ea033..e5e684ee90368c114fb3a626cc08aeba5da3d669 100644 (file)
@@ -122,7 +122,7 @@ impl TextFragment for EditorHeaderSeparator {
     fn render(&self, width: usize) -> Vec<ColouredString> {
         vec! {
             ColouredString::uniform(
-                &((&"-".repeat(width - min(2, width))).to_string() + "|"),
+                &((&"-".repeat(width - min(2, width))).to_owned() + "|"),
                 '-',
             ).truncate(width).to_owned(),
         }
@@ -149,19 +149,19 @@ pub struct UsernameHeader {
 impl UsernameHeader {
     pub fn from(account: &str, nameline: &str) -> Box<dyn TextFragment> {
     Box::new(UsernameHeader{
-            header: "From".to_string(),
+            header: "From".to_owned(),
             colour: 'F',
-            account: account.to_string(),
-            nameline: nameline.to_string(),
+            account: account.to_owned(),
+            nameline: nameline.to_owned(),
         })
     }
 
     pub fn via(account: &str, nameline: &str) -> Box<dyn TextFragment> {
         Box::new(UsernameHeader{
-                header: "Via".to_string(),
+                header: "Via".to_owned(),
                 colour: 'f',
-                account: account.to_string(),
-                nameline: nameline.to_string(),
+                account: account.to_owned(),
+                nameline: nameline.to_owned(),
             })
     }
 }
@@ -630,7 +630,7 @@ impl html::Receiver for HTMLFormatter {
             tag == "body" {
             // do nothing, except don't report this as an unknown tag
         } else {
-            self.bad_tags.insert(tag.to_string());
+            self.bad_tags.insert(tag.to_owned());
         }
     }
     fn end_tag(&mut self, tag: &str, _attrs: &HashMap<String, String>) {
@@ -1017,7 +1017,7 @@ impl Media {
             .collect();
         trim_para_list(&mut paras);
         Box::new(Media {
-                url: url.to_string(),
+                url: url.to_owned(),
                 description: paras,
             })
     }
@@ -1147,23 +1147,23 @@ fn test_filestatus_build() {
     assert_eq!(fs.message, Some("hello, world".to_owned()));
 
     let fsf = FileStatusLine::new()
-        .add("A".to_string(), "Annoy", 10)
+        .add("A".to_owned(), "Annoy", 10)
         .finalise();
     assert_eq!(fsf.priwidth, vec! {
             (10, 9),
         });
 
     let fsf = FileStatusLine::new()
-        .add("A".to_string(), "Annoy", 10)
-        .add("B".to_string(), "Badger", 10)
+        .add("A".to_owned(), "Annoy", 10)
+        .add("B".to_owned(), "Badger", 10)
         .finalise();
     assert_eq!(fsf.priwidth, vec! {
             (10, 9 + 10 + FileStatusLine::SPACING),
         });
 
     let fsf = FileStatusLine::new()
-        .add("A".to_string(), "Annoy", 10)
-        .add("B".to_string(), "Badger", 5)
+        .add("A".to_owned(), "Annoy", 10)
+        .add("B".to_owned(), "Badger", 5)
         .finalise();
     assert_eq!(fsf.priwidth, vec! {
             (10, 9),