Refactoring reading a bit

This commit is contained in:
Igor Katson 2021-06-28 15:44:29 +01:00
parent a799ff1219
commit 2192842099
2 changed files with 14 additions and 4 deletions

View file

@ -57,9 +57,9 @@ impl<ByteBuf> Piece<ByteBuf>
where
ByteBuf: AsRef<[u8]>,
{
pub fn from_vec(index: u32, begin: u32, block: Vec<u8>) -> Piece<ByteBuf>
pub fn from_data<T>(index: u32, begin: u32, block: T) -> Piece<ByteBuf>
where
ByteBuf: From<Vec<u8>>,
ByteBuf: From<T>,
{
Piece {
index,

View file

@ -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,