pub trait DiskIoBackend: Send + Sync {
Show 15 methods
// Required methods
fn name(&self) -> &str;
fn register(&self, info_hash: Id20, storage: Arc<dyn TorrentStorage>);
fn unregister(&self, info_hash: Id20);
fn write_chunk(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
data: Bytes,
flush: bool,
) -> Result<(), Error>;
fn read_chunk(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
length: u32,
volatile: bool,
) -> Result<Bytes, Error>;
fn read_piece(&self, info_hash: Id20, piece: u32) -> Result<Vec<u8>, Error>;
fn hash_piece(
&self,
info_hash: Id20,
piece: u32,
expected: &Id20,
) -> Result<bool, Error>;
fn hash_piece_v2(
&self,
info_hash: Id20,
piece: u32,
expected: &Id32,
) -> Result<bool, Error>;
fn hash_block(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
length: u32,
) -> Result<Id32, Error>;
fn clear_piece(&self, info_hash: Id20, piece: u32);
fn flush_piece(&self, info_hash: Id20, piece: u32) -> Result<(), Error>;
fn flush_all(&self) -> Result<(), Error>;
fn cached_pieces(&self, info_hash: Id20) -> Vec<u32>;
fn stats(&self) -> DiskIoStats;
fn write_block_direct(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
s0: &[u8],
s1: &[u8],
) -> Result<(), Error>;
}Expand description
Trait for pluggable disk I/O backends.
Implementations are shared across the session via Arc<dyn DiskIoBackend>.
All methods must be safe to call from multiple threads concurrently.
Required Methods§
Sourcefn register(&self, info_hash: Id20, storage: Arc<dyn TorrentStorage>)
fn register(&self, info_hash: Id20, storage: Arc<dyn TorrentStorage>)
Register a torrent’s storage for I/O operations.
Sourcefn unregister(&self, info_hash: Id20)
fn unregister(&self, info_hash: Id20)
Unregister a torrent’s storage.
Sourcefn write_chunk(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
data: Bytes,
flush: bool,
) -> Result<(), Error>
fn write_chunk( &self, info_hash: Id20, piece: u32, begin: u32, data: Bytes, flush: bool, ) -> Result<(), Error>
Write a chunk of piece data. If flush is true, persist immediately.
§Errors
Returns an error if the underlying storage write fails.
Sourcefn read_chunk(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
length: u32,
volatile: bool,
) -> Result<Bytes, Error>
fn read_chunk( &self, info_hash: Id20, piece: u32, begin: u32, length: u32, volatile: bool, ) -> Result<Bytes, Error>
Read a chunk of piece data. volatile hints the data won’t be re-read soon.
§Errors
Returns an error if the underlying storage read fails.
Sourcefn read_piece(&self, info_hash: Id20, piece: u32) -> Result<Vec<u8>, Error>
fn read_piece(&self, info_hash: Id20, piece: u32) -> Result<Vec<u8>, Error>
Read an entire piece from storage, returning its raw bytes.
For backends with a write buffer, the piece is flushed before reading.
§Errors
Returns an error if the underlying storage read fails.
Sourcefn hash_piece(
&self,
info_hash: Id20,
piece: u32,
expected: &Id20,
) -> Result<bool, Error>
fn hash_piece( &self, info_hash: Id20, piece: u32, expected: &Id20, ) -> Result<bool, Error>
Verify a piece against its expected SHA-1 hash.
§Errors
Returns an error if the underlying storage read fails.
Sourcefn hash_piece_v2(
&self,
info_hash: Id20,
piece: u32,
expected: &Id32,
) -> Result<bool, Error>
fn hash_piece_v2( &self, info_hash: Id20, piece: u32, expected: &Id32, ) -> Result<bool, Error>
Verify a piece against its expected SHA-256 hash (BEP 52).
§Errors
Returns an error if the underlying storage read fails.
Sourcefn hash_block(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
length: u32,
) -> Result<Id32, Error>
fn hash_block( &self, info_hash: Id20, piece: u32, begin: u32, length: u32, ) -> Result<Id32, Error>
Compute SHA-256 hash of a single block within a piece (BEP 52 Merkle).
§Errors
Returns an error if the underlying storage read fails.
Sourcefn clear_piece(&self, info_hash: Id20, piece: u32)
fn clear_piece(&self, info_hash: Id20, piece: u32)
Discard buffered data for a piece (e.g., after hash failure).
Sourcefn flush_piece(&self, info_hash: Id20, piece: u32) -> Result<(), Error>
fn flush_piece(&self, info_hash: Id20, piece: u32) -> Result<(), Error>
Flush buffered writes for a piece to persistent storage.
§Errors
Returns an error if the underlying storage flush fails.
Sourcefn flush_all(&self) -> Result<(), Error>
fn flush_all(&self) -> Result<(), Error>
Flush all buffered writes across all torrents.
§Errors
Returns an error if the underlying storage flush fails.
Sourcefn cached_pieces(&self, info_hash: Id20) -> Vec<u32>
fn cached_pieces(&self, info_hash: Id20) -> Vec<u32>
Return piece indices currently held in cache (for SuggestPiece, M44).
Sourcefn stats(&self) -> DiskIoStats
fn stats(&self) -> DiskIoStats
Return current I/O statistics.
Sourcefn write_block_direct(
&self,
info_hash: Id20,
piece: u32,
begin: u32,
s0: &[u8],
s1: &[u8],
) -> Result<(), Error>
fn write_block_direct( &self, info_hash: Id20, piece: u32, begin: u32, s0: &[u8], s1: &[u8], ) -> Result<(), Error>
Write a block directly to storage from two slices (vectored write).
Bypasses the buffer pool entirely — data goes straight to the
underlying TorrentStorage. The two slices represent contiguous
block data split across a ring-buffer wrap boundary.
This is the zero-copy direct-pwrite path used by peer tasks.
§Errors
Returns an error if the underlying storage write fails.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".