Skip to content

Commit

Permalink
Remove async-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
toblux committed Dec 6, 2024
1 parent 9a311c0 commit e1b823b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"]
14 changes: 5 additions & 9 deletions src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,31 +108,28 @@ pub struct Socket<P: AsRef<Path>> {
}

/// 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<Self::Stream>;
fn connect(&self) -> impl std::future::Future<Output = io::Result<Self::Stream>>;
}

#[async_trait(?Send)]
impl<A: ToSocketAddrs> TransportProtocol for Tcp<A> {
type Stream = TcpStream;

async fn connect(&self) -> io::Result<Self::Stream> {
TcpStream::connect(&self.host_address).await
fn connect(&self) -> impl std::future::Future<Output = io::Result<Self::Stream>> {
TcpStream::connect(&self.host_address)
}
}

#[async_trait(?Send)]
#[cfg(unix)]
impl<P: AsRef<Path>> TransportProtocol for Socket<P> {
type Stream = UnixStream;

async fn connect(&self) -> io::Result<Self::Stream> {
UnixStream::connect(&self.socket_path).await
fn connect(&self) -> impl std::future::Future<Output = io::Result<Self::Stream>> {
UnixStream::connect(&self.socket_path)
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/tokio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use std::path::Path;
use tokio::{
fs::File,
Expand Down Expand Up @@ -112,31 +111,28 @@ pub struct Socket<P: AsRef<Path>> {
}

/// 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<Self::Stream>;
fn connect(&self) -> impl std::future::Future<Output = io::Result<Self::Stream>>;
}

#[async_trait(?Send)]
impl<A: ToSocketAddrs> TransportProtocol for Tcp<A> {
type Stream = TcpStream;

async fn connect(&self) -> io::Result<Self::Stream> {
TcpStream::connect(&self.host_address).await
fn connect(&self) -> impl std::future::Future<Output = io::Result<Self::Stream>> {
TcpStream::connect(&self.host_address)
}
}

#[async_trait(?Send)]
#[cfg(unix)]
impl<P: AsRef<Path>> TransportProtocol for Socket<P> {
type Stream = UnixStream;

async fn connect(&self) -> io::Result<Self::Stream> {
UnixStream::connect(&self.socket_path).await
fn connect(&self) -> impl std::future::Future<Output = io::Result<Self::Stream>> {
UnixStream::connect(&self.socket_path)
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/clamav_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: Send + Sync>(_t: T) {}

mod lib_tests {
use super::*;

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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_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")]
Expand Down

0 comments on commit e1b823b

Please sign in to comment.