Skip to content

Commit e29c0ef

Browse files
committed
impl Debug for AioCb and SigevNotify
Also, fix style bug in AIO tests
1 parent 3c25561 commit e29c0ef

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/sys/aio.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use {Error, Errno, Result};
22
use std::os::unix::io::RawFd;
33
use libc::{c_void, off_t, size_t};
44
use libc;
5+
use std::fmt;
6+
use std::fmt::Debug;
57
use std::io::Write;
68
use std::io::stderr;
79
use std::marker::PhantomData;
@@ -270,6 +272,23 @@ pub fn lio_listio(mode: LioMode, list: &[&mut AioCb],
270272
}).map(drop)
271273
}
272274

275+
impl<'a> Debug for AioCb<'a> {
276+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
277+
fmt.debug_struct("AioCb")
278+
.field("aio_fildes", &self.aiocb.aio_fildes)
279+
.field("aio_offset", &self.aiocb.aio_offset)
280+
.field("aio_buf", &self.aiocb.aio_buf)
281+
.field("aio_nbytes", &self.aiocb.aio_nbytes)
282+
.field("aio_lio_opcode", &self.aiocb.aio_lio_opcode)
283+
.field("aio_reqprio", &self.aiocb.aio_reqprio)
284+
.field("aio_sigevent", &SigEvent::from(&self.aiocb.aio_sigevent))
285+
.field("mutable", &self.mutable)
286+
.field("in_progress", &self.in_progress)
287+
.field("phantom", &self.phantom)
288+
.finish()
289+
}
290+
}
291+
273292
impl<'a> Drop for AioCb<'a> {
274293
/// If the `AioCb` has no remaining state in the kernel, just drop it.
275294
/// Otherwise, collect its error and return values, so as not to leak

src/sys/signal.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use libc;
55
use {Errno, Error, Result};
6+
use std::fmt;
7+
use std::fmt::Debug;
68
use std::mem;
79
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
810
use std::os::unix::io::RawFd;
@@ -505,6 +507,33 @@ impl SigEvent {
505507
}
506508
}
507509

510+
impl Debug for SigEvent {
511+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
512+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
513+
fmt.debug_struct("SigEvent")
514+
.field("sigev_notify", &self.sigevent.sigev_notify)
515+
.field("sigev_signo", &self.sigevent.sigev_signo)
516+
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
517+
.field("sigev_notify_thread_id",
518+
&self.sigevent.sigev_notify_thread_id)
519+
.finish()
520+
}
521+
522+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
523+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
524+
fmt.debug_struct("SigEvent")
525+
.field("sigev_notify", &self.sigevent.sigev_notify)
526+
.field("sigev_signo", &self.sigevent.sigev_signo)
527+
.field("sigev_value", &self.sigevent.sigev_value.sival_ptr)
528+
.finish()
529+
}
530+
}
531+
532+
impl<'a> From<&'a libc::sigevent> for SigEvent {
533+
fn from(sigevent: &libc::sigevent) -> Self {
534+
SigEvent{ sigevent: sigevent.clone() }
535+
}
536+
}
508537

509538
#[cfg(test)]
510539
mod tests {

test/sys/test_aio.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ fn test_write() {
193193
}
194194

195195
// XXX: should be sig_atomic_t, but rust's libc doesn't define that yet
196-
static mut signaled: i32 = 0;
196+
static mut SIGNALED: i32 = 0;
197197

198198
extern fn sigfunc(_: c_int) {
199199
// It's a pity that Rust can't understand that static mutable sig_atomic_t
200200
// variables can be safely accessed
201-
unsafe { signaled = 1 };
201+
unsafe { SIGNALED = 1 };
202202
}
203203

204204
// Test an aio operation with completion delivered by a signal
@@ -207,7 +207,7 @@ fn test_write_sigev_signal() {
207207
let sa = SigAction::new(SigHandler::Handler(sigfunc),
208208
SA_RESETHAND,
209209
SigSet::empty());
210-
unsafe {signaled = 0 };
210+
unsafe {SIGNALED = 0 };
211211
unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap();
212212

213213
const INITIAL: &'static [u8] = b"abcdef123456";
@@ -227,7 +227,7 @@ fn test_write_sigev_signal() {
227227
},
228228
LioOpcode::LIO_NOP);
229229
aiocb.write().unwrap();
230-
while unsafe { signaled == 0 } {
230+
while unsafe { SIGNALED == 0 } {
231231
thread::sleep(time::Duration::from_millis(10));
232232
}
233233

@@ -357,11 +357,11 @@ fn test_lio_listio_signal() {
357357
0, //priority
358358
SigevNotify::SigevNone,
359359
LioOpcode::LIO_READ);
360-
unsafe {signaled = 0 };
360+
unsafe {SIGNALED = 0 };
361361
unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap();
362362
let err = lio_listio(LioMode::LIO_NOWAIT, &[&mut wcb, &mut rcb], sigev_notify);
363363
err.expect("lio_listio failed");
364-
while unsafe { signaled == 0 } {
364+
while unsafe { SIGNALED == 0 } {
365365
thread::sleep(time::Duration::from_millis(10));
366366
}
367367

0 commit comments

Comments
 (0)