Refactoring reading a bit
This commit is contained in:
parent
a799ff1219
commit
2192842099
2 changed files with 14 additions and 4 deletions
|
|
@ -57,9 +57,9 @@ impl<ByteBuf> Piece<ByteBuf>
|
||||||
where
|
where
|
||||||
ByteBuf: AsRef<[u8]>,
|
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
|
where
|
||||||
ByteBuf: From<Vec<u8>>,
|
ByteBuf: From<T>,
|
||||||
{
|
{
|
||||||
Piece {
|
Piece {
|
||||||
index,
|
index,
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,13 @@ impl PeerConnection {
|
||||||
"read_chunk_blocking(peer={}, chunk_info={:?}",
|
"read_chunk_blocking(peer={}, chunk_info={:?}",
|
||||||
peer_handle, &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??;
|
.await??;
|
||||||
|
|
||||||
|
|
@ -265,7 +271,11 @@ impl PeerConnection {
|
||||||
peer_handle
|
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.piece_index.get(),
|
||||||
chunk_info.offset,
|
chunk_info.offset,
|
||||||
chunk,
|
chunk,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue