1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Cancelling and observing long reads from another thread: [`CancelToken`] and
//! [`Progress`].
use Arc;
use ;
use crateError;
/// A request, from any thread, that the reads of an image stop: one per image, shared by
/// every disc made from it ([`crate::Disc::share`], `clone`) and their cursors. Get it with
/// [`crate::Disc::cancel_token`] (or [`crate::Image::cancel_token`]) before handing the
/// disc to the thread that reads.
///
/// While the request stands, every call that reads audio sectors through any disc of the
/// image returns [`Error::Cancelled`]: a call that starts then at once, a call in progress
/// after at most 32 more sectors read (a cursor reads 32 at a time, a provisional search
/// at most 32 per probe), or, if the request arrives during one of the reads sized to the
/// range after a provisional start or a resume from a seek index, after that read of at most
/// [`crate::seek::MAX_READ_SECTORS`] sectors (1 MiB), plus the decoding of one group of frames already started for the
/// frame it is returning. Nothing is lost: the cursor stays where it was, the frames it
/// holds ahead included (decoding jobs already sent to a pool finish), and once the
/// request is [withdrawn](CancelToken::withdraw) it continues as if never interrupted (a
/// provisional search, [`crate::SeekPolicy::Speculative`], cancelled before it chose its
/// start, starts again). A cancelled cursor can also just be dropped. Opening a disc and
/// reading its TOC are not cancelled, nor is [`crate::Image::read_sectors`].
///
/// The request stays until withdrawn: a caller that keeps the disc (or any disc of the
/// image) withdraws it before reading again. A disc opened afresh, even of the same file,
/// has a token of its own.
///
/// ```no_run
/// # fn main() -> Result<(), scarletbook::Error> {
/// use scarletbook::{Disc, Error, Slot};
/// let mut disc = Disc::open(std::io::BufReader::new(std::fs::File::open("disc.iso")?))?;
/// let token = disc.cancel_token();
/// let reader = std::thread::spawn(move || -> Result<u32, Error> {
/// let mut frames = 0;
/// for frame in disc.track_audio(Slot::TwoChannel, 1)? {
/// match frame {
/// Ok(_) => frames += 1,
/// Err(Error::Cancelled) => break,
/// Err(e) => return Err(e),
/// }
/// }
/// Ok(frames)
/// });
/// token.cancel(); // from this thread, for example on Ctrl-C
/// let _frames = reader.join().expect("reader thread")?;
/// # Ok(())
/// # }
/// ```
;
/// How far a cursor has read, readable from any thread while the cursor reads: the
/// sectors it has read through the image so far, and the sectors it expects to have read
/// once it is done. Get it from the cursor (for example
/// [`crate::TrackAudioCursor::progress`]) before handing the cursor to the thread that
/// reads; it stays valid after the cursor is dropped.
///
/// The counts are updated with every read the cursor makes (at most 32 sectors at a
/// time, except the reads sized to the range after a provisional start or a resume from a
/// seek index, of at most
/// [`crate::seek::MAX_READ_SECTORS`]): the scan before a range's first frame, a provisional search, a seek index's
/// replay and the range itself. `sectors_read` is exact; `sectors_expected` is an
/// estimate (the range's sectors are not known until they are read: its end is placed in
/// proportion to the area's play time), revised as the cursor reads, at least
/// `sectors_read`, and equal to it once the cursor has read its range to the end. A
/// continued cursor ([`crate::Disc::continue_track_audio`] and the like) is a new reading
/// with counts of its own: ask it for its own `Progress`; the earlier one no longer moves.
;