Cannot use Bytes crate from git, but crate works

Something strange is happening when I try to use the bytes crate via git instead of the crate from crates.io. It seems the BytesMut type no longer impls BufMut. I have a small reproducing example here:

Cargo.toml:

[package]
name = "foo"
version = "0.1.0"
edition = "2021"

[dependencies]
#bytes = "1.6"
bytes = { git = "GitHub - tokio-rs/bytes: Utilities for working with bytes", branch = "master" }
tokio = { version = "*", features = ["full"] }

main.rs:

use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
use bytes::BytesMut;

#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let mut buffer = BytesMut::with_capacity(10);
f.read_buf(&mut buffer).await?;
Ok(())
}

Error when using the dependency from git instead of as a crate:

error[E0277]: the trait bound BytesMut: bytes::buf::buf_mut::BufMut is not satisfied
--> src/main.rs:10:16
|
10 | f.read_buf(&mut buffer).await?;
| -------- ^^^^^^^^^^^ the trait bytes::buf::buf_mut::BufMut is not implemented for BytesMut
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait bytes::buf::buf_mut::BufMut:
ReadBuf<'a>
Box
bytes::bytes_mut::BytesMut
bytes::buf::chain::Chain<T, U>
bytes::buf::limit::Limit
Vec
&mut T
&mut [u8]
&mut [MaybeUninit]
note: required by a bound in tokio::io::AsyncReadExt::read_buf
--> /.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/io/util/async_read_ext.rs:249:16
|
246 | fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
| -------- required by a bound in this associated function
...
249 | B: BufMut + ?Sized,
| ^^^^^^ required by this bound in AsyncReadExt::read_buf

I'd be thankful for any pointers on what's going on

It's because tokio also depends on bytes, but it depends on the crates.io version that is considered different from your github version, so now you end up with two different versions of bytes. Each copy has different traits.

You need to use a [patch.crates-io] section instead. See Overriding Dependencies - The Cargo Book for more info.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.