Skip to main content

coven_storage/
byte_progress.rs

1//! How far a request's object bytes have gone, for a caller that shows a
2//! transfer as it goes (ยง6.7): the bytes of a request body handed to the
3//! connection, or the bytes of a response received. Bytes sent are never
4//! confirmation: the provider may still refuse or lose what it was handed.
5
6use std::sync::Arc;
7
8/// Told how far through an object a request's bytes reach, counted from the
9/// object's start, each time more are handed over or received. A request
10/// sent again, such as after a refreshed sign-in, counts again from where
11/// it starts. Clones tell the same place.
12#[derive(Clone)]
13pub struct ByteProgress {
14    report: Option<Arc<dyn Fn(u64) + Send + Sync>>,
15    /// Where the request's body starts in its object.
16    start: u64,
17}
18
19impl std::fmt::Debug for ByteProgress {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        f.debug_struct("ByteProgress")
22            .field("read", &self.report.is_some())
23            .field("start", &self.start)
24            .finish()
25    }
26}
27
28impl ByteProgress {
29    /// Tell `report` each new count.
30    pub fn new(report: impl Fn(u64) + Send + Sync + 'static) -> Self {
31        Self { report: Some(Arc::new(report)), start: 0 }
32    }
33
34    /// Tell no one, for a request whose progress nothing shows.
35    pub fn unread() -> Self {
36        Self { report: None, start: 0 }
37    }
38
39    /// The same place told about bytes that start `start` bytes into the
40    /// object, as a part or a ranged read's do.
41    pub(crate) fn starting_at(&self, start: u64) -> Self {
42        Self { report: self.report.clone(), start: self.start + start }
43    }
44
45    /// `moved` bytes of this request's have gone: handed to the connection,
46    /// or received.
47    pub fn moved(&self, moved: u64) {
48        if let Some(report) = &self.report {
49            report(self.start + moved);
50        }
51    }
52}
53
54#[cfg(test)]
55#[path = "byte_progress_tests.rs"]
56mod tests;