pub struct Event {
pub key: usize,
pub readable: bool,
pub writable: bool,
/* private fields */
}
Expand description
Indicates that a file descriptor or socket can read or write without blocking.
Fields§
§key: usize
Key identifying the file descriptor or socket.
readable: bool
Can it do a read operation without blocking?
writable: bool
Can it do a write operation without blocking?
Implementations§
source§impl Event
impl Event
sourcepub const fn all(key: usize) -> Event
pub const fn all(key: usize) -> Event
All kinds of events (readable and writable).
Equivalent to: Event::new(key, true, true)
sourcepub const fn readable(key: usize) -> Event
pub const fn readable(key: usize) -> Event
Only the readable event.
Equivalent to: Event::new(key, true, false)
sourcepub const fn writable(key: usize) -> Event
pub const fn writable(key: usize) -> Event
Only the writable event.
Equivalent to: Event::new(key, false, true)
sourcepub fn set_interrupt(&mut self, active: bool)
pub fn set_interrupt(&mut self, active: bool)
Add interruption events to this interest.
This usually indicates that the file descriptor or socket has been closed. It corresponds
to the EPOLLHUP
and POLLHUP
events.
Interruption events are only supported on the following platforms:
epoll
poll
- IOCP
- Event Ports
On other platforms, this function is a no-op.
sourcepub fn with_interrupt(self) -> Self
pub fn with_interrupt(self) -> Self
Add interruption events to this interest.
This usually indicates that the file descriptor or socket has been closed. It corresponds
to the EPOLLHUP
and POLLHUP
events.
Interruption events are only supported on the following platforms:
epoll
poll
- IOCP
- Event Ports
On other platforms, this function is a no-op.
sourcepub fn set_priority(&mut self, active: bool)
pub fn set_priority(&mut self, active: bool)
Add priority events to this interest.
This indicates that there is urgent data to read. It corresponds to the EPOLLPRI
and
POLLPRI
events.
Priority events are only supported on the following platforms:
epoll
poll
- IOCP
- Event Ports
On other platforms, this function is a no-op.
sourcepub fn with_priority(self) -> Self
pub fn with_priority(self) -> Self
Add priority events to this interest.
This indicates that there is urgent data to read. It corresponds to the EPOLLPRI
and
POLLPRI
events.
Priority events are only supported on the following platforms:
epoll
poll
- IOCP
- Event Ports
On other platforms, this function is a no-op.
sourcepub fn is_interrupt(&self) -> bool
pub fn is_interrupt(&self) -> bool
Tell if this event is the result of an interrupt notification.
This usually indicates that the file descriptor or socket has been closed. It corresponds
to the EPOLLHUP
and POLLHUP
events.
Interruption events are only supported on the following platforms:
epoll
poll
- IOCP
- Event Ports
On other platforms, this always returns false
.
sourcepub fn is_priority(&self) -> bool
pub fn is_priority(&self) -> bool
Tell if this event is the result of a priority notification.
This indicates that there is urgent data to read. It corresponds to the EPOLLPRI
and
POLLPRI
events.
Priority events are only supported on the following platforms:
epoll
poll
- IOCP
- Event Ports
On other platforms, this always returns false
.
sourcepub fn is_connect_failed(&self) -> Option<bool>
pub fn is_connect_failed(&self) -> Option<bool>
Tells if this event is the result of a connection failure.
This function checks if a TCP connection has failed. It corresponds to the EPOLLERR
or EPOLLHUP
event in Linux
and CONNECT_FAILED
event in Windows IOCP.
Examples
use std::{io, net};
// Assuming polling and socket2 are included as dependencies in Cargo.toml
use polling::Event;
use socket2::Type;
fn main() -> io::Result<()> {
let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
let poller = polling::Poller::new()?;
unsafe {
poller.add(&socket, Event::new(0, true, true))?;
}
let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 8080);
socket.set_nonblocking(true)?;
let _ = socket.connect(&addr.into());
let mut events = polling::Events::new();
events.clear();
poller.wait(&mut events, None)?;
let event = events.iter().next();
let event = match event {
Some(event) => event,
None => {
println!("no event");
return Ok(());
},
};
println!("event: {:?}", event);
if event
.is_connect_failed()
.unwrap_or_default()
{
println!("connect failed");
}
Ok(())
}
Returns
Returns Some(true)
if the connection has failed, Some(false)
if the connection has not failed,
or None
if the platform does not support detecting this condition.
sourcepub fn clear_extra(&mut self)
pub fn clear_extra(&mut self)
Remove any extra information from this event.
sourcepub fn with_no_extra(self) -> Self
pub fn with_no_extra(self) -> Self
Get a version of this event with no extra information.
This is useful for comparing events with ==
.