compio-driver 0.12.0-rc.1

Low-level driver for compio
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Platform-specific drivers.
//!
//! Some types differ by compilation target.

#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(feature = "once_cell_try", feature(once_cell_try))]
#![allow(unused_features)]
#![warn(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![doc(
    html_logo_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
)]
#![doc(
    html_favicon_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
)]

use std::{
    io,
    num::NonZero,
    task::{Poll, Waker},
    time::Duration,
};

use compio_buf::BufResult;
use compio_log::instrument;

mod control;
mod macros;
mod panic;

mod key;
pub use key::Key;

mod asyncify;
pub use asyncify::*;

mod fd;
pub use fd::*;

mod driver_type;
pub use driver_type::*;

mod sys;
pub use sys::{op, *};

mod cancel;
pub use cancel::*;

mod buffer_pool;
pub use buffer_pool::{BoxAllocator, BufferAllocator, BufferPool, BufferRef};

use crate::{
    buffer_pool::{BufferAlloc, BufferPoolRoot},
    key::ErasedKey,
    panic::resume_unwind_io,
    sys::op::OpCodeFlag,
};

/// The return type of [`Proactor::push`].
#[derive(Debug)]
pub enum PushEntry<K, R> {
    /// The operation is pushed to the submission queue.
    Pending(K),
    /// The operation is ready and returns.
    Ready(R),
}

impl<K, R> PushEntry<K, R> {
    /// Get if the current variant is [`PushEntry::Ready`].
    pub const fn is_ready(&self) -> bool {
        matches!(self, Self::Ready(_))
    }

    /// Take the ready variant if exists.
    pub fn take_ready(self) -> Option<R> {
        match self {
            Self::Pending(_) => None,
            Self::Ready(res) => Some(res),
        }
    }

    /// Map the [`PushEntry::Pending`] branch.
    pub fn map_pending<L>(self, f: impl FnOnce(K) -> L) -> PushEntry<L, R> {
        match self {
            Self::Pending(k) => PushEntry::Pending(f(k)),
            Self::Ready(r) => PushEntry::Ready(r),
        }
    }

    /// Map the [`PushEntry::Ready`] branch.
    pub fn map_ready<S>(self, f: impl FnOnce(R) -> S) -> PushEntry<K, S> {
        match self {
            Self::Pending(k) => PushEntry::Pending(k),
            Self::Ready(r) => PushEntry::Ready(f(r)),
        }
    }
}

/// Low-level actions of completion-based IO.
/// It owns the operations to keep the driver safe.
pub struct Proactor {
    driver: Driver,
    buffer_pool: BufferPoolState,
}

enum BufferPoolState {
    Uninit {
        allocator: BufferAlloc,
        num_of_bufs: u16,
        buffer_len: usize,
        flags: u16,
    },
    Init(BufferPoolRoot),
}

impl BufferPoolState {
    fn get(&mut self, driver: &mut Driver) -> io::Result<BufferPool> {
        loop {
            match self {
                BufferPoolState::Uninit {
                    allocator,
                    num_of_bufs,
                    buffer_len,
                    flags,
                } => {
                    *self = BufferPoolState::Init(BufferPoolRoot::new(
                        driver,
                        *allocator,
                        *num_of_bufs,
                        *buffer_len,
                        *flags,
                    )?);
                }
                BufferPoolState::Init(root) => return Ok(root.get_pool()),
            }
        }
    }
}

impl Drop for Proactor {
    fn drop(&mut self) {
        let BufferPoolState::Init(buffer_pool) = &mut self.buffer_pool else {
            return;
        };
        debug_assert!(buffer_pool.is_unique()); // Just in case. Shouldn't happen
        _ = unsafe { buffer_pool.release(&mut self.driver) };
    }
}

assert_not_impl!(Proactor, Send);
assert_not_impl!(Proactor, Sync);

impl Proactor {
    /// Create [`Proactor`] with 1024 entries.
    pub fn new() -> io::Result<Self> {
        Self::builder().build()
    }

    /// Create [`ProactorBuilder`] to config the proactor.
    pub fn builder() -> ProactorBuilder {
        ProactorBuilder::new()
    }

    fn with_builder(builder: &ProactorBuilder) -> io::Result<Self> {
        Ok(Self {
            driver: Driver::new(builder)?,
            buffer_pool: BufferPoolState::Uninit {
                allocator: builder.buffer_pool_allocator,
                num_of_bufs: builder.buffer_pool_size,
                buffer_len: builder.buffer_pool_buffer_len,
                flags: builder.buffer_pool_flag,
            },
        })
    }

    /// Get a default [`Extra`] for underlying driver.
    pub fn default_extra(&self) -> Extra {
        Extra::new(&self.driver)
    }

    /// The current driver type.
    pub fn driver_type(&self) -> DriverType {
        self.driver.driver_type()
    }

    /// Attach an fd to the driver.
    ///
    /// ## Platform specific
    /// * IOCP: it will be attached to the completion port. An fd could only be
    ///   attached to one driver, and could only be attached once, even if you
    ///   `try_clone` it.
    /// * io-uring & polling: it will do nothing but return `Ok(())`.
    pub fn attach(&mut self, fd: RawFd) -> io::Result<()> {
        self.driver.attach(fd)
    }

    /// Cancel an operation with the pushed [`Key`].
    ///
    /// Returns the result if the key is unique and the operation is completed.
    ///
    /// The cancellation is not reliable. The underlying operation may continue,
    /// but just don't return from [`Proactor::poll`].
    pub fn cancel<T: OpCode>(&mut self, key: Key<T>) -> Option<BufResult<usize, T>> {
        instrument!(compio_log::Level::DEBUG, "cancel", ?key);
        if key.set_cancelled() {
            return None;
        }
        if key.is_unique() && key.has_result() {
            let (res, buf) = key.take_result().into_parts();
            Some(BufResult(resume_unwind_io(res), buf))
        } else {
            self.driver.cancel(key.erase());
            None
        }
    }

    /// Cancel an operation with a [`Cancel`] token.
    ///
    /// Returns if a cancellation has been issued.
    ///
    /// The cancellation is not reliable. The underlying operation may continue,
    /// but just don't return from [`Proactor::pop`]. This will do nothing if
    /// the operation has already been completed or cancelled before.
    pub fn cancel_token(&mut self, token: Cancel) -> bool {
        instrument!(compio_log::Level::DEBUG, "cancel_token", ?token);

        let Some(key) = token.upgrade() else {
            return false;
        };
        if key.set_cancelled() || key.has_result() {
            return false;
        }
        self.driver.cancel(key);
        true
    }

    /// Create a [`Cancel`] that can be used to cancel the operation even
    /// without the key.
    ///
    /// This acts like a weak reference to the [`Key`], but can only be used to
    /// cancel the operation with [`Proactor::cancel_token`]. Extra copy of
    /// [`Key`] may cause [`Proactor::pop`] to panic while keys registered
    /// as [`Cancel`] will not. So this is useful in cases where you're not sure
    /// if the operation will be cancelled.
    pub fn register_cancel<T: OpCode>(&mut self, key: &Key<T>) -> Cancel {
        Cancel::new(key)
    }

    /// Push an operation into the driver, and return the unique key [`Key`],
    /// associated with it.
    pub fn push<T: sys::OpCode + 'static>(
        &mut self,
        op: T,
    ) -> PushEntry<Key<T>, BufResult<usize, T>> {
        self.push_with_extra(op, self.default_extra())
    }

    /// Push an operation into the driver with user-defined [`Extra`], and
    /// return the unique key [`Key`], associated with it.
    pub fn push_with_extra<T: sys::OpCode + 'static>(
        &mut self,
        op: T,
        extra: Extra,
    ) -> PushEntry<Key<T>, BufResult<usize, T>> {
        let key = Key::new(op, extra, self.driver_type());
        match self.driver.push(key.clone().erase()) {
            Poll::Pending => PushEntry::Pending(key),
            Poll::Ready(res) => {
                key.set_result(res);
                PushEntry::Ready(key.take_result())
            }
        }
    }

    /// Poll the driver and get completed entries.
    /// You need to call [`Proactor::pop`] to get the pushed
    /// operations.
    pub fn poll(&mut self, timeout: Option<Duration>) -> io::Result<()> {
        self.driver.poll(timeout)
    }

    /// Get the pushed operations from the completion entries.
    ///
    /// # Panics
    ///
    /// This function will panic if the [`Key`] is not unique or if the
    /// operation is blocking and it panicked in the thread pool.
    pub fn pop<T: OpCode>(&mut self, key: Key<T>) -> PushEntry<Key<T>, BufResult<usize, T>> {
        instrument!(compio_log::Level::DEBUG, "pop", ?key);
        if key.has_result() {
            let (res, buf) = key.take_result().into_parts();
            PushEntry::Ready(BufResult(resume_unwind_io(res), buf))
        } else {
            PushEntry::Pending(key)
        }
    }

    /// Get the pushed operations from the completion entries along the
    /// [`Extra`] associated.
    ///
    /// # Panics
    ///
    /// This function will panic if the [`Key`] is not unique or if the
    /// operation is blocking and it panicked in the thread pool.
    pub fn pop_with_extra<T: OpCode>(
        &mut self,
        key: Key<T>,
    ) -> PushEntry<Key<T>, (BufResult<usize, T>, Extra)> {
        instrument!(compio_log::Level::DEBUG, "pop", ?key);
        if key.has_result() {
            let extra = key.swap_extra(self.default_extra());
            let (res, buf) = key.take_result().into_parts();
            PushEntry::Ready((BufResult(resume_unwind_io(res), buf), extra))
        } else {
            PushEntry::Pending(key)
        }
    }

    /// Get one completion entry for a multishot operation. If it returns
    /// [`None`], the user should call [`Proactor::pop_with_extra`] to get the
    /// final result of the operation.
    pub fn pop_multishot<T: OpCode>(&mut self, key: &Key<T>) -> Option<BufResult<usize, Extra>> {
        instrument!(compio_log::Level::DEBUG, "pop_multishot", ?key);
        self.driver.pop_multishot(key)
    }

    /// Update the waker of the specified op.
    pub fn update_waker<T>(&mut self, op: &Key<T>, waker: &Waker) {
        op.set_waker(waker);
    }

    /// Create a waker to interrupt the inner driver.
    pub fn waker(&self) -> Waker {
        self.driver.waker()
    }

    /// Register file descriptors for fixed-file operations with io_uring.
    ///
    /// This only works on `io_uring` driver. It will return an [`Unsupported`]
    /// error on other drivers.
    ///
    /// [`Unsupported`]: std::io::ErrorKind::Unsupported
    pub fn register_files(&self, fds: &[RawFd]) -> io::Result<()> {
        fn unsupported(_: &[RawFd]) -> io::Error {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "Fixed-file registration is only supported on io-uring driver",
            )
        }

        #[cfg(io_uring)]
        match self.driver.as_iour() {
            Some(iour) => iour.register_files(fds),
            None => Err(unsupported(fds)),
        }

        #[cfg(not(io_uring))]
        Err(unsupported(fds))
    }

    /// Unregister previously registered file descriptors.
    ///
    /// This only works on `io_uring` driver. It will return an [`Unsupported`]
    /// error on other drivers.
    ///
    /// [`Unsupported`]: std::io::ErrorKind::Unsupported
    pub fn unregister_files(&self) -> io::Result<()> {
        fn unsupported() -> io::Error {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "Fixed-file unregistration is only supported on io-uring driver",
            )
        }

        #[cfg(io_uring)]
        match self.driver.as_iour() {
            Some(iour) => iour.unregister_files(),
            None => Err(unsupported()),
        }

        #[cfg(not(io_uring))]
        Err(unsupported())
    }

    /// Register a new personality in io-uring driver.
    ///
    /// Returns the personality id, which can be used with
    /// [`Extra::set_personality`] to set the personality for an operation.
    ///
    /// This only works on `io_uring` driver. It will return an [`Unsupported`]
    /// error on other drivers. See [`Submitter::register_personality`] for
    /// more.
    ///
    /// [`Unsupported`]: std::io::ErrorKind::Unsupported
    /// [`Submitter::register_personality`]: https://docs.rs/io-uring/latest/io_uring/struct.Submitter.html#method.register_personality
    pub fn register_personality(&self) -> io::Result<u16> {
        fn unsupported() -> io::Error {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "Personality is only supported on io-uring driver",
            )
        }

        #[cfg(io_uring)]
        match self.driver.as_iour() {
            Some(iour) => iour.register_personality(),
            None => Err(unsupported()),
        }

        #[cfg(not(io_uring))]
        Err(unsupported())
    }

    /// Unregister the given personality in io-uring driver.
    ///
    /// This only works on `io_uring` driver. It will return an [`Unsupported`]
    /// error on other drivers. See [`Submitter::unregister_personality`] for
    /// more.
    ///
    /// [`Unsupported`]: std::io::ErrorKind::Unsupported
    /// [`Submitter::unregister_personality`]: https://docs.rs/io-uring/latest/io_uring/struct.Submitter.html#method.unregister_personality
    pub fn unregister_personality(&self, personality: u16) -> io::Result<()> {
        fn unsupported(_: u16) -> io::Error {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "Personality is only supported on io-uring driver",
            )
        }

        #[cfg(io_uring)]
        match self.driver.as_iour() {
            Some(iour) => iour.unregister_personality(personality),
            None => Err(unsupported(personality)),
        }

        #[cfg(not(io_uring))]
        Err(unsupported(personality))
    }

    /// Get the buffer pool of the driver.
    ///
    /// This will lazily initialize the pool at the first time it's accessed,
    /// and future access to the pool will be cheap and infallible.
    pub fn buffer_pool(&mut self) -> io::Result<BufferPool> {
        self.buffer_pool.get(&mut self.driver)
    }
}

impl AsRawFd for Proactor {
    fn as_raw_fd(&self) -> RawFd {
        self.driver.as_raw_fd()
    }
}

/// An completed entry returned from kernel.
///
/// This represents the ownership of [`Key`] passed into the kernel is given
/// back from it to the driver.
#[derive(Debug)]
pub(crate) struct Entry {
    key: ErasedKey,
    result: io::Result<usize>,

    #[cfg(io_uring)]
    flags: u32,
}

unsafe impl Send for Entry {}
unsafe impl Sync for Entry {}

impl Entry {
    pub(crate) fn new(key: ErasedKey, result: io::Result<usize>) -> Self {
        #[cfg(not(io_uring))]
        {
            Self { key, result }
        }
        #[cfg(io_uring)]
        {
            Self {
                key,
                result,
                flags: 0,
            }
        }
    }

    #[allow(dead_code)]
    pub fn user_data(&self) -> usize {
        self.key.as_raw()
    }

    #[allow(dead_code)]
    pub fn into_key(self) -> ErasedKey {
        self.key
    }

    #[cfg(io_uring)]
    pub fn flags(&self) -> u32 {
        self.flags
    }

    #[cfg(io_uring)]
    pub(crate) fn set_flags(&mut self, flags: u32) {
        self.flags = flags;
    }

    pub fn notify(self) {
        #[cfg(io_uring)]
        self.key.borrow().extra_mut().set_flags(self.flags());
        self.key.set_result(self.result);
    }
}

#[derive(Debug, Clone)]
enum ThreadPoolBuilder {
    Create { limit: usize, recv_limit: Duration },
    Reuse(AsyncifyPool),
}

impl Default for ThreadPoolBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ThreadPoolBuilder {
    pub fn new() -> Self {
        Self::Create {
            limit: 256,
            recv_limit: Duration::from_secs(60),
        }
    }

    pub fn create_or_reuse(&self) -> AsyncifyPool {
        match self {
            Self::Create { limit, recv_limit } => AsyncifyPool::new(*limit, *recv_limit),
            Self::Reuse(pool) => pool.clone(),
        }
    }
}

/// Builder for [`Proactor`].
#[derive(Debug, Clone)]
pub struct ProactorBuilder {
    capacity: u32,
    pool_builder: ThreadPoolBuilder,
    sqpoll_idle: Option<Duration>,
    cqsize: Option<u32>,
    coop_taskrun: bool,
    taskrun_flag: bool,
    eventfd: Option<RawFd>,
    driver_type: Option<DriverType>,
    op_flags: OpCodeFlag,
    buffer_pool_size: u16,
    buffer_pool_flag: u16,
    buffer_pool_buffer_len: usize,
    buffer_pool_allocator: BufferAlloc,
}

// SAFETY: `RawFd` is thread safe.
unsafe impl Send for ProactorBuilder {}
unsafe impl Sync for ProactorBuilder {}

impl Default for ProactorBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ProactorBuilder {
    /// Create the builder with default config.
    pub fn new() -> Self {
        Self {
            capacity: 1024,
            pool_builder: ThreadPoolBuilder::new(),
            sqpoll_idle: None,
            cqsize: None,
            coop_taskrun: false,
            taskrun_flag: false,
            eventfd: None,
            driver_type: None,
            op_flags: OpCodeFlag::empty(),
            buffer_pool_size: 8,
            buffer_pool_flag: 0,
            buffer_pool_buffer_len: 8192,
            buffer_pool_allocator: BufferAlloc::new::<BoxAllocator>(),
        }
    }

    /// Set the capacity of the inner event queue or submission queue, if
    /// exists. The default value is 1024.
    pub fn capacity(&mut self, capacity: u32) -> &mut Self {
        self.capacity = capacity;
        self
    }

    /// Set the completion queue size of io-uring driver. The value should be
    /// greater than `capacity`.
    pub fn cqsize(&mut self, cqsize: u32) -> &mut Self {
        self.cqsize = Some(cqsize);
        self
    }

    /// Set the thread number limit of the inner thread pool, if exists. The
    /// default value is 256.
    ///
    /// It will be ignored if `reuse_thread_pool` is set.
    ///
    /// Warning: some operations don't work if the limit is set to zero:
    /// * `Asyncify` needs thread pool.
    /// * Operations except `Recv*`, `Send*`, `Connect`, `Accept` may need
    ///   thread pool.
    pub fn thread_pool_limit(&mut self, value: usize) -> &mut Self {
        if let ThreadPoolBuilder::Create { limit, .. } = &mut self.pool_builder {
            *limit = value;
        }
        self
    }

    /// Set the waiting timeout of the inner thread, if exists. The default is
    /// 60 seconds.
    ///
    /// It will be ignored if `reuse_thread_pool` is set.
    pub fn thread_pool_recv_timeout(&mut self, timeout: Duration) -> &mut Self {
        if let ThreadPoolBuilder::Create { recv_limit, .. } = &mut self.pool_builder {
            *recv_limit = timeout;
        }
        self
    }

    /// Set to reuse an existing [`AsyncifyPool`] in this proactor.
    pub fn reuse_thread_pool(&mut self, pool: AsyncifyPool) -> &mut Self {
        self.pool_builder = ThreadPoolBuilder::Reuse(pool);
        self
    }

    /// Force reuse the thread pool for each proactor created by this builder,
    /// even `reuse_thread_pool` is not set.
    pub fn force_reuse_thread_pool(&mut self) -> &mut Self {
        self.reuse_thread_pool(self.create_or_get_thread_pool());
        self
    }

    /// Create or reuse the thread pool from the config.
    pub fn create_or_get_thread_pool(&self) -> AsyncifyPool {
        self.pool_builder.create_or_reuse()
    }

    /// Set `io-uring` sqpoll idle duration,
    ///
    /// This will also enable io-uring's sqpoll feature.
    ///
    /// # Notes
    ///
    /// - Only effective when the `io-uring` feature is enabled
    /// - `idle` must be >= 1ms, otherwise sqpoll idle will be set to 0 ms
    /// - `idle` will be rounded down
    pub fn sqpoll_idle(&mut self, idle: Duration) -> &mut Self {
        self.sqpoll_idle = Some(idle);
        self
    }

    /// Optimize performance for most cases, especially compio is a single
    /// thread runtime.
    ///
    /// However, it can't run with sqpoll feature.
    ///
    /// # Notes
    ///
    /// - Available since Linux Kernel 5.19.
    /// - Only effective when the `io-uring` feature is enabled
    pub fn coop_taskrun(&mut self, enable: bool) -> &mut Self {
        self.coop_taskrun = enable;
        self
    }

    /// Allows io-uring driver to know if any cqe's are available when try to
    /// push an sqe to the submission queue.
    ///
    /// This should be enabled with [`coop_taskrun`](Self::coop_taskrun)
    ///
    /// # Notes
    ///
    /// - Available since Linux Kernel 5.19.
    /// - Only effective when the `io-uring` feature is enabled
    pub fn taskrun_flag(&mut self, enable: bool) -> &mut Self {
        self.taskrun_flag = enable;
        self
    }

    /// Register an eventfd to io-uring.
    ///
    /// # Notes
    ///
    /// - Only effective when the `io-uring` feature is enabled
    pub fn register_eventfd(&mut self, fd: RawFd) -> &mut Self {
        self.eventfd = Some(fd);
        self
    }

    /// Set which io-uring [`OpCode`] must be supported by the driver.
    ///
    /// Support for io-uring opcodes varies by kernel version. Setting this
    /// will force the driver to check for support of the specified opcodes, and
    /// when any of them are not supported:
    ///
    /// - Fallback to `polling` driver if it is enabled, or
    /// - Return an [`Unsupported`] error when building the proactor otherwise.
    ///
    /// # Notes
    ///
    /// - Only effective when the `io-uring` feature is enabled
    /// - [`OpCodeFlag`] is a bitflag struct, you can combine multiple opcodes
    ///   with bitwise OR or use [`OpCodeFlag::all`] to require all opcodes to
    ///   be supported.
    ///
    /// [`Unsupported`]: std::io::ErrorKind::Unsupported
    pub fn detect_opcode_support(&mut self, flags: OpCodeFlag) -> &mut Self {
        self.op_flags = flags;
        self
    }

    /// Force a driver type to use.
    ///
    /// It is ignored if the fusion driver is disabled.
    pub fn driver_type(&mut self, t: DriverType) -> &mut Self {
        self.driver_type = Some(t);
        self
    }

    /// Number of buffers in the buffer pool.
    ///
    /// `size` will be rounded up if it's not power of 2.
    ///
    /// Default to be `8`.
    pub fn buffer_pool_size(&mut self, size: NonZero<u16>) -> &mut Self {
        self.buffer_pool_size = size.get();
        self
    }

    /// Flag to be used to initialize buffer pool.
    ///
    /// This is only supported on io-uring driver.
    ///
    /// Default to be `0`.
    pub fn buffer_pool_flag(&mut self, flag: u16) -> &mut Self {
        self.buffer_pool_flag = flag;
        self
    }

    /// Length of each buffer pool's buffer.
    ///
    /// Default to be `8192`.
    pub fn buffer_pool_buffer_len(&mut self, size: usize) -> &mut Self {
        self.buffer_pool_buffer_len = size;
        self
    }

    /// Set the allocator for buffer pool.
    ///
    /// This is different from the std's unstable `Allocator` trait: it's purely
    /// static and doesn't take an instance at all. This means implementation
    /// should be global (e.g., `Global`, `malloc` or `mmap`).
    ///
    /// Default to [`BoxAllocator`].
    ///
    /// # Note
    ///
    /// Default allocator performs [very poor] when using managed i/o on Zen 3,
    /// possibly due to [a bug related to FSRM]. If you observe such a problem,
    /// try swap the allocator to a mmap-based one may solve it.
    ///
    /// [very poor]: https://github.com/compio-rs/compio/issues/472
    /// [a bug related to FSRM]: https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/2030515
    pub fn buffer_pool_allocator<A: BufferAllocator>(&mut self) -> &mut Self {
        self.buffer_pool_allocator = BufferAlloc::new::<A>();
        self
    }

    /// Build the [`Proactor`].
    pub fn build(&self) -> io::Result<Proactor> {
        Proactor::with_builder(self)
    }
}

mod seal {
    use std::io;

    use compio_buf::BufResult;

    pub(crate) trait Seal {}

    impl Seal for io::Error {}
    impl<T> Seal for io::Result<T> {}
    impl<T, B> Seal for BufResult<T, B> {}
}

/// Extension trait for [`io::Error`] and results with it.
#[allow(private_bounds)]
pub trait ErrorExt: seal::Seal {
    #[doc(hidden)]
    fn as_io_error(&self) -> Option<&io::Error>;

    /// Whether the error or result is cancelled.
    fn is_cancelled(&self) -> bool {
        #[cfg(unix)]
        const CANCEL_ERROR: i32 = libc::ECANCELED;
        #[cfg(windows)]
        const CANCEL_ERROR: i32 = windows_sys::Win32::Foundation::ERROR_OPERATION_ABORTED as _;

        self.as_io_error()
            .and_then(io::Error::raw_os_error)
            .is_some_and(|e| e == CANCEL_ERROR)
    }
}

impl ErrorExt for io::Error {
    fn as_io_error(&self) -> Option<&io::Error> {
        Some(self)
    }
}

impl<T> ErrorExt for io::Result<T> {
    fn as_io_error(&self) -> Option<&io::Error> {
        self.as_ref().err()
    }
}

impl<T, B> ErrorExt for BufResult<T, B> {
    fn as_io_error(&self) -> Option<&io::Error> {
        self.0.as_io_error()
    }
}