Skip to main content

Lengths

Struct Lengths 

Source
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

Source

pub fn new(total_length: u64, piece_length: u64, chunk_size: u32) -> Self

Create a new Lengths calculator.

§Panics

Panics if piece_length or chunk_size is 0.

Source

pub fn total_length(&self) -> u64

Total size of all content.

Source

pub fn piece_length(&self) -> u64

Standard piece length.

Source

pub fn chunk_size(&self) -> u32

Chunk/block size.

Source

pub fn num_pieces(&self) -> u32

Total number of pieces.

Source

pub fn piece_size(&self, piece_index: u32) -> u32

Actual length of a specific piece (last piece may be shorter).

Source

pub fn chunks_in_piece(&self, piece_index: u32) -> u32

Number of chunks in a specific piece.

Source

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).

Source

pub fn piece_offset(&self, piece_index: u32) -> u64

Absolute byte offset for the start of a piece.

Source

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.

Source

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.

Source

pub fn file_pieces( &self, file_offset: u64, file_length: u64, ) -> Option<(u32, u32)>

Given file boundaries, determine which pieces a file spans. Returns (first_piece, last_piece) inclusive.

Trait Implementations§

Source§

impl Clone for Lengths

Source§

fn clone(&self) -> Lengths

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Lengths

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Lengths

Source§

impl PartialEq for Lengths

Source§

fn eq(&self, other: &Lengths) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Lengths

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.