From e968c6c331cc08860edccea46ca9a26692ce365c Mon Sep 17 00:00:00 2001 From: Thorsten Blum Date: Fri, 6 Dec 2024 23:53:32 +0100 Subject: [PATCH] Remove async-trait --- Cargo.toml | 5 ++--- src/async_std.rs | 14 +++++--------- src/tokio.rs | 14 +++++--------- tests/clamav_client.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe325e1..9012763 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ exclude = ["clamd", ".github"] tokio = { version = "1.34.0", default-features = false, features = ["fs", "io-util", "net"], optional = true } tokio-stream = { version = "0.1.14", default-features = false, optional = true } async-std = { version = "1.12.0", optional = true } -async-trait = { version = "0.1.77", optional = true } bytes = { version = "1", optional = true } [dev-dependencies] @@ -26,9 +25,9 @@ tokio-util = { version = "0.7.10", features = ["io"] } async-std = { version = "1.12.0", features = ["attributes"] } [features] -tokio = ["dep:async-trait", "dep:tokio"] +tokio = ["dep:tokio"] tokio-stream = ["tokio", "dep:tokio-stream", "dep:bytes"] -async-std = ["dep:async-trait", "dep:async-std", "dep:bytes"] +async-std = ["dep:async-std", "dep:bytes"] [package.metadata.docs.rs] features = ["tokio", "tokio-stream", "async-std"] diff --git a/src/async_std.rs b/src/async_std.rs index b22fd9a..20df9ae 100644 --- a/src/async_std.rs +++ b/src/async_std.rs @@ -5,7 +5,6 @@ use async_std::{ path::Path, stream::{Stream, StreamExt}, }; -use async_trait::async_trait; #[cfg(unix)] use async_std::os::unix::net::UnixStream; @@ -109,31 +108,28 @@ pub struct Socket> { } /// The communication protocol to use -#[async_trait(?Send)] pub trait TransportProtocol { /// Bidirectional stream type Stream: ReadExt + WriteExt + Unpin; /// Converts the protocol instance into the corresponding stream - async fn connect(&self) -> io::Result; + fn connect(&self) -> impl std::future::Future>; } -#[async_trait(?Send)] impl TransportProtocol for Tcp { type Stream = TcpStream; - async fn connect(&self) -> io::Result { - TcpStream::connect(&self.host_address).await + fn connect(&self) -> impl std::future::Future> { + TcpStream::connect(&self.host_address) } } -#[async_trait(?Send)] #[cfg(unix)] impl> TransportProtocol for Socket

{ type Stream = UnixStream; - async fn connect(&self) -> io::Result { - UnixStream::connect(&self.socket_path).await + fn connect(&self) -> impl std::future::Future> { + UnixStream::connect(&self.socket_path) } } diff --git a/src/tokio.rs b/src/tokio.rs index 5355d9d..27be701 100644 --- a/src/tokio.rs +++ b/src/tokio.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use std::path::Path; use tokio::{ fs::File, @@ -112,31 +111,28 @@ pub struct Socket> { } /// The communication protocol to use -#[async_trait(?Send)] pub trait TransportProtocol { /// Bidirectional stream type Stream: AsyncRead + AsyncWrite + Unpin; /// Converts the protocol instance into the corresponding stream - async fn connect(&self) -> io::Result; + fn connect(&self) -> impl std::future::Future>; } -#[async_trait(?Send)] impl TransportProtocol for Tcp { type Stream = TcpStream; - async fn connect(&self) -> io::Result { - TcpStream::connect(&self.host_address).await + fn connect(&self) -> impl std::future::Future> { + TcpStream::connect(&self.host_address) } } -#[async_trait(?Send)] #[cfg(unix)] impl> TransportProtocol for Socket

{ type Stream = UnixStream; - async fn connect(&self) -> io::Result { - UnixStream::connect(&self.socket_path).await + fn connect(&self) -> impl std::future::Future> { + UnixStream::connect(&self.socket_path) } } diff --git a/tests/clamav_client.rs b/tests/clamav_client.rs index c9928c3..bbcb829 100644 --- a/tests/clamav_client.rs +++ b/tests/clamav_client.rs @@ -14,6 +14,8 @@ const OK_RESPONSE: &[u8] = b"stream: OK\0"; const OVERSIZED_TEST_FILE_PATH: &str = "tests/data/stream-max-length-test-file.bin"; const SIZE_LIMIT_EXCEEDED_ERROR_RESPONSE: &[u8] = b"INSTREAM size limit exceeded. ERROR\0"; +fn assert_implements_send_sync(_t: T) {} + mod lib_tests { use super::*; @@ -345,6 +347,20 @@ mod tokio_tests { assert_eq!(&response, SIZE_LIMIT_EXCEEDED_ERROR_RESPONSE); assert_eq!(clamav_client::clean(&response), Ok(false)); } + + #[tokio::test] + async fn async_tokio_implements_send_sync_trait() { + trait _AssertSendSync: Send + Sync {} + impl _AssertSendSync for clamav_client::tokio::Tcp<&str> {} + impl _AssertSendSync for clamav_client::tokio::Socket<&str> {} + + assert_implements_send_sync(clamav_client::tokio::scan_buffer(&[], CLAMD_HOST_TCP, None)); + assert_implements_send_sync(clamav_client::tokio::scan_buffer( + &[], + CLAMD_HOST_SOCKET, + None, + )); + } } #[cfg(feature = "tokio-stream")] @@ -643,6 +659,24 @@ mod async_std_tests { assert_eq!(&response, SIZE_LIMIT_EXCEEDED_ERROR_RESPONSE); assert_eq!(clamav_client::clean(&response), Ok(false)); } + + #[async_std::test] + async fn async_std_implements_send_sync_trait() { + trait _AssertSendSync: Send + Sync {} + impl _AssertSendSync for clamav_client::async_std::Tcp<&str> {} + impl _AssertSendSync for clamav_client::async_std::Socket<&str> {} + + assert_implements_send_sync(clamav_client::async_std::scan_buffer( + &[], + CLAMD_HOST_TCP, + None, + )); + assert_implements_send_sync(clamav_client::async_std::scan_buffer( + &[], + CLAMD_HOST_SOCKET, + None, + )); + } } #[cfg(feature = "async-std")]