pub struct Lengths { /* private fields */ }Expand description
Piece and chunk arithmetic for BitTorrent downloads.
Manages the mapping between:
- Pieces: fixed-size blocks verified by SHA1 (except possibly the last piece)
- Chunks: sub-piece blocks requested from peers (typically 16 KiB)
- Files: the actual files on disk that pieces map across
Implementations§
Source§impl Lengths
impl Lengths
Sourcepub fn total_length(&self) -> u64
pub fn total_length(&self) -> u64
Total size of all content.
Sourcepub fn piece_length(&self) -> u64
pub fn piece_length(&self) -> u64
Standard piece length.
Sourcepub fn chunk_size(&self) -> u32
pub fn chunk_size(&self) -> u32
Chunk/block size.
Sourcepub fn num_pieces(&self) -> u32
pub fn num_pieces(&self) -> u32
Total number of pieces.
Sourcepub fn piece_size(&self, piece_index: u32) -> u32
pub fn piece_size(&self, piece_index: u32) -> u32
Actual length of a specific piece (last piece may be shorter).
Sourcepub fn chunks_in_piece(&self, piece_index: u32) -> u32
pub fn chunks_in_piece(&self, piece_index: u32) -> u32
Number of chunks in a specific piece.
Sourcepub fn chunk_info(
&self,
piece_index: u32,
chunk_index: u32,
) -> Option<(u32, u32)>
pub fn chunk_info( &self, piece_index: u32, chunk_index: u32, ) -> Option<(u32, u32)>
Offset and length of a specific chunk within a piece.
Returns (offset_within_piece, chunk_length).
Sourcepub fn piece_offset(&self, piece_index: u32) -> u64
pub fn piece_offset(&self, piece_index: u32) -> u64
Absolute byte offset for the start of a piece.
Sourcepub fn piece_index_for_byte(&self, byte_offset: u64) -> Option<u32>
pub fn piece_index_for_byte(&self, byte_offset: u64) -> Option<u32>
Map an absolute byte offset to a piece index.
Returns None when byte_offset >= total_length.
Prefer this over the ad-hoc (byte / piece_length) as u32 form: M132’s
in-flight underflow is the cautionary precedent for narrowing casts on
hot paths. Routing all byte→piece conversions through one bounded
function keeps that bug class in one place.
Sourcepub fn byte_to_piece_with_offset(&self, byte_offset: u64) -> Option<(u32, u32)>
pub fn byte_to_piece_with_offset(&self, byte_offset: u64) -> Option<(u32, u32)>
Map an absolute byte offset to (piece_index, offset_within_piece).
Sibling of Self::piece_index_for_byte for callers that also need
the offset within the piece (e.g. disk reads at a sub-piece position).
Returns None when byte_offset >= total_length.