Mercurial > public > mercurial-scm > hg
diff rust/hg-core/src/config/layer.rs @ 46499:eace48b4a786
rust: Use the DisplayBytes trait in config printing
This is similar to `std::fmt::Display`, but for arbitrary bytes instead
of Unicode. Writing to an abstract output stream helps avoid allocating
intermediate `Vec<u8>` buffers.
Differential Revision: https://phab.mercurial-scm.org/D9966
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 08 Feb 2021 11:13:56 +0100 |
parents | d7685105e504 |
children | 2e5dd18d6dc3 |
line wrap: on
line diff
--- a/rust/hg-core/src/config/layer.rs Thu Feb 04 13:32:11 2021 -0800 +++ b/rust/hg-core/src/config/layer.rs Mon Feb 08 11:13:56 2021 +0100 @@ -9,7 +9,7 @@ use crate::errors::{HgError, IoResultExt}; use crate::utils::files::{get_bytes_from_path, get_path_from_bytes}; -use format_bytes::format_bytes; +use format_bytes::{write_bytes, DisplayBytes}; use lazy_static::lazy_static; use regex::bytes::Regex; use std::collections::HashMap; @@ -165,8 +165,11 @@ } } -impl std::fmt::Debug for ConfigLayer { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl DisplayBytes for ConfigLayer { + fn display_bytes( + &self, + out: &mut dyn std::io::Write, + ) -> std::io::Result<()> { let mut sections: Vec<_> = self.sections.iter().collect(); sections.sort_by(|e0, e1| e0.0.cmp(e1.0)); @@ -175,16 +178,13 @@ items.sort_by(|e0, e1| e0.0.cmp(e1.0)); for (item, config_entry) in items { - writeln!( - f, - "{}", - String::from_utf8_lossy(&format_bytes!( - b"{}.{}={} # {}", - section, - item, - &config_entry.bytes, - &self.origin.to_bytes(), - )) + write_bytes!( + out, + b"{}.{}={} # {}\n", + section, + item, + &config_entry.bytes, + &self.origin, )? } } @@ -224,13 +224,15 @@ * Others? */ } -impl ConfigOrigin { - /// TODO use some kind of dedicated trait? - pub fn to_bytes(&self) -> Vec<u8> { +impl DisplayBytes for ConfigOrigin { + fn display_bytes( + &self, + out: &mut dyn std::io::Write, + ) -> std::io::Result<()> { match self { - ConfigOrigin::File(p) => get_bytes_from_path(p), - ConfigOrigin::CommandLine => b"--config".to_vec(), - ConfigOrigin::Environment(e) => format_bytes!(b"${}", e), + ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)), + ConfigOrigin::CommandLine => out.write_all(b"--config"), + ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e), } } }