mirror of
https://github.com/Noratrieb/jsonformat.git
synced 2026-01-14 14:15:03 +01:00
On-stack dynamic dispatch to avoid boxing
This commit is contained in:
parent
8832d617a3
commit
9652a46435
1 changed files with 22 additions and 6 deletions
28
src/main.rs
28
src/main.rs
|
|
@ -16,9 +16,17 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let reader: Box<dyn Read> = match matches.value_of("input") {
|
// Note: on-stack dynamic dispatch
|
||||||
Some(path) => Box::new(File::open(path)?),
|
let (mut file, mut stdin);
|
||||||
None => Box::new(std::io::stdin()),
|
let reader: &mut dyn Read = match matches.value_of("input") {
|
||||||
|
Some(path) => {
|
||||||
|
file = File::open(path)?;
|
||||||
|
&mut file
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
stdin = std::io::stdin();
|
||||||
|
&mut stdin
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let replaced_indent = matches.value_of("indentation").map(|value| {
|
let replaced_indent = matches.value_of("indentation").map(|value| {
|
||||||
|
|
@ -50,9 +58,17 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
output = windows_output_default_file.as_deref().or(output);
|
output = windows_output_default_file.as_deref().or(output);
|
||||||
|
|
||||||
let writer: Box<dyn Write> = match output {
|
// Note: on-stack dynamic dispatch
|
||||||
Some(file) => Box::new(File::create(file)?),
|
let (mut file, mut stdout);
|
||||||
None => Box::new(std::io::stdout()),
|
let writer: &mut dyn Write = match output {
|
||||||
|
Some(filename) => {
|
||||||
|
file = File::create(filename)?;
|
||||||
|
&mut file
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
stdout = std::io::stdout();
|
||||||
|
&mut stdout
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut reader = BufReader::new(reader);
|
let mut reader = BufReader::new(reader);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue