Type annotations required: cannot resolve `_: bytes::ByteOrder`

Hi Rust Crew,

I am using the Bytes create to read data out of a socket, I then want to convert the data to a u32. Looking at the Bytes creates the buf trait lets you do just this (https://carllerche.github.io/bytes/bytes/trait.Buf.html) but every time I try to use the get function I get the error I put in the title.

Here is basically what my code is doing:

let mut bytes = BytesMut::with_capacity(64);
bytes.put(b'3');
let bufs = bytes.into_buf();
let new_size = bufs.get_u32();

then when I compile I get:

error[E0283]: type annotations required: cannot resolve _: bytes::ByteOrder
--> src/main.rs:11:22
|
11 | let new_size = bufs.get_u32();

I thought it might be because into_buf might be returning some kind of Result type, and the definition says it should not be.

Then I saw that the documentation defines the type as:

impl IntoBuf for Bytes[src]
type Buf = Cursor

So I thought maybe Cursor is not in scope, and that is why compiler is getting confused but adding: use std::io::Cursor; did not fix the issue.

So I am a bit stuck here, I thought from reading the docs on the Byte create I should be able to get from Byte -> Buf -> u32 pretty easily.

Thanks for any help.

This change added those methods and the docs reflect that - previously, however, the methods were generic over the ByteOrder type. So the version you’re using needs to have the byte order specified, eg bufs.get_u32::<BigEndian>().

1 Like

Thanks Vitaly that was it, now I know to always use the docs.rs to find the documentation instead of straight to github, lesson learned! Thanks.