From e29c0ef2a23f952a1c2420e18b4553e669029d64 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Thu, 23 Feb 2017 22:54:32 -0700 Subject: [PATCH] impl Debug for AioCb and SigevNotify Also, fix style bug in AIO tests --- src/sys/aio.rs | 19 +++++++++++++++++++ src/sys/signal.rs | 29 +++++++++++++++++++++++++++++ test/sys/test_aio.rs | 12 ++++++------ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/sys/aio.rs b/src/sys/aio.rs index f529d7a915..13a03b5dfd 100644 --- a/src/sys/aio.rs +++ b/src/sys/aio.rs @@ -2,6 +2,8 @@ use {Error, Errno, Result}; use std::os::unix::io::RawFd; use libc::{c_void, off_t, size_t}; use libc; +use std::fmt; +use std::fmt::Debug; use std::io::Write; use std::io::stderr; use std::marker::PhantomData; @@ -270,6 +272,23 @@ pub fn lio_listio(mode: LioMode, list: &[&mut AioCb], }).map(drop) } +impl<'a> Debug for AioCb<'a> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("AioCb") + .field("aio_fildes", &self.aiocb.aio_fildes) + .field("aio_offset", &self.aiocb.aio_offset) + .field("aio_buf", &self.aiocb.aio_buf) + .field("aio_nbytes", &self.aiocb.aio_nbytes) + .field("aio_lio_opcode", &self.aiocb.aio_lio_opcode) + .field("aio_reqprio", &self.aiocb.aio_reqprio) + .field("aio_sigevent", &SigEvent::from(&self.aiocb.aio_sigevent)) + .field("mutable", &self.mutable) + .field("in_progress", &self.in_progress) + .field("phantom", &self.phantom) + .finish() + } +} + impl<'a> Drop for AioCb<'a> { /// If the `AioCb` has no remaining state in the kernel, just drop it. /// Otherwise, collect its error and return values, so as not to leak diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 26cf51fddc..78c6f5c066 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -3,6 +3,8 @@ use libc; use {Errno, Error, Result}; +use std::fmt; +use std::fmt::Debug; use std::mem; #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] use std::os::unix::io::RawFd; @@ -505,6 +507,33 @@ impl SigEvent { } } +impl Debug for SigEvent { + #[cfg(any(target_os = "linux", target_os = "freebsd"))] + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("SigEvent") + .field("sigev_notify", &self.sigevent.sigev_notify) + .field("sigev_signo", &self.sigevent.sigev_signo) + .field("sigev_value", &self.sigevent.sigev_value.sival_ptr) + .field("sigev_notify_thread_id", + &self.sigevent.sigev_notify_thread_id) + .finish() + } + + #[cfg(not(any(target_os = "linux", target_os = "freebsd")))] + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("SigEvent") + .field("sigev_notify", &self.sigevent.sigev_notify) + .field("sigev_signo", &self.sigevent.sigev_signo) + .field("sigev_value", &self.sigevent.sigev_value.sival_ptr) + .finish() + } +} + +impl<'a> From<&'a libc::sigevent> for SigEvent { + fn from(sigevent: &libc::sigevent) -> Self { + SigEvent{ sigevent: sigevent.clone() } + } +} #[cfg(test)] mod tests { diff --git a/test/sys/test_aio.rs b/test/sys/test_aio.rs index 9b736c4bf1..7f7a77193e 100644 --- a/test/sys/test_aio.rs +++ b/test/sys/test_aio.rs @@ -193,12 +193,12 @@ fn test_write() { } // XXX: should be sig_atomic_t, but rust's libc doesn't define that yet -static mut signaled: i32 = 0; +static mut SIGNALED: i32 = 0; extern fn sigfunc(_: c_int) { // It's a pity that Rust can't understand that static mutable sig_atomic_t // variables can be safely accessed - unsafe { signaled = 1 }; + unsafe { SIGNALED = 1 }; } // Test an aio operation with completion delivered by a signal @@ -207,7 +207,7 @@ fn test_write_sigev_signal() { let sa = SigAction::new(SigHandler::Handler(sigfunc), SA_RESETHAND, SigSet::empty()); - unsafe {signaled = 0 }; + unsafe {SIGNALED = 0 }; unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap(); const INITIAL: &'static [u8] = b"abcdef123456"; @@ -227,7 +227,7 @@ fn test_write_sigev_signal() { }, LioOpcode::LIO_NOP); aiocb.write().unwrap(); - while unsafe { signaled == 0 } { + while unsafe { SIGNALED == 0 } { thread::sleep(time::Duration::from_millis(10)); } @@ -357,11 +357,11 @@ fn test_lio_listio_signal() { 0, //priority SigevNotify::SigevNone, LioOpcode::LIO_READ); - unsafe {signaled = 0 }; + unsafe {SIGNALED = 0 }; unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap(); let err = lio_listio(LioMode::LIO_NOWAIT, &[&mut wcb, &mut rcb], sigev_notify); err.expect("lio_listio failed"); - while unsafe { signaled == 0 } { + while unsafe { SIGNALED == 0 } { thread::sleep(time::Duration::from_millis(10)); }