Function io::blocks_from_bytes

source ·
pub fn blocks_from_bytes(
    byte_range: Range<usize>,
    block_size: usize
) -> [Option<BlockByteTransfer>; 3]
Expand description

Calculates block-wise bounds for an I/O transfer based on a byte-wise range into a block-wise stream.

This function returns transfer operations that prioritize using fewer temporary buffers and fewer data copy operations between those buffers instead of prioritizing issuing fewer I/O transfer operations. If you prefer to issue a single I/O transfer to cover the whole range of byte (which may be faster depending on the underlying I/O device), then you should not use this function.

There are up to three transfer operations that can possibly occur, depending on the alignment of the byte-wise range:

  1. A partial single-block transfer of some bytes in the first block, only if the start of byte_range is not aligned to block_size.
  2. A multi-block transfer of contiguous whole blocks, only if byte_range spans more than 2 blocks.
  3. A partial single-block transfer of some bytes in the last block, only if the end of byte_range is not aligned to block_size.

Example

Given a read request for a byte_range of 1500..3950 and a block_size of 512 bytes, this function will return the following three transfer operations:

  1. Read 1 block (block 2) and transfer the last 36 bytes of that block (476..512) into the byte range 1500..1536.
  2. Read 4 blocks (blocks 3..7) and transfer all of those 2048 bytes into the byte range 1536..3584.
  3. Read 1 block (block 7) and transfer the first 366 bytes of that block (0..366) into the byte range 3584..3950.

Arguments

  • byte_range: the absolute range of bytes where the I/O transfer starts and ends, specified as absolute offsets from the beginning of the block-wise I/O stream.
  • block_size: the size in bytes of each block in the block-wise I/O stream.

Return

Returns a list of the three above transfer operations, enclosed in Options to convey that not all operations may be necessary.