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:
- A partial single-block transfer of some bytes in the first block,
only if the start of
byte_rangeis not aligned toblock_size. - A multi-block transfer of contiguous whole blocks,
only if
byte_rangespans more than 2 blocks. - A partial single-block transfer of some bytes in the last block,
only if the end of
byte_rangeis not aligned toblock_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:
- Read 1 block (block
2) and transfer the last 36 bytes of that block (476..512) into the byte range1500..1536. - Read 4 blocks (blocks
3..7) and transfer all of those 2048 bytes into the byte range1536..3584. - Read 1 block (block
7) and transfer the first 366 bytes of that block (0..366) into the byte range3584..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.