Struct Event
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: usizeKey identifying the file descriptor or socket.
readable: boolCan it do a read operation without blocking?
writable: boolCan it do a write operation without blocking?
Implementations§
§impl Event
impl Event
pub 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)
pub const fn readable(key: usize) -> Event
pub const fn readable(key: usize) -> Event
Only the readable event.
Equivalent to: Event::new(key, true, false)
pub const fn writable(key: usize) -> Event
pub const fn writable(key: usize) -> Event
Only the writable event.
Equivalent to: Event::new(key, false, true)
pub 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:
epollpoll- IOCP
- Event Ports
On other platforms, this function is a no-op.
pub fn set_rd_interrupt(&mut self, active: bool)
pub fn set_rd_interrupt(&mut self, active: bool)
Add rd interruption events to this interest.
This usually indicates that the file descriptor or socket has been closed. It corresponds
to the EPOLLRDHUP and POLLRDHUP events.
Interruption events are only supported on the following platforms:
epollpoll- IOCP
- Event Ports
On other platforms, this function is a no-op.
pub fn with_interrupt(self) -> Event
pub fn with_interrupt(self) -> Event
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:
epollpoll- IOCP
- Event Ports
On other platforms, this function is a no-op.
pub fn with_rd_interrupt(self) -> Event
pub fn with_rd_interrupt(self) -> Event
Add rd interruption events to this interest.
This usually indicates that the file descriptor or socket has been closed. It corresponds
to the EPOLLRDHUP and POLLRDHUP events.
Interruption events are only supported on the following platforms:
epollpoll- IOCP
- Event Ports
On other platforms, this function is a no-op.
pub 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:
epollpoll- IOCP
- Event Ports
On other platforms, this function is a no-op.
pub fn with_priority(self) -> Event
pub fn with_priority(self) -> Event
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:
epollpoll- IOCP
- Event Ports
On other platforms, this function is a no-op.
pub 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:
epollpoll- IOCP
- Event Ports
On other platforms, this always returns false.
pub fn is_rd_interrupt(&self) -> bool
pub fn is_rd_interrupt(&self) -> bool
Tell if this event is the result of an rd interrupt notification.
pub 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:
epollpoll- IOCP
- Event Ports
On other platforms, this always returns false.
pub fn is_connect_failed(&self) -> Option<bool>
👎Deprecated since 3.4.0: use is_err in combination of is_hup instead, see documentation for is_err
pub fn is_connect_failed(&self) -> Option<bool>
use is_err in combination of is_hup instead, see documentation for is_err
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.
pub fn is_err(&self) -> Option<bool>
pub fn is_err(&self) -> Option<bool>
Tells if this event is the result of a connection failure.
This function checks if an error exist, particularly useful in detecting if TCP connection failed. It corresponds to the EPOLLERR event in Linux
and CONNECT_FAILED event in Windows IOCP.
§Caveats
In epoll, a TCP connection failure is indicated by EPOLLERR + EPOLLHUP, though just EPOLLERR is enough to indicate a connection failure.
EPOLLHUP may happen when we haven’t event called connect on the socket, but it is still a valid event to check for.
Returns Some(true) if the connection has failed, Some(false) if there is no error,
or None if the platform does not support detecting this condition.
pub fn clear_extra(&mut self)
pub fn clear_extra(&mut self)
Remove any extra information from this event.
pub fn with_no_extra(self) -> Event
pub fn with_no_extra(self) -> Event
Get a version of this event with no extra information.
This is useful for comparing events with ==.
Trait Implementations§
impl Copy for Event
impl Eq for Event
impl StructuralPartialEq for Event
Auto Trait Implementations§
impl Freeze for Event
impl RefUnwindSafe for Event
impl Send for Event
impl Sync for Event
impl Unpin for Event
impl UnsafeUnpin for Event
impl UnwindSafe for Event
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.