Create Your Own Event Queue 82 The last method on Poll is the most interesting one. It's the poll function, which will park the current thread and tell the operating system to wake it up when an event has happened on a source we're tracking, or the timeout has elapsed, whichever comes first. We also close the impl Poll block here: ch04/a-epoll/src/poll. rs pub fn poll(&mut self, events: &mut Events, timeout: Option<i32>)-> Result<()> { let fd = self. registry. raw_fd; let timeout = timeout. unwrap_or(-1); let max_events = events. capacity() as i32; let res = unsafe { ffi::epoll_wait(fd, events. as_mut_ptr(), max_events, timeout) }; if res < 0 { return Err(io::Error::last_os_error()); }; unsafe { events. set_len(res as usize) }; Ok(()) } } The first thing we do is to get the raw file descriptor for the event queue and store it in the fd variable. Next is our timeout. If it's Some, we unwrap that value, and if it's None, we set it to-1, which is the value that tells the operating system that we want to block until an event occurs even though that might never happen. At the top of the file, we defined Events as a type alias for Vec<ffi::Event>, so the next thing we do is to get the capacity of that Vec. It's important that we don't rely on Vec::len since that reports how many items we have in the Vec. Vec::capacity reports the space we've allocated and that's what we're after. Next up is the call to ffi::epoll_wait. This call will return successfully if it has a value of 0 or larger, telling us how many events have occurred. Note We would get a value of 0 if a timeout elapses before an event has happened. The last thing we do is to make an unsafe call to events. set_len(res as usize). This function is unsafe since we could potentially set the length so that we would access memory that's not been initialized yet in safe Rust. We know from the guarantee the operating system gives us that the number of events it returns is pointing to valid data in our Vec, so this is safe in our case.