io_uring_owner/
lib.rs

1#![warn(
2    clippy::unwrap_used,
3    missing_docs,
4    rust_2018_idioms,
5    unused_lifetimes,
6    unused_qualifications
7)]
8#![doc = include_str!("../README.md")]
9
10//! io_uring Owner types
11
12mod error;
13use core::{fmt, fmt::Display};
14pub use error::TakeError;
15
16/// Ownership denotes where the ownership stands in long living records and where it is important to know it's status.
17/// All Accept, EpollCtl and Buffer records have varying dynamics how these
18/// are allocated and re-used and this requires a flexible type given re-allocation
19/// may be expensive in case of buffers whilst desierable just re-create record in
20/// Accept or EpollCtl case.
21#[derive(Clone, Debug, Default, PartialEq)]
22pub enum Owner {
23    /// Record was created (default)
24    #[default]
25    Created,
26    /// Registered (e.g. via ProvideBuffers)
27    Registered,
28    /// User is filling the created buffer
29    Filling,
30    /// Taken is owned intermediately internally before used
31    Taken,
32    /// Record is owned by the Kernel
33    Kernel,
34    /// Record is returned to the user(space)
35    Returned,
36    /// Record is marked for re-use (e.g. expensive allocation)
37    /// Typical user: BufferRec
38    Reusable,
39}
40
41impl Owner {
42    /// Is the Owner in a state that underlying thing can be taken?
43    #[inline]
44    pub fn take(&mut self) -> Result<(), TakeError> {
45        let r = match self {
46            Owner::Created => Ok(()),
47            Owner::Registered => Ok(()),
48            Owner::Filling => Err(TakeError::PendingFilling),
49            Owner::Taken => Err(TakeError::AlreadyTaken),
50            Owner::Kernel => Err(TakeError::KernelOwns),
51            Owner::Returned => Err(TakeError::Returned),
52            Owner::Reusable => Ok(()),
53        };
54        match r {
55            Ok(()) => {
56                *self = Owner::Taken;
57                Ok(())
58            }
59            Err(e) => Err(e),
60        }
61    }
62}
63
64impl Display for Owner {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        match self {
67            Self::Created => write!(f, "Created"),
68            Self::Registered => write!(f, "Registered"),
69            Self::Filling => write!(f, "Filling"),
70            Self::Taken => write!(f, "Taken"),
71            Self::Kernel => write!(f, "Kernel"),
72            Self::Returned => write!(f, "Returned"),
73            Self::Reusable => write!(f, "Reusable"),
74        }
75    }
76}