Skip to content

Commit 2c417b3

Browse files
committed
Implement epoll_data union
1 parent 933263e commit 2c417b3

File tree

5 files changed

+79
-20
lines changed

5 files changed

+79
-20
lines changed

libc-test/build.rs

-12
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,6 @@ fn test_solarish(target: &str) {
668668

669669
cfg.field_name(move |struct_, field| {
670670
match struct_ {
671-
// rust struct uses raw u64, rather than union
672-
"epoll_event" if field == "u64" => "data.u64".to_string(),
673671
// rust struct was committed with typo for Solaris
674672
"door_arg_t" if field == "dec_num" => "desc_num".to_string(),
675673
"stat" if field.ends_with("_nsec") => {
@@ -893,7 +891,6 @@ fn test_netbsd(target: &str) {
893891
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
894892
s.replace("e_nsec", ".tv_nsec")
895893
}
896-
"u64" if struct_ == "epoll_event" => "data.u64".to_string(),
897894
s => s.to_string(),
898895
}
899896
});
@@ -1094,7 +1091,6 @@ fn test_dragonflybsd(target: &str) {
10941091
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
10951092
s.replace("e_nsec", ".tv_nsec")
10961093
}
1097-
"u64" if struct_ == "epoll_event" => "data.u64".to_string(),
10981094
// Field is named `type` in C but that is a Rust keyword,
10991095
// so these fields are translated to `type_` in the bindings.
11001096
"type_" if struct_ == "rtprio" => "type".to_string(),
@@ -1424,8 +1420,6 @@ fn test_android(target: &str) {
14241420
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
14251421
s.to_string()
14261422
}
1427-
// FIXME: appears that `epoll_event.data` is an union
1428-
"u64" if struct_ == "epoll_event" => "data.u64".to_string(),
14291423
s => s.to_string(),
14301424
}
14311425
});
@@ -1960,8 +1954,6 @@ fn test_emscripten(target: &str) {
19601954
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
19611955
s.replace("e_nsec", ".tv_nsec")
19621956
}
1963-
// FIXME: appears that `epoll_event.data` is an union
1964-
"u64" if struct_ == "epoll_event" => "data.u64".to_string(),
19651957
s => s.to_string(),
19661958
}
19671959
});
@@ -2389,10 +2381,6 @@ fn test_linux(target: &str) {
23892381
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
23902382
s.replace("e_nsec", ".tv_nsec")
23912383
}
2392-
// FIXME: epoll_event.data is actuall a union in C, but in Rust
2393-
// it is only a u64 because we only expose one field
2394-
// http://man7.org/linux/man-pages/man2/epoll_wait.2.html
2395-
"u64" if struct_ == "epoll_event" => "data.u64".to_string(),
23962384
// The following structs have a field called `type` in C,
23972385
// but `type` is a Rust keyword, so these fields are translated
23982386
// to `type_` in Rust.

src/fuchsia/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,13 @@ s! {
409409

410410
pub struct epoll_event {
411411
pub events: u32,
412+
pub data: epoll_data,
413+
}
414+
415+
pub union epoll_data {
416+
pub ptr: *mut ::c_void,
417+
pub fd: ::c_int,
418+
pub u32: u32,
412419
pub u64: u64,
413420
}
414421

src/unix/linux_like/mod.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ s_no_extra_traits! {
214214
repr(packed))]
215215
pub struct epoll_event {
216216
pub events: u32,
217+
pub data: epoll_data,
218+
}
219+
220+
pub union epoll_data {
221+
pub ptr: *mut ::c_void,
222+
pub fd: ::c_int,
223+
pub u32: u32,
217224
pub u64: u64,
218225
}
219226

@@ -264,25 +271,46 @@ cfg_if! {
264271
impl PartialEq for epoll_event {
265272
fn eq(&self, other: &epoll_event) -> bool {
266273
self.events == other.events
267-
&& self.u64 == other.u64
274+
&& unsafe { self.data.u64 == other.data.u64 }
268275
}
269276
}
270277
impl Eq for epoll_event {}
271278
impl ::fmt::Debug for epoll_event {
272279
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
273280
let events = self.events;
274-
let u64 = self.u64;
281+
let data = self.data;
275282
f.debug_struct("epoll_event")
276283
.field("events", &events)
277-
.field("u64", &u64)
284+
.field("data", &data)
278285
.finish()
279286
}
280287
}
281288
impl ::hash::Hash for epoll_event {
282289
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
283290
let events = self.events;
284-
let u64 = self.u64;
291+
let data = self.data;
285292
events.hash(state);
293+
data.hash(state);
294+
}
295+
}
296+
297+
impl PartialEq for epoll_data {
298+
fn eq(&self, other: &epoll_data) -> bool {
299+
unsafe { self.u64 == other.u64 }
300+
}
301+
}
302+
impl Eq for epoll_data {}
303+
impl ::fmt::Debug for epoll_data {
304+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
305+
let u64 = unsafe { self.u64 };
306+
f.debug_struct("epoll_data")
307+
.field("data", &u64)
308+
.finish()
309+
}
310+
}
311+
impl ::hash::Hash for epoll_data {
312+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
313+
let u64 = unsafe { self.u64 };
286314
u64.hash(state);
287315
}
288316
}

src/unix/solarish/mod.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ s_no_extra_traits! {
429429
), repr(packed))]
430430
pub struct epoll_event {
431431
pub events: u32,
432+
pub data: epoll_data,
433+
}
434+
435+
pub union epoll_data {
436+
pub ptr: *mut ::c_void,
437+
pub fd: ::c_int,
438+
pub u32: u32,
432439
pub u64: u64,
433440
}
434441

@@ -563,25 +570,46 @@ cfg_if! {
563570
impl PartialEq for epoll_event {
564571
fn eq(&self, other: &epoll_event) -> bool {
565572
self.events == other.events
566-
&& self.u64 == other.u64
573+
&& unsafe { self.data.u64 == other.data.u64 }
567574
}
568575
}
569576
impl Eq for epoll_event {}
570577
impl ::fmt::Debug for epoll_event {
571578
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
572579
let events = self.events;
573-
let u64 = self.u64;
580+
let data = self.data;
574581
f.debug_struct("epoll_event")
575582
.field("events", &events)
576-
.field("u64", &u64)
583+
.field("data", &data)
577584
.finish()
578585
}
579586
}
580587
impl ::hash::Hash for epoll_event {
581588
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
582589
let events = self.events;
583-
let u64 = self.u64;
590+
let data = self.data;
584591
events.hash(state);
592+
data.hash(state);
593+
}
594+
}
595+
596+
impl PartialEq for epoll_data {
597+
fn eq(&self, other: &epoll_data) -> bool {
598+
unsafe { self.u64 == other.u64 }
599+
}
600+
}
601+
impl Eq for epoll_data {}
602+
impl ::fmt::Debug for epoll_data {
603+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
604+
let u64 = unsafe { self.u64 };
605+
f.debug_struct("epoll_data")
606+
.field("data", &u64)
607+
.finish()
608+
}
609+
}
610+
impl ::hash::Hash for epoll_data {
611+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
612+
let u64 = unsafe { self.u64 };
585613
u64.hash(state);
586614
}
587615
}

src/unix/uclibc/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ s_no_extra_traits! {
362362
#[allow(missing_debug_implementations)]
363363
pub struct epoll_event {
364364
pub events: u32,
365+
pub data: epoll_data,
366+
}
367+
368+
#[allow(missing_debug_implementations)]
369+
pub union epoll_data {
370+
pub ptr: *mut ::c_void,
371+
pub fd: ::c_int,
372+
pub u32: u32,
365373
pub u64: u64,
366374
}
367375

0 commit comments

Comments
 (0)