Expand description
Traits and types for expressing I/O transfers of both byte-wise and block-wise granularity.
The important items are summarized below:
BlockReader
,BlockWriter
: traits that represent I/O streams which can be read from or written to at the granularity of a single block (as the smallest transferable chunk).BlockIo
: a parent trait that specifies the size in bytes of each block in a block-based I/O stream.KnownLength
: a trait that represents an I/O stream with a known length, such as a disk drive.ByteReader
,ByteWriter
: traits that represent I/O streams which can be read from or written to at the granularity of an individual byte.- Wrapper types that allow byte-wise access atop block-based I/O streams:
ByteReaderWrapper
,ByteWriterWrapper
,ByteReaderWriterWrapper
.- Notably, the
blocks_from_bytes()
function is useful for calculating the set of block-based I/O transfers that are needed to satisfy an arbitrary byte-wise transfer.
- Notably, the
For example, a storage device like a hard drive that supports transfers of 512-byte blocks
should implement BlockIo
, BlockReader
, BlockWriter
, and KnownLength
traits.
A user can then use those traits directly to transfer whole blocks to/from the device,
or wrap the storage device in one of the byte-wise reader/writer types
in order to transfer arbitrary bytes (as little as one byte) at a time to/from the device.
We also provide the LockableIo
type for convenient use with I/O streams or devices
that exist behind a shared lock, e.g., Arc<Mutex<IO>>
, Mutex<IO>
, etc.
This allows you to access the I/O stream transparently through the lock by using traits
that the interior IO
object implements, such as the block-wise I/O traits listed above.
Stateless vs. Stateful I/O
Note that the above traits represent “stateless” access into I/O streams or devices, in that successive read/write operations will not advance any kind of “offset” or cursor.
To read or write while tracking the current offset into the I/O stream,
we provide the ReaderWriter
, Reader
, and Writer
structs,
which act as “stateful” wrappers around an underlying “stateless” I/O stream
(such as a stateless ByteReader
or ByteWriter
).
This offers a more convenient interface with more traditional I/O behavior,
in which the next read or write operation will start where the prior one ended.
Structs
Lockable
type L
,
for the purpose of forwarding various IO-related traits through the lock to the IO
type.ByteReader
I/O stream.ByteWriter
I/O stream.Enums
Traits
BlockReader
and BlockWriter
.BlockIo::block_size()
function.BlockIo::block_size()
function.