-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
tokio-util/codec: AnyDelimiterCodec can decode bytes but only encode str #7169
Comments
Maybe we can change impl<T> Encoder<T> for AnyDelimiterCodec
where
T: AsRef<str>,
{ ... } to impl<T> Encoder<T> for AnyDelimiterCodec
where
T: AsRef<[u8]>,
{ ... } @maminrayej, any advice? if it 's acceptable, I can submit PR |
Your suggested change would be a breaking change and can only be done with a major version bump of tokio-util. |
What types are there that are |
You are right, thank you for your reply. And can not solve it except a breaking change, right? (Or other methods that I didn't think of, please give me any advice) In addition, when reading the implement, I find another "defect": fn encode(&mut self, chunk: T, buf: &mut BytesMut) -> Result<(), AnyDelimiterCodecError> {
let chunk = chunk.as_ref();
//
buf.reserve(chunk.len() + 1);
buf.put(chunk.as_bytes());
buf.put(self.sequence_writer.as_ref());
Ok(())
} param of buf.reserve() is |
I think |
Version
Platform
Darwin Mac.localdomain 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020 arm64
Description
I'm consuming a binary data stream using
tokio_util::codec::Framed
configured with atokio_util::codec::AnyDelimiterCodec
. My delimited is the0
byte (it is a binary protocol; yes, I do have an encoding that guarantees no 0 bytes appear within a message). Consuming messages works perfectly.On the other hand, encoding new binary messages hits a problem, the codec implements
AsRef<str>
for the encoder, which makes it expect an utf-8 string, not an arbitrary binary.Looking at the code, it just downcasts it to an arbitrary binary anyway, so there's no value in the utf-8 enforcement in the first place:
Thus, my question is whether it's possible to change this generic enforcement, or add one that operates on raw binary blobs too?
Currently I can work around this via
std::str::from_utf8_unchecked
inside an unsafe block, it just seems like an omission in the library that the codec operates 100% safely on binary streams but it has an unwarranted syntactic limitation of utf-8 (in one direction only).The text was updated successfully, but these errors were encountered: