From: Simon Tatham Date: Tue, 26 Dec 2023 10:20:58 +0000 (+0000) Subject: Mass change of .to_string() to .to_owned(). X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=c5d74a639c84e22b7df4414bbda5d5e9ed4d211a;p=mastodonochrome.git Mass change of .to_string() to .to_owned(). 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. --- diff --git a/src/coloured_string.rs b/src/coloured_string.rs index a6fae98..6efdfc6 100644 --- a/src/coloured_string.rs +++ b/src/coloured_string.rs @@ -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 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> 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 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, } } diff --git a/src/text.rs b/src/text.rs index 651ffd4..e5e684e 100644 --- a/src/text.rs +++ b/src/text.rs @@ -122,7 +122,7 @@ impl TextFragment for EditorHeaderSeparator { fn render(&self, width: usize) -> Vec { 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 { 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 { 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) { @@ -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),