From 2192842099e7346458846589b0e09f9327d8c69d Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 28 Jun 2021 15:44:29 +0100 Subject: [PATCH] Refactoring reading a bit --- crates/librqbit/src/peer_binary_protocol.rs | 4 ++-- crates/librqbit/src/peer_connection.rs | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/librqbit/src/peer_binary_protocol.rs b/crates/librqbit/src/peer_binary_protocol.rs index 9d29fa7..872322f 100644 --- a/crates/librqbit/src/peer_binary_protocol.rs +++ b/crates/librqbit/src/peer_binary_protocol.rs @@ -57,9 +57,9 @@ impl Piece where ByteBuf: AsRef<[u8]>, { - pub fn from_vec(index: u32, begin: u32, block: Vec) -> Piece + pub fn from_data(index: u32, begin: u32, block: T) -> Piece where - ByteBuf: From>, + ByteBuf: From, { Piece { index, diff --git a/crates/librqbit/src/peer_connection.rs b/crates/librqbit/src/peer_connection.rs index 055405f..0430d57 100644 --- a/crates/librqbit/src/peer_connection.rs +++ b/crates/librqbit/src/peer_connection.rs @@ -249,7 +249,13 @@ impl PeerConnection { "read_chunk_blocking(peer={}, chunk_info={:?}", peer_handle, &chunk_info ), - move || state.file_ops().read_chunk(peer_handle, chunk_info), + move || { + let mut buf = Vec::new(); + state + .file_ops() + .read_chunk(peer_handle, chunk_info, &mut buf)?; + Ok(buf) + }, ) .await??; @@ -265,7 +271,11 @@ impl PeerConnection { peer_handle ) })?; - let message = Message::Piece(Piece::from_vec( + + // TODO: this is not super efficient as it does copying multiple times. + // Theoretically, this could be done in the sending code, so that it reads straight into + // the send buffer. + let message = Message::Piece(Piece::from_data( chunk_info.piece_index.get(), chunk_info.offset, chunk,