Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove async-trait #14

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clamav-client"
version = "1.0.1"
version = "2.0.0"
edition = "2021"
rust-version = "1.63.0"
authors = ["Thorsten Blum <[email protected]>"]
Expand All @@ -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"]
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
clamav-client = "1.0.1"
clamav-client = "2.0.0"
```

To use the `async` functions in `clamav_client::tokio`, add this to your `Cargo.toml`:

```toml
[dependencies]
clamav-client = { version = "1.0.1", features = ["tokio"] }
clamav-client = { version = "2.0.0", features = ["tokio"] }
```

To scan Tokio streams, enable the `tokio-stream` feature instead and add this to your `Cargo.toml`:

```toml
[dependencies]
clamav-client = { version = "1.0.1", features = ["tokio-stream"] }
clamav-client = { version = "2.0.0", features = ["tokio-stream"] }
```

Support for `async-std` is also available by enabling the `async-std` feature:

```toml
[dependencies]
clamav-client = { version = "1.0.1", features = ["async-std"] }
clamav-client = { version = "2.0.0", features = ["async-std"] }
```

## Migrations
Expand Down Expand Up @@ -228,3 +228,4 @@ Contributions are welcome!
- [Paul Makles](https://github.com/insertish)
- [Sean Clarke](https://github.com/SeanEClarke)
- [Kanji Tanaka](https://github.com/kaicoh)
- [Raui Ghazaleh](https://github.com/raui100)
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
42 changes: 42 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,24 @@ 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> {}
assert_implements_send_sync(clamav_client::tokio::scan_buffer(&[], CLAMD_HOST_TCP, None));

#[cfg(unix)]
{
impl _AssertSendSync for clamav_client::tokio::Socket<&str> {}
assert_implements_send_sync(clamav_client::tokio::scan_buffer(
&[],
CLAMD_HOST_SOCKET,
None,
));
}
}
}

#[cfg(feature = "tokio-stream")]
Expand Down Expand Up @@ -643,6 +663,28 @@ 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> {}
assert_implements_send_sync(clamav_client::async_std::scan_buffer(
&[],
CLAMD_HOST_TCP,
None,
));

#[cfg(unix)]
{
impl _AssertSendSync for clamav_client::async_std::Socket<&str> {}
assert_implements_send_sync(clamav_client::async_std::scan_buffer(
&[],
CLAMD_HOST_SOCKET,
None,
));
}
}
}

#[cfg(feature = "async-std")]
Expand Down
Loading