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>
👎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>
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.
Sourcepub 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.
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 ==
.
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 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,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)