linux_aio_tokio/
commands.rs

1use crate::flags::{ReadFlags, WriteFlags};
2use crate::locked_buf::LifetimeExtender;
3use crate::{aio, LockedBuf};
4
5/// Raw AIO command
6#[derive(Debug)]
7pub enum RawCommand<'a> {
8    /// Read
9    Pread {
10        /// Offset
11        offset: u64,
12        /// Buffer
13        buffer: &'a mut LockedBuf,
14        /// Read flags
15        flags: ReadFlags,
16        /// Optional len
17        len: u64,
18    },
19
20    /// Write
21    Pwrite {
22        /// Offset
23        offset: u64,
24        /// Buffer
25        buffer: &'a LockedBuf,
26
27        /// Write flags
28        flags: WriteFlags,
29        /// Optional len
30        len: u64,
31    },
32
33    /// Sync data only
34    Fdsync,
35
36    /// Sync data and metadata
37    Fsync,
38}
39
40impl<'a> RawCommand<'a> {
41    pub(crate) fn opcode(&self) -> u32 {
42        use RawCommand::*;
43
44        match self {
45            Pread { .. } => aio::IOCB_CMD_PREAD,
46            Pwrite { .. } => aio::IOCB_CMD_PWRITE,
47            Fdsync => aio::IOCB_CMD_FDSYNC,
48            Fsync => aio::IOCB_CMD_FSYNC,
49        }
50    }
51
52    pub(crate) fn offset(&self) -> Option<u64> {
53        use RawCommand::*;
54
55        match *self {
56            Pread { offset, .. } => Some(offset),
57            Pwrite { offset, .. } => Some(offset),
58            Fdsync => None,
59            Fsync => None,
60        }
61    }
62
63    pub(crate) fn len(&self) -> Option<u64> {
64        use RawCommand::*;
65
66        match *self {
67            Pread { len, .. } => Some(len),
68            Pwrite { len, .. } => Some(len),
69            Fdsync => None,
70            Fsync => None,
71        }
72    }
73
74    pub(crate) fn buffer_addr(&self) -> Option<(u64, u64)> {
75        use RawCommand::*;
76
77        match self {
78            Pread { buffer, .. } => Some(buffer.aio_addr_and_len()),
79            Pwrite { buffer, .. } => Some(buffer.aio_addr_and_len()),
80            Fdsync => None,
81            Fsync => None,
82        }
83    }
84
85    pub(crate) fn flags(&self) -> Option<u32> {
86        use RawCommand::*;
87
88        match self {
89            Pread { flags, .. } => Some(flags.bits() as _),
90            Pwrite { flags, .. } => Some(flags.bits() as _),
91            Fdsync => None,
92            Fsync => None,
93        }
94    }
95
96    pub(crate) fn buffer_lifetime_extender(&self) -> Option<LifetimeExtender> {
97        use RawCommand::*;
98
99        match self {
100            Pread { buffer, .. } => Some(buffer.lifetime_extender()),
101            Pwrite { buffer, .. } => Some(buffer.lifetime_extender()),
102            Fdsync => None,
103            Fsync => None,
104        }
105    }
106}