-
Notifications
You must be signed in to change notification settings - Fork 1k
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
enable sharing the binary file and use a larger buffer #2786
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request.
I am in favor of the move to Vec<u8>
. I suggest we don't write to a file but continue to write to stdout.
examples/file-sharing.rs
Outdated
@@ -196,6 +208,8 @@ enum CliArgument { | |||
name: String, | |||
}, | |||
Get { | |||
#[clap(long)] | |||
path: PathBuf, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of providing a file path for the retrieved file to be written to, how about leveraging unix pipelines?
E.g. one would do:
cargo run -- get [...] > my_file
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Thanks for your advices. I've changed the output to stdout().
examples/file-sharing.rs
Outdated
let mut reader = BufReader::new(File::open(&path)?); | ||
let mut vec = Vec::<u8>::new(); | ||
let mut buffer = reader.fill_buf()?; | ||
let mut len = buffer.len(); | ||
while len > 0 { | ||
vec.extend_from_slice(buffer); | ||
reader.consume(len); | ||
buffer = reader.fill_buf()?; | ||
len = buffer.len(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this is reading the entire file into vec
, what is the difference to the previous version, i.e. std::fs::read_to_string
? If you want to read an entire file as u8
, why not use the below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. fixed.
examples/file-sharing.rs
Outdated
@@ -134,8 +135,7 @@ async fn main() -> Result<(), Box<dyn Error>> { | |||
// Reply with the content of the file on incoming requests. | |||
Some(network::Event::InboundRequest { request, channel }) => { | |||
if request == name { | |||
let file_content = std::fs::read_to_string(&path)?; | |||
network_client.respond_file(file_content, channel).await; | |||
network_client.respond_file(&std::fs::read(&path)?, channel).await; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
network_client.respond_file(&std::fs::read(&path)?, channel).await; | |
network_client.respond_file(std::fs::read(&path)?, channel).await; |
Why borrow here when we clone again later on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but expected &Vec<u8>
here. any better choice?
Co-authored-by: Max Inden <mail@max-inden.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
One small suggestion.
examples/file-sharing.rs
Outdated
pub async fn respond_file(&mut self, file: &Vec<u8>, channel: ResponseChannel<FileResponse>) { | ||
self.sender | ||
.send(Command::RespondFile { file, channel }) | ||
.send(Command::RespondFile { file: file.to_vec(), channel }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think the reference on line 354 is unnecessary if we are going to create a copy anyway and it will remove the diff on line 356.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, done. Thanks~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thanks for the follow ups.
Thanks~ |
Description
Update an examle for trasnfering a binary file
Links to any relevant issues
Open Questions
Change checklist
I have added tests that prove my fix is effective or that my feature worksA changelog entry has been made in the appropriate crates