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

Move memchr behind an optional feature #77

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: taiki-e/install-action@cargo-hack
- run: cargo build --all --all-features --all-targets
- run: cargo hack build --feature-powerset --no-dev-deps
- run: cargo hack build --feature-powerset --no-dev-deps --target thumbv7m-none-eabi --skip std,default
- run: cargo hack build --feature-powerset --no-dev-deps --target thumbv7m-none-eabi --skip std,default,memchr
- run: cargo test
- run: cargo test --no-default-features
- run: cargo test --no-default-features --features alloc
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ exclude = ["/.*"]

[features]
default = ["race", "std"]
std = ["alloc", "fastrand/std", "futures-io", "parking", "memchr"]
std = ["alloc", "fastrand/std", "futures-io", "parking"]
alloc = []
memchr = ["std", "memchr_crate"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the future support of core::io, I wonder if the memchr feature should not enable the std feature. However, I'm not sure if that will happen during the period when 2.x is the latest version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now futures-lite uses futures-io v0.3. I expect that v0.3 probably won't export core::io traits without a breaking change, right? So I think we are secure in doing this for now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

futures-io has (enabled by default) std feature for future compatibility about this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've decouple std and memchr.

race = ["fastrand"]

[dependencies]
fastrand = { version = "2.0.0", optional = true, default-features = false }
futures-core = { version = "0.3.5", default-features = false }
futures-io = { version = "0.3.5", optional = true }
memchr = { version = "2.3.3", optional = true }
memchr_crate = { package = "memchr", version = "2.3.3", optional = true }
parking = { version = "2.2.0", optional = true }
pin-project-lite = "0.2.0"

Expand Down
11 changes: 10 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ fn read_until_internal<R: AsyncBufReadExt + ?Sized>(
let (done, used) = {
let available = ready!(reader.as_mut().poll_fill_buf(cx))?;

if let Some(i) = memchr::memchr(byte, available) {
if let Some(i) = memchr(byte, available) {
buf.extend_from_slice(&available[..=i]);
(true, i + 1)
} else {
Expand Down Expand Up @@ -3091,3 +3091,12 @@ impl<T: AsyncWrite + Unpin> AsyncWrite for WriteHalf<T> {
Pin::new(&mut *inner).poll_close(cx)
}
}

#[cfg(feature = "memchr")]
use memchr_crate::memchr;

/// Unoptimized memchr fallback.
#[cfg(not(feature = "memchr"))]
fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
haystack.iter().position(|&b| b == needle)
}