-
Notifications
You must be signed in to change notification settings - Fork 25
/
poll.rs
107 lines (95 loc) · 2.8 KB
/
poll.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
//! Serialization support for poll-related enums and structs.
use core::fmt;
use serde::Deserialize;
use serde::Serialize;
use crate::Displayable;
use crate::FromToRaw;
use crate::MemoryAccess;
/// A serializable version of `libc::pollfd`.
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug, Default)]
#[repr(C)]
#[allow(missing_docs)]
pub struct PollFd {
pub fd: libc::c_int,
pub events: PollFlags,
pub revents: PollFlags,
}
impl From<PollFd> for libc::pollfd {
fn from(pollfd: PollFd) -> libc::pollfd {
libc::pollfd {
fd: pollfd.fd,
events: pollfd.events.bits(),
revents: pollfd.revents.bits(),
}
}
}
impl From<libc::pollfd> for PollFd {
fn from(pollfd: libc::pollfd) -> Self {
Self {
fd: pollfd.fd,
events: PollFlags::from_bits_retain(pollfd.events),
revents: PollFlags::from_bits_retain(pollfd.revents),
}
}
}
impl Displayable for PollFd {
fn fmt<M: MemoryAccess>(
&self,
_memory: &M,
_outputs: bool,
f: &mut fmt::Formatter,
) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
bitflags::bitflags! {
/// Flags for [`PollFd`].
#[derive(Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct PollFlags: libc::c_short {
/// There is data to read.
const POLLIN = libc::POLLIN;
/// There is some exceptional condition on the file descriptor.
const POLLPRI = libc::POLLPRI;
/// Writing is now possible.
const POLLOUT = libc::POLLOUT;
/// Equivalent to [`POLLIN`].
const POLLRDNORM = libc::POLLRDNORM;
/// Equivalent to [`POLLOUT`].
const POLLWRNORM = libc::POLLWRNORM;
/// Priority band can be read (generally unused on Linux).
const POLLRDBAND = libc::POLLRDBAND;
/// Priority data may be written.
const POLLWRBAND = libc::POLLWRBAND;
/// Error condition.
const POLLERR = libc::POLLERR;
/// Hang up.
const POLLHUP = libc::POLLHUP;
/// Invalid request.
const POLLNVAL = libc::POLLNVAL;
}
}
impl FromToRaw for PollFlags {
fn from_raw(raw: usize) -> Self {
Self::from_bits_truncate(raw as libc::c_short)
}
fn into_raw(self) -> usize {
self.bits() as usize
}
}
impl Displayable for PollFlags {
fn fmt<M: MemoryAccess>(
&self,
_memory: &M,
_outputs: bool,
f: &mut fmt::Formatter,
) -> fmt::Result {
fmt::Display::fmt(&self.bits(), f)
}
}