summaryrefslogtreecommitdiff
path: root/src/sys
diff options
context:
space:
mode:
authorqupeng <onlyqupeng@gmail.com>2016-12-08 19:50:49 +0800
committerqupeng <onlyqupeng@gmail.com>2016-12-11 17:49:48 +0800
commitbff660f1ed579b39f70f07a228f9135686242c6b (patch)
tree370b7dd6eba014e31594f694244e06404588ded4 /src/sys
parent2cfeb5758e1c4957acf4a8108a4c0793ef18a937 (diff)
downloadnix-bff660f1ed579b39f70f07a228f9135686242c6b.zip
fix #480 and add simple test cases for that.
r? @fiveop
Diffstat (limited to 'src/sys')
-rw-r--r--src/sys/epoll.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs
index 9774318f..3c68b49f 100644
--- a/src/sys/epoll.rs
+++ b/src/sys/epoll.rs
@@ -1,6 +1,7 @@
use {Errno, Result};
use libc::{self, c_int};
use std::os::unix::io::RawFd;
+use std::ptr;
bitflags!(
#[repr(C)]
@@ -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) };
@@ -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)
}