From 992a16812258bae5192a997484ada046a88a6425 Mon Sep 17 00:00:00 2001 From: cjw Date: Fri, 7 Oct 2022 22:47:33 +0800 Subject: [PATCH] runtime: remove `Option` around `mio::Events` in io driver (#5078) --- tokio/src/runtime/io/mod.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tokio/src/runtime/io/mod.rs b/tokio/src/runtime/io/mod.rs index aa9447322ef..d264dc4798b 100644 --- a/tokio/src/runtime/io/mod.rs +++ b/tokio/src/runtime/io/mod.rs @@ -27,7 +27,7 @@ pub(crate) struct Driver { tick: u8, /// Reuse the `mio::Events` value across calls to poll. - events: Option, + events: mio::Events, /// Primary slab handle containing the state for each resource registered /// with this driver. @@ -119,7 +119,7 @@ impl Driver { Ok(Driver { tick: 0, - events: Some(mio::Events::with_capacity(1024)), + events: mio::Events::with_capacity(1024), poll, resources: slab, inner: Arc::new(Inner { @@ -173,7 +173,7 @@ impl Driver { self.resources.compact() } - let mut events = self.events.take().expect("i/o driver event store missing"); + let mut events = &mut self.events; // Block waiting for an event to happen, peeling out how many events // happened. @@ -194,27 +194,28 @@ impl Driver { let token = event.token(); if token != TOKEN_WAKEUP { - self.dispatch(token, Ready::from_mio(event)); + Self::dispatch( + &mut self.resources, + self.tick, + token, + Ready::from_mio(event), + ); ready_count += 1; } } self.inner.metrics.incr_ready_count_by(ready_count); - - self.events = Some(events); } - fn dispatch(&mut self, token: mio::Token, ready: Ready) { + fn dispatch(resources: &mut Slab, tick: u8, token: mio::Token, ready: Ready) { let addr = slab::Address::from_usize(ADDRESS.unpack(token.0)); - let resources = &mut self.resources; - let io = match resources.get(addr) { Some(io) => io, None => return, }; - let res = io.set_readiness(Some(token.0), Tick::Set(self.tick), |curr| curr | ready); + let res = io.set_readiness(Some(token.0), Tick::Set(tick), |curr| curr | ready); if res.is_err() { // token no longer valid!