Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use static dispatch instead of dynamic dispatch #4

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use std::ffi::OsString;
use std::io::Write;

/// run parses the given itr arguments and triggers the primary program logic.
pub fn run<I, T>(itr: I, output: &mut (dyn Write)) -> Result<()>
pub fn run<I, T, W>(itr: I, output: &mut W) -> Result<()>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
W: Write,
{
exec(Args::parse_from(itr), output)
}
Expand All @@ -22,9 +23,8 @@ fn run_success() {
.split_whitespace();

let mut output_cursor = Cursor::new(vec![]);
let output_writer: &mut (dyn Write) = &mut output_cursor;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! I don't have to use this ugly conversion any more 🎉


run(itr, output_writer).expect("to run correctly");
run(itr, &mut output_cursor).expect("to run correctly");

let buf = output_cursor.into_inner();
let output = match std::str::from_utf8(&buf) {
Expand All @@ -36,7 +36,7 @@ fn run_success() {
}

/// exec makes a HTTP request for the configured URL and constructs a Header for display.
fn exec(args: Args, output: &mut (dyn Write)) -> Result<()> {
fn exec<W: Write>(args: Args, output: &mut W) -> Result<()> {
args.color.init();

let resp = reqwest::blocking::get(&args.url)
Expand Down
8 changes: 4 additions & 4 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use reqwest::StatusCode;
use std::collections::BTreeMap;
use std::io::{BufWriter, Write};

pub struct Headers<'a, 'b> {
pub struct Headers<'a, 'b, W: Write> {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly I didn't HAVE to assign a trait bound here.

I'm presuming the compiler was able to figure that out from the below impl block which does use a trait bound (at the compiler's request).

That said, I decided to be explicit about the trait bound here.

filters: Option<String>,
map: &'a HeaderMap,
output: &'b mut (dyn Write),
output: &'b mut W,
}

impl<'a, 'b> Headers<'a, 'b> {
pub fn new(map: &'a HeaderMap, filters: Option<String>, output: &'b mut (dyn Write)) -> Self {
impl<'a, 'b, W: Write> Headers<'a, 'b, W> {
pub fn new(map: &'a HeaderMap, filters: Option<String>, output: &'b mut W) -> Self {
Self {
filters,
map,
Expand Down