Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more accessors for AioCb #773

Merged
merged 1 commit into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added socket option variant that enables the timestamp socket
control message: `nix::sys::socket::sockopt::ReceiveTimestamp`
([#663](https://github.com/nix-rust/nix/pull/663))
- Added more accessor methods for `AioCb`
([#773](https://github.com/nix-rust/nix/pull/773))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
42 changes: 42 additions & 0 deletions src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ pub struct AioCb<'a> {
}

impl<'a> AioCb<'a> {
/// Returns the underlying file descriptor associated with the `AioCb`
pub fn fd(&self) -> RawFd {
self.aiocb.aio_fildes
}

/// Constructs a new `AioCb` with no associated buffer.
///
/// The resulting `AioCb` structure is suitable for use with `AioCb::fsync`.
Expand Down Expand Up @@ -239,6 +244,38 @@ impl<'a> AioCb<'a> {
})
}

/// Returns the `aiocb`'s `LioOpcode` field
///
/// If the value cannot be represented as an `LioOpcode`, returns `None`
/// instead.
pub fn lio_opcode(&self) -> Option<LioOpcode> {
match self.aiocb.aio_lio_opcode {
libc::LIO_READ => Some(LioOpcode::LIO_READ),
libc::LIO_WRITE => Some(LioOpcode::LIO_WRITE),
libc::LIO_NOP => Some(LioOpcode::LIO_NOP),
_ => None
}
}

/// Returns the requested length of the aio operation in bytes
///
/// This method returns the *requested* length of the operation. To get the
/// number of bytes actually read or written by a completed operation, use
/// `aio_return` instead.
pub fn nbytes(&self) -> usize {
self.aiocb.aio_nbytes
}

/// Returns the file offset stored in the `AioCb`
pub fn offset(&self) -> off_t {
self.aiocb.aio_offset
}

/// Returns the priority of the `AioCb`
pub fn priority(&self) -> libc::c_int {
self.aiocb.aio_reqprio
}

/// Asynchronously reads from a file descriptor into a buffer
pub fn read(&mut self) -> Result<()> {
assert!(self.mutable, "Can't read into an immutable buffer");
Expand All @@ -250,6 +287,11 @@ impl<'a> AioCb<'a> {
})
}

/// Returns the `SigEvent` stored in the `AioCb`
pub fn sigevent(&self) -> SigEvent {
SigEvent::from(&self.aiocb.aio_sigevent)
}

/// Retrieve return status of an asynchronous operation. Should only be
/// called once for each `AioCb`, after `AioCb::error` indicates that it has
/// completed. The result is the same as for `read`, `write`, of `fsync`.
Expand Down
22 changes: 22 additions & 0 deletions test/sys/test_aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ fn poll_aio(mut aiocb: &mut AioCb) -> Result<()> {
}
}

#[test]
fn test_accessors() {
let mut rbuf = vec![0; 4];
let aiocb = AioCb::from_mut_slice( 1001,
2, //offset
&mut rbuf,
42, //priority
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 99
},
LioOpcode::LIO_NOP);
assert_eq!(1001, aiocb.fd());
assert_eq!(Some(LioOpcode::LIO_NOP), aiocb.lio_opcode());
assert_eq!(4, aiocb.nbytes());
assert_eq!(2, aiocb.offset());
assert_eq!(42, aiocb.priority());
let sev = aiocb.sigevent().sigevent();
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
}

// Tests AioCb.cancel. We aren't trying to test the OS's implementation, only our
// bindings. So it's sufficient to check that AioCb.cancel returned any
// AioCancelStat value.
Expand Down