Skip to content

Commit

Permalink
fix #480 and add simple test cases for that.
Browse files Browse the repository at this point in the history
  • Loading branch information
hicqu committed Dec 11, 2016
1 parent 2cfeb57 commit bff660f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/sys/epoll.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {Errno, Result};
use libc::{self, c_int};
use std::os::unix::io::RawFd;
use std::ptr;

bitflags!(
#[repr(C)]
Expand Down Expand Up @@ -57,6 +58,16 @@ impl EpollEvent {
}
}

impl<'a> Into<&'a mut EpollEvent> for Option<&'a mut EpollEvent> {
#[inline]
fn into(self) -> &'a mut EpollEvent {
match self {
Some(epoll_event) => epoll_event,
None => unsafe { &mut *ptr::null_mut::<EpollEvent>() }
}
}
}

#[inline]
pub fn epoll_create() -> Result<RawFd> {
let res = unsafe { libc::epoll_create(1024) };
Expand All @@ -72,9 +83,10 @@ pub fn epoll_create1(flags: EpollCreateFlags) -> Result<RawFd> {
}

#[inline]
pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut EpollEvent) -> Result<()> {
let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, &mut event.event) };

pub fn epoll_ctl<'a, T>(epfd: RawFd, op: EpollOp, fd: RawFd, event: T) -> Result<()>
where T: Into<&'a mut EpollEvent>
{
let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, &mut event.into().event) };
Errno::result(res).map(drop)
}

Expand Down
1 change: 1 addition & 0 deletions test/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mod test_ioctl;
mod test_wait;
mod test_select;
mod test_uio;
mod test_epoll;
20 changes: 20 additions & 0 deletions test/sys/test_epoll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use nix::sys::epoll::{EpollCreateFlags, EpollFlags, EpollOp, EpollEvent};
use nix::sys::epoll::{EPOLLIN, EPOLLERR};
use nix::sys::epoll::{epoll_create1, epoll_ctl};
use nix::{Error, Errno};

#[test]
pub fn test_epoll_errno() {
let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
let result = epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::Sys(Errno::ENOENT));
}

#[test]
pub fn test_epoll_ctl() {
let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
let mut event = EpollEvent::new(EPOLLIN | EPOLLERR, 1);
epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, &mut event).unwrap();
epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None).unwrap();
}

0 comments on commit bff660f

Please sign in to comment.