pub struct FileDownload { /* private fields */ }Expand description
A file download in progress with true USB streaming.
This struct wraps the low-level ReceiveStream and provides convenient
methods for tracking progress. Data is streamed directly from USB as
chunks arrive, without buffering the entire file in memory.
§Important
The MTP session is locked while this download is active. You must consume the entire download (or drop it) before calling other storage methods.
§Example
ⓘ
let mut download = storage.download_stream(handle).await?;
println!("Downloading {} bytes...", download.size());
while let Some(chunk) = download.next_chunk().await {
let bytes = chunk?;
file.write_all(&bytes).await?;
println!("Progress: {:.1}%", download.progress() * 100.0);
}Implementations§
Source§impl FileDownload
impl FileDownload
Sourcepub fn bytes_received(&self) -> u64
pub fn bytes_received(&self) -> u64
Bytes received so far.
Sourcepub async fn next_chunk(&mut self) -> Option<Result<Bytes, Error>>
pub async fn next_chunk(&mut self) -> Option<Result<Bytes, Error>>
Get the next chunk of data from USB.
Returns None when the download is complete.
Sourcepub async fn collect_with_progress<F>(
self,
on_progress: F,
) -> Result<Vec<u8>, Error>
pub async fn collect_with_progress<F>( self, on_progress: F, ) -> Result<Vec<u8>, Error>
Consume the download and iterate with a progress callback.
Calls on_progress after each chunk. Return ControlFlow::Break(())
to cancel the download.
§Example
ⓘ
let data = download.collect_with_progress(|progress| {
println!("{:.1}%", progress.percent().unwrap_or(0.0));
ControlFlow::Continue(())
}).await?;Auto Trait Implementations§
impl Freeze for FileDownload
impl !RefUnwindSafe for FileDownload
impl Send for FileDownload
impl Sync for FileDownload
impl Unpin for FileDownload
impl UnsafeUnpin for FileDownload
impl !UnwindSafe for FileDownload
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more