cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! A stated-facts [`OsChild`]: what each call answers, in order, and nothing the OS would do. A call
//! past its table panics. It never blocks; a `Running` answer hands out a [`WaitFor`] that the test
//! fires with [`FakeProcess::wake`], and the driver's own blocking call is what a test counts.
//!
//! [`FakeProcess::over`] puts a real [`OsChild`] behind the tables: a call the table has an answer
//! for gets it, and every other call, `os_handle` included, is the real one.

use std::collections::VecDeque;
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Instant;

use super::child::KillRefusal;
use super::child::{ExitRecord, Foreign, OsChild, PeekOutcome, TryObserveOutcome};
use super::{Exit, OsProcess, WaitFor};
use crate::error::Error;
use crate::signal::{Sent, Sig};

/// One scripted answer of [`OsProcess::exited`].
#[derive(Debug, Clone, Copy)]
pub(crate) enum Answer {
    Exited,
    Running,
    /// The look fails with an `Error::Io` of this kind.
    Err(std::io::ErrorKind),
}

/// One scripted answer of [`OsChild::try_observe`], the waiter's look.
#[derive(Debug, Clone, Copy)]
pub(crate) enum ReapAnswer {
    /// The look finds the exit record, and leaves it in place.
    Exited(ExitRecord),
    Running,
    Recheck,
    Foreign(Foreign),
    /// The reap fails with an `Error::Io` of this kind.
    Err(std::io::ErrorKind),
}

/// One scripted answer of [`OsChild::peek`]. A failure is the whole `io::Error`, so a test can name
/// its message.
pub(crate) type PeekAnswer = Result<PeekOutcome, std::io::Error>;

/// One scripted answer of [`OsChild::try_reap`], the consume at the end of the handle. A failure is
/// the whole `io::Error`, as [`PeekAnswer`]'s is.
#[cfg(unix)]
pub(crate) type ConsumeAnswer = Result<crate::wait::exit_only::Reap, std::io::Error>;

/// One scripted answer of [`OsProcess::signal`]. A failure is the whole `io::Error`, so a test can
/// state a raw errno.
pub(crate) type SignalAnswer = Result<Sent, std::io::Error>;

/// A failed [`SignalAnswer`]: the signal is refused with the raw `errno`.
#[cfg(unix)]
pub(crate) fn failed_signal(errno: i32) -> SignalAnswer {
    Err(std::io::Error::from_raw_os_error(errno))
}

/// Cloning gives another handle on the same tables and the same wake source: the test keeps one,
/// the code under test owns the other.
#[derive(Clone)]
pub(crate) struct FakeProcess(Arc<Shared>);

struct Shared {
    script: Mutex<Script>,
    source: Source,
    /// What answers a call the tables have nothing for. `None`: such a call panics.
    real: Option<Box<dyn OsChild>>,
    /// What [`OsChild::kernel`] answers, once [`with_kernel`](FakeProcess::with_kernel) states it.
    kernel: std::sync::OnceLock<crate::identity::SpawnKernel>,
}

/// What calls consume and record, behind one lock so a `&FakeProcess` is `Sync`.
struct Script {
    /// What [`OsChild::pid`] answers. `None`: the real child's, if there is one, else no pid.
    pid: Option<u32>,
    /// What [`OsChild::kill_refusal`] answers. `None`: the real child's, if there is one; a fake
    /// with none states it or panics.
    kill_refusal: Option<KillRefusal>,
    exits: VecDeque<Answer>,
    peeks: VecDeque<PeekAnswer>,
    reaps: VecDeque<ReapAnswer>,
    signals: VecDeque<SignalAnswer>,
    /// What a peek answers once `peeks` is empty. `None`: such a call falls through.
    peeks_always: Option<Box<dyn FnMut() -> PeekAnswer + Send>>,
    /// What a reap answers once `reaps` is empty.
    reaps_always: Option<ReapAnswer>,
    /// What [`OsChild::number_holder`] answers, one read each. It answers before
    /// `number_reads_always`.
    #[cfg(unix)]
    number_reads: VecDeque<crate::identity::NumberRead>,
    /// What a read of the number answers once `number_reads` is empty. `None`: such a read goes to
    /// the real child's, if there is one and no kernel is stated, else through the kernel.
    #[cfg(unix)]
    number_reads_always: Option<crate::identity::Resolved<crate::identity::ProcessId>>,
    /// Calls that the repeated answers took.
    repeats: usize,
    /// `exited` and `try_observe` calls: the looks.
    looks: usize,
    /// What [`OsChild::try_reap`] answers, one call each.
    #[cfg(unix)]
    consumes: VecDeque<ConsumeAnswer>,
    /// What [`OsChild::exact_consume`] answers. `None`: the real child's, if there is one, else `true`.
    #[cfg(unix)]
    exact_consume: Option<bool>,
    /// [`OsChild::try_reap`] calls.
    #[cfg(unix)]
    reap_calls: usize,
    look_times: Vec<Instant>,
    signal_calls: Vec<Sig>,
    on_look: Option<Box<dyn FnMut(usize) + Send>>,
}

impl FakeProcess {
    /// A process that answers `exits` to successive `exited` looks. Every table starts empty, and a
    /// call past its table panics.
    pub(crate) fn new(exits: impl IntoIterator<Item = Answer>) -> Self {
        Self::with(exits, None)
    }

    /// A process that answers from its tables (all empty to start) and, where a table has nothing,
    /// from `real`.
    pub(crate) fn over(real: Box<dyn OsChild>) -> Self {
        Self::with([], Some(real))
    }

    fn with(exits: impl IntoIterator<Item = Answer>, real: Option<Box<dyn OsChild>>) -> Self {
        Self(Arc::new(Shared {
            script: Mutex::new(Script {
                pid: None,
                kill_refusal: None,
                exits: exits.into_iter().collect(),
                peeks: VecDeque::new(),
                reaps: VecDeque::new(),
                signals: VecDeque::new(),
                peeks_always: None,
                reaps_always: None,
                #[cfg(unix)]
                number_reads: VecDeque::new(),
                #[cfg(unix)]
                number_reads_always: None,
                repeats: 0,
                looks: 0,
                #[cfg(unix)]
                consumes: VecDeque::new(),
                #[cfg(unix)]
                exact_consume: None,
                #[cfg(unix)]
                reap_calls: 0,
                look_times: Vec::new(),
                signal_calls: Vec::new(),
                on_look: None,
            }),
            source: Source::new(),
            real,
            kernel: std::sync::OnceLock::new(),
        }))
    }

    /// Answer `kernel` to [`OsChild::kernel`]. Without it, a fake over a real child answers that
    /// child's, and any other panics.
    #[cfg(target_os = "linux")]
    pub(crate) fn with_kernel(self, kernel: crate::identity::SpawnKernel) -> Self {
        assert!(
            self.0.kernel.set(kernel).is_ok(),
            "FakeProcess: the kernel is stated once"
        );
        self
    }

    /// Answer `reads` to successive [`OsChild::number_holder`] calls, one each. Replaces the queue.
    /// Past them, see [`number_reads_always`](Self::number_reads_always).
    #[cfg(unix)]
    pub(crate) fn number_reads(self, reads: impl IntoIterator<Item = crate::identity::NumberRead>) -> Self {
        self.script().number_reads = reads.into_iter().collect();
        self
    }

    /// How many stated [`number_reads`](Self::number_reads) no call has consumed.
    #[cfg(unix)]
    pub(crate) fn number_reads_left(&self) -> usize {
        self.script().number_reads.len()
    }

    /// Panics unless every stated number read was consumed: a test that states a read asserts it,
    /// so a read that stopped happening fails.
    #[cfg(unix)]
    #[track_caller]
    pub(crate) fn assert_number_reads_exhausted(&self) {
        let left = self.number_reads_left();
        assert_eq!(left, 0, "{left} stated number read(s) were never consumed");
    }

    /// Answer every [`OsChild::number_holder`] the [`number_reads`](Self::number_reads) queue has
    /// nothing left for with `read`, no cause. With nothing stated, the real child's or the
    /// kernel answers.
    #[cfg(unix)]
    pub(crate) fn number_reads_always(self, read: crate::identity::Resolved<crate::identity::ProcessId>) -> Self {
        self.script().number_reads_always = Some(read);
        self
    }

    /// Answer `pid` to [`OsChild::pid`]. Without it, a fake over a real child answers that child's,
    /// and any other has no pid.
    pub(crate) fn with_pid(self, pid: u32) -> Self {
        self.script().pid = Some(pid);
        self
    }

    /// Answer `refusal` to [`OsChild::kill_refusal`]. Without it, a fake over a real child answers
    /// that child's, and any other panics.
    #[cfg(any(windows, target_os = "macos"))]
    pub(crate) fn with_kill_refusal(self, refusal: KillRefusal) -> Self {
        self.script().kill_refusal = Some(refusal);
        self
    }

    /// Answer `exits` to successive [`OsProcess::exited`] looks; a look past them goes to the real
    /// child of a fake made [`over`](Self::over) one.
    #[cfg(not(target_os = "macos"))]
    pub(crate) fn exits(self, exits: impl IntoIterator<Item = Answer>) -> Self {
        self.script().exits = exits.into_iter().collect();
        self
    }

    /// Answer `peeks` to successive [`OsChild::peek`] calls.
    pub(crate) fn peeks(self, peeks: impl IntoIterator<Item = PeekAnswer>) -> Self {
        self.script().peeks = peeks.into_iter().collect();
        self
    }

    /// Answer every [`OsChild::peek`] that the `peeks` table has nothing left for with `answer()`,
    /// however many calls there are.
    #[cfg(unix)]
    pub(crate) fn peeks_always(self, answer: impl FnMut() -> PeekAnswer + Send + 'static) -> Self {
        self.script().peeks_always = Some(Box::new(answer));
        self
    }

    /// How many scripted [`OsChild::peek`] answers no call has consumed.
    #[cfg(unix)]
    pub(crate) fn peeks_left(&self) -> usize {
        self.script().peeks.len()
    }

    /// Panics unless every scripted peek answer was consumed: a test that counts on a number of
    /// looks asserts it, so a look that stopped happening fails.
    #[cfg(unix)]
    #[track_caller]
    pub(crate) fn assert_peeks_exhausted(&self) {
        let left = self.peeks_left();
        assert_eq!(left, 0, "{left} scripted peek answer(s) were never consumed");
    }

    /// Answer `reaps` to successive [`OsChild::try_observe`] calls.
    pub(crate) fn reaps(self, reaps: impl IntoIterator<Item = ReapAnswer>) -> Self {
        self.script().reaps = reaps.into_iter().collect();
        self
    }

    /// Answer every [`OsChild::try_observe`] that the `reaps` table has nothing left for with `answer`,
    /// however many calls there are.
    #[cfg(target_os = "macos")]
    pub(crate) fn reaps_always(self, answer: ReapAnswer) -> Self {
        self.script().reaps_always = Some(answer);
        self
    }

    /// Stops [`peeks_always`](Self::peeks_always) and [`reaps_always`](Self::reaps_always) from
    /// answering: once the tables are empty, calls go to the real child.
    #[cfg(target_os = "macos")]
    pub(crate) fn no_repeats(self) -> Self {
        self.script().peeks_always = None;
        self.script().reaps_always = None;
        self
    }

    /// How many calls [`peeks_always`](Self::peeks_always) and [`reaps_always`](Self::reaps_always)
    /// answered.
    #[cfg(target_os = "macos")]
    pub(crate) fn repeats(&self) -> usize {
        self.script().repeats
    }

    /// Answer `consumes` to successive [`OsChild::try_reap`] calls. A call past them is the real
    /// child's, if there is one, else answers `Running`.
    #[cfg(unix)]
    pub(crate) fn consumes(self, consumes: impl IntoIterator<Item = ConsumeAnswer>) -> Self {
        self.script().consumes = consumes.into_iter().collect();
        self
    }

    /// Answer `exact` to [`OsChild::exact_consume`]. Without it, a fake over a real child answers
    /// that child's, and any other answers `true`.
    #[cfg(unix)]
    pub(crate) fn with_exact_consume(self, exact: bool) -> Self {
        self.script().exact_consume = Some(exact);
        self
    }

    /// Answer `signals` to successive [`OsProcess::signal`] calls.
    pub(crate) fn signals(self, signals: impl IntoIterator<Item = SignalAnswer>) -> Self {
        self.script().signals = signals.into_iter().collect();
        self
    }

    /// Fire the [`WaitFor`] that `Running` answers hand out; it stays fired, except that each
    /// `Running` answer of [`OsChild::try_observe`] hands out an unfired one.
    pub(crate) fn wake(&self) {
        self.0.source.fire();
    }

    /// How many looks were made: calls of `exited` and of `try_observe`.
    pub(crate) fn looks(&self) -> usize {
        self.script().looks
    }

    /// How many times the end of the handle asked this fake to consume the exit record
    /// ([`OsChild::try_reap`]).
    #[cfg(unix)]
    pub(crate) fn reap_calls(&self) -> usize {
        self.script().reap_calls
    }

    /// The `wait::now()` of each look, taken after its `on_look` hook ran.
    pub(crate) fn look_times(&self) -> Vec<Instant> {
        self.script().look_times.clone()
    }

    /// Each signal sent, in order.
    pub(crate) fn signal_calls(&self) -> Vec<Sig> {
        self.script().signal_calls.clone()
    }

    /// Run `hook` with the index of each look, at the start of the look. It runs with no lock held,
    /// so it may park, and other calls are answered meanwhile.
    pub(crate) fn on_look(&self, hook: impl FnMut(usize) + Send + 'static) {
        self.script().on_look = Some(Box::new(hook));
    }

    /// The lock, whether or not a hook panicked under it.
    fn script(&self) -> MutexGuard<'_, Script> {
        self.0.script.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Count a look and run its hook.
    fn begin_look(&self) {
        let (index, hook) = {
            let mut script = self.script();
            let index = script.looks;
            script.looks += 1;
            (index, script.on_look.take())
        };
        if let Some(mut hook) = hook {
            hook(index);
            let mut script = self.script();
            if script.on_look.is_none() {
                script.on_look = Some(hook);
            }
        }
        self.script().look_times.push(crate::wait::now());
    }
}

impl fmt::Debug for FakeProcess {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let script = self.script();
        f.debug_struct("FakeProcess")
            .field("exits", &script.exits)
            .field("looks", &script.looks)
            .field("repeats", &script.repeats)
            .finish_non_exhaustive()
    }
}

impl OsProcess for FakeProcess {
    fn signal(&self, sig: Sig) -> Result<Sent, Error> {
        let scripted = {
            let mut script = self.script();
            script.signal_calls.push(sig);
            script.signals.pop_front()
        };
        match (scripted, &self.0.real) {
            (Some(Ok(sent)), _) => Ok(sent),
            (Some(Err(error)), _) => Err(Error::Io(error)),
            (None, Some(real)) => real.signal(sig),
            (None, None) => panic!("FakeProcess: a signal was sent past the scripted answers"),
        }
    }

    fn exited(&self) -> Result<Exit<'_>, Error> {
        self.begin_look();
        let scripted = self.script().exits.pop_front();
        match (scripted, &self.0.real) {
            (Some(Answer::Exited), _) => Ok(Exit::Exited),
            (Some(Answer::Running), _) => Ok(Exit::Running(self.0.source.wait_for())),
            (Some(Answer::Err(kind)), _) => Err(Error::Io(kind.into())),
            (None, Some(real)) => real.exited(),
            (None, None) => panic!("FakeProcess: a look was made past the scripted answers"),
        }
    }

    #[cfg(any(target_os = "linux", windows))]
    fn os_handle(&self) -> super::child::OsHandle<'_> {
        match &self.0.real {
            Some(real) => real.os_handle(),
            None => self.0.source.os_handle(),
        }
    }
}

impl OsChild for FakeProcess {
    fn pid(&self) -> Option<u32> {
        let stated = self.script().pid;
        stated.or_else(|| self.0.real.as_ref().and_then(|real| real.pid()))
    }

    fn kernel(&self) -> &dyn crate::identity::Kernel {
        match (self.0.kernel.get(), &self.0.real) {
            (Some(stated), _) => &**stated,
            (None, Some(real)) => real.kernel(),
            (None, None) => panic!("FakeProcess: the kernel was asked past the stated facts"),
        }
    }

    #[cfg(unix)]
    fn number_holder(&self, pid: u32) -> crate::identity::NumberRead {
        let stated = {
            let mut script = self.script();
            let always = script.number_reads_always;
            script
                .number_reads
                .pop_front()
                .or(always.map(|read| (read, crate::identity::UnknownCause::default())))
        };
        stated.unwrap_or_else(|| match (self.0.kernel.get(), &self.0.real) {
            (None, Some(real)) => real.number_holder(pid),
            _ => crate::identity::ProcessId::of_with(self.kernel(), pid),
        })
    }

    fn kill_refusal(&self) -> KillRefusal {
        let stated = self.script().kill_refusal;
        match (stated, &self.0.real) {
            (Some(refusal), _) => refusal,
            (None, Some(real)) => real.kill_refusal(),
            (None, None) => panic!("FakeProcess: a kill refusal was asked past the stated facts"),
        }
    }

    fn peek(&self) -> Result<PeekOutcome, Error> {
        let scripted = {
            let mut script = self.script();
            script.peeks.pop_front().or_else(|| {
                let repeated = script.peeks_always.as_mut().map(|always| always());
                script.repeats += usize::from(repeated.is_some());
                repeated
            })
        };
        match (scripted, &self.0.real) {
            (Some(Ok(outcome)), _) => Ok(outcome),
            (Some(Err(error)), _) => Err(Error::Io(error)),
            (None, Some(real)) => real.peek(),
            (None, None) => panic!("FakeProcess: a peek was made past the scripted answers"),
        }
    }

    fn try_observe(&self) -> Result<TryObserveOutcome<'_>, Error> {
        self.begin_look();
        let scripted = {
            let mut script = self.script();
            script.reaps.pop_front().or_else(|| {
                let repeated = script.reaps_always;
                script.repeats += usize::from(repeated.is_some());
                repeated
            })
        };
        match (scripted, &self.0.real) {
            (Some(ReapAnswer::Exited(record)), _) => Ok(TryObserveOutcome::Exited(record)),
            (Some(ReapAnswer::Running), _) => {
                self.0.source.reset();
                Ok(TryObserveOutcome::Running(self.0.source.wait_for()))
            }
            (Some(ReapAnswer::Recheck), _) => Ok(TryObserveOutcome::Recheck),
            (Some(ReapAnswer::Foreign(foreign)), _) => Ok(TryObserveOutcome::Foreign(foreign)),
            (Some(ReapAnswer::Err(kind)), _) => Err(Error::Io(kind.into())),
            (None, Some(real)) => real.try_observe(),
            (None, None) => panic!("FakeProcess: a look was made past the scripted answers"),
        }
    }

    /// The scripted answer, else the real child's, else `Running`: a fake of stated facts consumes
    /// nothing. Every call is counted (see [`FakeProcess::reap_calls`]).
    #[cfg(unix)]
    fn try_reap(&self) -> Result<crate::wait::exit_only::Reap, Error> {
        let scripted = {
            let mut script = self.script();
            script.reap_calls += 1;
            script.consumes.pop_front()
        };
        match (scripted, &self.0.real) {
            (Some(answer), _) => answer.map_err(Error::Io),
            (None, Some(real)) => real.try_reap(),
            (None, None) => Ok(crate::wait::exit_only::Reap::Running),
        }
    }

    #[cfg(unix)]
    fn exact_consume(&self) -> bool {
        let stated = self.script().exact_consume;
        match (stated, &self.0.real) {
            (Some(exact), _) => exact,
            (None, Some(real)) => real.exact_consume(),
            (None, None) => true,
        }
    }
}

/// What a `Running` answer hands out, and the way a test fires it.
#[cfg(unix)]
struct Source {
    rx: std::io::PipeReader,
    tx: std::io::PipeWriter,
}

#[cfg(unix)]
impl Source {
    fn new() -> Self {
        let (rx, tx) = std::io::pipe().expect("FakeProcess: pipe");
        Self { rx, tx }
    }

    fn fire(&self) {
        use std::io::Write;
        (&self.tx).write_all(&[1]).expect("FakeProcess: fire the pipe");
    }

    /// Unfire it: read what `fire` wrote.
    fn reset(&self) {
        use std::io::Read;
        use std::os::fd::AsFd;
        while crate::os_process::poll::is_readable(self.rx.as_fd()).expect("FakeProcess: poll the pipe") {
            (&self.rx).read_exact(&mut [0]).expect("FakeProcess: unfire the pipe");
        }
    }

    fn wait_for(&self) -> WaitFor<'_> {
        use std::os::fd::AsFd;
        WaitFor::Readable(self.rx.as_fd())
    }

    #[cfg(target_os = "linux")]
    fn os_handle(&self) -> std::os::fd::BorrowedFd<'_> {
        use std::os::fd::AsFd;
        self.rx.as_fd()
    }
}

#[cfg(windows)]
struct Source(std::os::windows::io::OwnedHandle);

#[cfg(windows)]
impl Source {
    fn new() -> Self {
        Self(crate::os_process::windows::test_event::new_test_event().expect("FakeProcess: create the event"))
    }

    fn fire(&self) {
        crate::os_process::windows::test_event::signal_test_event(&self.0);
    }

    /// Unfire it.
    fn reset(&self) {
        use std::os::windows::io::AsRawHandle;
        // SAFETY: the event is alive for the call.
        unsafe {
            windows::Win32::System::Threading::ResetEvent(windows::Win32::Foundation::HANDLE(self.0.as_raw_handle()))
        }
        .expect("FakeProcess: reset the event");
    }

    fn wait_for(&self) -> WaitFor<'_> {
        WaitFor::Signalled(self.os_handle())
    }

    fn os_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
        use std::os::windows::io::AsHandle;
        self.0.as_handle()
    }
}