From b4a7561ccc1ed7dab6cd2c786902dec8c4ad9be6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 25 Jul 2020 22:51:22 +0200 Subject: Suggestion.apply_to(): Read from given reader, not original file We can't read from the original file, because we want to be able to use that as the writer in `apply()`. Previously, this would result in the original file simply being truncated, as we weren't reading from the correct file. To fix this, we need to give `apply_to()` both a reader and a writer. Now, in `apply()`, we can pass it the temporary file to read, and the original to write to. Used a `Path` as an input to `apply_to()` instead of a `Read`er because I got an error that didn't really make sense to me when I did so: error[E0275]: overflow evaluating the requirement `&tokio_reactor::poll_evented::PollEvented<_>: std::io::Read` | = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`git_suggested_patch`) = note: required because of the requirements on the impl of `std::io::Read` for `&tokio_reactor::poll_evented::PollEvented>` = note: required because of the requirements on the impl of `std::io::Read` for `&tokio_reactor::poll_evented::PollEvented>>` A search for the error message turned up a few active issues: - https://github.com/rust-lang/rust/issues/57854 - https://github.com/rust-lang/rust/issues/62755 - https://github.com/rust-lang/rust/issues/39959 The above issues indicate that this could come from having a variable with an inferred type. However, for the moment the issue isn't resolved, and the error message doesn't indicate where the problem is actually coming from. As I was unable to track down the source of the problem, I decided not to bother using a `Read` argument. --- src/lib.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 396c2ee..682464f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub use crate::url::SuggestionUrl; use std::fs; use std::fs::{File, OpenOptions}; use std::io::{BufRead, BufReader, Write}; +use std::path::Path; use git2::Repository; use github_rs::client::{Executor, Github}; @@ -132,14 +133,15 @@ impl Suggestion { .truncate(true) .open(repo_root.join(&self.path)).unwrap(); - self.apply_to(&mut original) + self.apply_to(new.path(), &mut original) } - fn apply_to(&self, writer: &mut W) ->Result<(), Error> { - let repo = Repository::open(".").unwrap(); - let repo_root = repo.workdir().unwrap(); - - let original = File::open(repo_root.join(&self.path)).unwrap(); + fn apply_to, W: Write>( + &self, + from: P, + writer: &mut W, + ) -> Result<(), Error> { + let original = File::open(from).unwrap(); let reader = BufReader::new(original); for (i, line) in reader.lines().enumerate() { @@ -346,7 +348,7 @@ mod tests { "#; let mut actual = Cursor::new(Vec::new()); - suggestion.apply_to(&mut actual).unwrap(); + suggestion.apply_to(temp.path(), &mut actual).unwrap(); assert_eq!( std::str::from_utf8(&actual.into_inner()).unwrap(), -- cgit v1.2.3