Mercurial > hg
diff rust/hg-core/src/lib.rs @ 46444:6c778d20c8c2
rust: replace ToString impls with Display
ToString is automatically implementing for everything that implements
Display, and Display can avoid allocating intermediate strings.
Differential Revision: https://phab.mercurial-scm.org/D9904
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Thu, 28 Jan 2021 19:21:57 +0100 |
parents | 43d63979a75e |
children | 1f55cd5b292f |
line wrap: on
line diff
--- a/rust/hg-core/src/lib.rs Wed Jan 27 14:45:25 2021 +0100 +++ b/rust/hg-core/src/lib.rs Thu Jan 28 19:21:57 2021 +0100 @@ -39,6 +39,7 @@ PatternFileWarning, PatternSyntax, }; use std::collections::HashMap; +use std::fmt; use twox_hash::RandomXxHashBuilder64; /// This is a contract between the `micro-timer` crate and us, to expose @@ -59,14 +60,16 @@ InvalidPath(HgPathError), } -impl ToString for DirstateMapError { - fn to_string(&self) -> String { +impl fmt::Display for DirstateMapError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { DirstateMapError::PathNotFound(_) => { - "expected a value, found none".to_string() + f.write_str("expected a value, found none") } - DirstateMapError::EmptyPath => "Overflow in dirstate.".to_string(), - DirstateMapError::InvalidPath(e) => e.to_string(), + DirstateMapError::EmptyPath => { + f.write_str("Overflow in dirstate.") + } + DirstateMapError::InvalidPath(path_error) => path_error.fmt(f), } } } @@ -91,25 +94,26 @@ NonRegexPattern(IgnorePattern), } -impl ToString for PatternError { - fn to_string(&self) -> String { +impl fmt::Display for PatternError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { PatternError::UnsupportedSyntax(syntax) => { - format!("Unsupported syntax {}", syntax) + write!(f, "Unsupported syntax {}", syntax) } PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => { - format!( + write!( + f, "{}:{}: unsupported syntax {}", file_path, line, syntax ) } PatternError::TooLong(size) => { - format!("matcher pattern is too long ({} bytes)", size) + write!(f, "matcher pattern is too long ({} bytes)", size) } - PatternError::IO(e) => e.to_string(), - PatternError::Path(e) => e.to_string(), + PatternError::IO(error) => error.fmt(f), + PatternError::Path(error) => error.fmt(f), PatternError::NonRegexPattern(pattern) => { - format!("'{:?}' cannot be turned into a regex", pattern) + write!(f, "'{:?}' cannot be turned into a regex", pattern) } } }