Skip to content

Commit 2166e71

Browse files
committed
Merge #773
773: Add more accessors for AioCb r=asomers a=asomers
2 parents d3303c2 + 6bb3704 commit 2166e71

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3030
- Added socket option variant that enables the timestamp socket
3131
control message: `nix::sys::socket::sockopt::ReceiveTimestamp`
3232
([#663](https://github.com/nix-rust/nix/pull/663))
33+
- Added more accessor methods for `AioCb`
34+
([#773](https://github.com/nix-rust/nix/pull/773))
3335

3436
### Changed
3537
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/sys/aio.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ pub struct AioCb<'a> {
8484
}
8585

8686
impl<'a> AioCb<'a> {
87+
/// Returns the underlying file descriptor associated with the `AioCb`
88+
pub fn fd(&self) -> RawFd {
89+
self.aiocb.aio_fildes
90+
}
91+
8792
/// Constructs a new `AioCb` with no associated buffer.
8893
///
8994
/// The resulting `AioCb` structure is suitable for use with `AioCb::fsync`.
@@ -239,6 +244,38 @@ impl<'a> AioCb<'a> {
239244
})
240245
}
241246

247+
/// Returns the `aiocb`'s `LioOpcode` field
248+
///
249+
/// If the value cannot be represented as an `LioOpcode`, returns `None`
250+
/// instead.
251+
pub fn lio_opcode(&self) -> Option<LioOpcode> {
252+
match self.aiocb.aio_lio_opcode {
253+
libc::LIO_READ => Some(LioOpcode::LIO_READ),
254+
libc::LIO_WRITE => Some(LioOpcode::LIO_WRITE),
255+
libc::LIO_NOP => Some(LioOpcode::LIO_NOP),
256+
_ => None
257+
}
258+
}
259+
260+
/// Returns the requested length of the aio operation in bytes
261+
///
262+
/// This method returns the *requested* length of the operation. To get the
263+
/// number of bytes actually read or written by a completed operation, use
264+
/// `aio_return` instead.
265+
pub fn nbytes(&self) -> usize {
266+
self.aiocb.aio_nbytes
267+
}
268+
269+
/// Returns the file offset stored in the `AioCb`
270+
pub fn offset(&self) -> off_t {
271+
self.aiocb.aio_offset
272+
}
273+
274+
/// Returns the priority of the `AioCb`
275+
pub fn priority(&self) -> libc::c_int {
276+
self.aiocb.aio_reqprio
277+
}
278+
242279
/// Asynchronously reads from a file descriptor into a buffer
243280
pub fn read(&mut self) -> Result<()> {
244281
assert!(self.mutable, "Can't read into an immutable buffer");
@@ -250,6 +287,11 @@ impl<'a> AioCb<'a> {
250287
})
251288
}
252289

290+
/// Returns the `SigEvent` stored in the `AioCb`
291+
pub fn sigevent(&self) -> SigEvent {
292+
SigEvent::from(&self.aiocb.aio_sigevent)
293+
}
294+
253295
/// Retrieve return status of an asynchronous operation. Should only be
254296
/// called once for each `AioCb`, after `AioCb::error` indicates that it has
255297
/// completed. The result is the same as for `read`, `write`, of `fsync`.

test/sys/test_aio.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ fn poll_aio(aiocb: &mut AioCb) -> Result<()> {
2121
}
2222
}
2323

24+
#[test]
25+
fn test_accessors() {
26+
let mut rbuf = vec![0; 4];
27+
let aiocb = AioCb::from_mut_slice( 1001,
28+
2, //offset
29+
&mut rbuf,
30+
42, //priority
31+
SigevNotify::SigevSignal {
32+
signal: Signal::SIGUSR2,
33+
si_value: 99
34+
},
35+
LioOpcode::LIO_NOP);
36+
assert_eq!(1001, aiocb.fd());
37+
assert_eq!(Some(LioOpcode::LIO_NOP), aiocb.lio_opcode());
38+
assert_eq!(4, aiocb.nbytes());
39+
assert_eq!(2, aiocb.offset());
40+
assert_eq!(42, aiocb.priority());
41+
let sev = aiocb.sigevent().sigevent();
42+
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
43+
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
44+
}
45+
2446
// Tests AioCb.cancel. We aren't trying to test the OS's implementation, only our
2547
// bindings. So it's sufficient to check that AioCb.cancel returned any
2648
// AioCancelStat value.

0 commit comments

Comments
 (0)