use crate::Result;
#[async_trait::async_trait]
pub trait TorrentStorageAsync: Send + Sync {
async fn write_chunk(&self, piece: u32, begin: u32, data: &[u8]) -> Result<()>;
async fn read_chunk(&self, piece: u32, begin: u32, length: u32) -> Result<Vec<u8>>;
async fn read_piece(&self, piece: u32) -> Result<Vec<u8>>;
}
#[cfg(test)]
mod tests {
use super::*;
struct MockAsyncStorage;
#[async_trait::async_trait]
impl TorrentStorageAsync for MockAsyncStorage {
async fn write_chunk(&self, _piece: u32, _begin: u32, _data: &[u8]) -> Result<()> {
Ok(())
}
async fn read_chunk(&self, _piece: u32, _begin: u32, length: u32) -> Result<Vec<u8>> {
Ok(vec![0u8; length as usize])
}
async fn read_piece(&self, _piece: u32) -> Result<Vec<u8>> {
Ok(Vec::new())
}
}
#[test]
fn async_trait_compiles() {
let _storage: Box<dyn TorrentStorageAsync> = Box::new(MockAsyncStorage);
}
}