Reputation: 91
Recently, I've been playing with Rust and gRPC using the Tonic libraries. I started looking at how to create a custom Codec and I'm scratching my head over this...
Starting with this chunk of code, I copied the MockEncoder
& MockDecoder
and added all the same imports used here in the test module:
https://github.com/hyperium/tonic/blob/master/tonic/src/codec/prost.rs#L133-L158
Then I got stuck on this error:
no method named `bytes` found for mutable reference `&mut tonic::codec::DecodeBuf<'_>` in the current scope
method not found in `&mut tonic::codec::DecodeBuf<'_>`rustc(E0599)
codec.rs(55, 37): method not found in `&mut tonic::codec::DecodeBuf<'_>`
The error is complaining about let out = Vec::from(buf.bytes());
because DecodeBuf
doesn't implement a bytes method.
Now, for some reason, if I wrap this code I copied in a module with #[cfg(tests)]
(similar to how this originally appeared in Tonic) this error goes away. I can compile and run tests without any errors.
Curious, what is #[cfg(tests)]
doing that makes buf.bytes()
compile and is there something I can import in order to move MockDecoder outside of this test config?
Upvotes: 0
Views: 332
Reputation: 76529
The option is #[cfg(test)]
(singular), not #[cfg(tests)]
(plural). This is probably not failing because it's not being compiled or run at all, since the option is never set.
Upvotes: 2