use peer::message::{PieceIndex, BlockOffset, BlockLength};
pub struct Block {
data: Vec<u8>,
active: bool,
piece_index: PieceIndex,
block_offset: BlockOffset,
block_len: BlockLength
}
impl Block {
pub fn with_capacity(capacity: BlockLength) -> Block {
let mut data_storage = Vec::with_capacity(capacity as usize);
unsafe{ data_storage.set_len(capacity as usize); }
Block{ data: data_storage, active: false,
piece_index: -1, block_offset: -1, block_len: -1 }
}
pub fn is_active(&self) -> bool {
self.active
}
pub fn set_active(&mut self, index: PieceIndex, offset: BlockOffset, length: BlockLength) {
self.piece_index = index;
self.block_offset = offset;
self.block_len = length;
self.active = true;
}
pub fn set_inactive(&mut self) {
self.active = false;
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.data.as_mut_slice()
}
pub fn piece_index(&self) -> PieceIndex {
self.piece_index
}
pub fn block_offset(&self) -> BlockOffset {
self.block_offset
}
pub fn block_len(&self) -> BlockLength {
self.block_len
}
}
impl AsSlice<u8> for Block {
fn as_slice<'a>(&'a self) -> &'a [u8] {
self.data.as_slice()
}
}