Skip to content

Commit 8f0cab0

Browse files
committed
Fix conflicting async io feature compilation
Previously, enabling `with-tokio` would also activate the `default` feature (`with-futures`), because Cargo includes default features by design. This would lead to a compilation error due to duplicate definitions from both `use` statements. This commit replaces the separate `#[cfg]` blocks with a single `cfg_if` macro. This creates a prioritized selection for the io backend.
1 parent fa1d38e commit 8f0cab0

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ documentation = "https://docs.rs/memcache-async"
1212
homepage = "https://github.com/vavrusa/memcache-async"
1313

1414
[dependencies]
15+
cfg-if = "1.0.1"
1516
futures = { version = "0.3" }
1617
tokio = { version = "1.37", features = ["io-util"] }
1718

src/ascii.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
//! This is a simplified implementation of [rust-memcache](https://github.com/aisk/rust-memcache)
22
//! ported for AsyncRead + AsyncWrite.
33
use core::fmt::Display;
4-
#[cfg(feature = "with-futures")]
5-
use futures::io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
64

7-
#[cfg(feature = "with-tokio")]
8-
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
5+
use cfg_if::cfg_if;
6+
7+
cfg_if! {
8+
// The order here matters. If both features are enabled,
9+
// this one will be chosen, avoiding a conflict.
10+
if #[cfg(feature = "with-tokio")] {
11+
use tokio::io as async_io;
12+
} else if #[cfg(feature = "with-futures")] {
13+
use futures::io as async_io;
14+
} else {
15+
compile_error!("An async I/O feature ('with-tokio' or 'with-futures') must be enabled.");
16+
}
17+
}
18+
19+
use async_io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
920

1021
use std::collections::HashMap;
1122
use std::io::{Error, ErrorKind};

0 commit comments

Comments
 (0)