mz_ore/netio/
async_ready.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use async_trait::async_trait;
17use tokio::io::{self, Interest, Ready};
18use tokio::net::TcpStream;
19use tokio_openssl::SslStream;
20
21/// Asynchronous IO readiness.
22///
23/// Like [`tokio::io::AsyncRead`] or [`tokio::io::AsyncWrite`], but for
24/// readiness events.
25#[async_trait]
26pub trait AsyncReady {
27    /// Checks for IO readiness.
28    ///
29    /// See [`TcpStream::ready`] for details.
30    async fn ready(&self, interest: Interest) -> io::Result<Ready>;
31}
32
33#[async_trait]
34impl AsyncReady for TcpStream {
35    async fn ready(&self, interest: Interest) -> io::Result<Ready> {
36        self.ready(interest).await
37    }
38}
39
40#[async_trait]
41impl<S> AsyncReady for SslStream<S>
42where
43    S: AsyncReady + Sync,
44{
45    async fn ready(&self, interest: Interest) -> io::Result<Ready> {
46        self.get_ref().ready(interest).await
47    }
48}