pub struct ObjectListing { /* private fields */ }Expand description
An in-progress directory listing that yields ObjectInfo items one at a time.
Created by Storage::list_objects_stream(). After the device returns the handle list, the
total count is known immediately (total()). Each call to next()
fetches one object’s metadata, so the consumer can report progress (e.g.,
“Loading files (42 of 500)…”) as items arrive.
§Important
The device is busy while this listing is active. You must consume all items (or drop the listing) before calling other storage methods.
§Example
use mtp_rs::mtp::{ListingItem, MtpDevice};
let mut listing = storage.list_objects_stream(None).await?;
println!("Loading {} files...", listing.total());
while let Some(item) = listing.next().await {
match item? {
ListingItem::Object(info) => {
println!("[{}/{}] {}", listing.fetched(), listing.total(), info.filename);
}
ListingItem::Skipped(skipped) => {
eprintln!("could not read handle {}: {}", skipped.handle.0, skipped.error);
}
}
}Implementations§
Source§impl ObjectListing
impl ObjectListing
Sourcepub fn total(&self) -> usize
pub fn total(&self) -> usize
Total number of object handles returned by the device.
When a parent filter is active (e.g. devices that return all objects for root), some items may be skipped, so the actual yielded count can be lower.
Sourcepub async fn next(&mut self) -> Option<Result<ListingItem, Error>>
pub async fn next(&mut self) -> Option<Result<ListingItem, Error>>
Fetch the next item from the device.
Returns None when the listing is exhausted. Items that don’t match the parent filter are
skipped by the backend and never surface here.
The Ok side has two shapes, and the distinction is the whole point: a
ListingItem::Object is an object whose metadata was read, and a
ListingItem::Skipped is one handle the device refused in a way that leaves the rest of
the listing usable (see Storage::collect_objects for what qualifies). An Err means the
listing itself is over: transport trouble, a broken session, cancellation, a malformed
response.
So Err is “stop”, Ok(Skipped) is “this one is unreadable, keep going”, and you can’t
confuse them by accident. Consumers that don’t care can filter:
let mut listing = storage.list_objects_stream(None).await?;
while let Some(item) = listing.next().await {
if let ListingItem::Object(info) = item? {
println!("{}", info.filename);
}
}If a CancelToken was passed via Storage::list_objects_stream_with_cancel and it’s
been cancelled, this returns Some(Err(Error::Cancelled)) at the next per-handle boundary.