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

BytesMut lacks an implementation of std::io::Write #77

Closed
NeoLegends opened this issue Mar 17, 2017 · 8 comments
Closed

BytesMut lacks an implementation of std::io::Write #77

NeoLegends opened this issue Mar 17, 2017 · 8 comments
Milestone

Comments

@NeoLegends
Copy link

Copied from tokio-rs/tokio-io#28

I'm currently using the codec module to implement a framed tansport.

Before the refactoring, the encode-method of the old Codec trait received a Vec as parameter to serialize the message into. Vec directly implements std::io::Write which makes it very easy to put any kind of data into it, since most implementations have some way of writing into an instance of Write.

Now the Vec has been replaced by BytesMut, which lacks a direct implementation of that trait. Write-compatibility is only archievable through into_writer, which consumes the BytesMut. Consuming is not possible, though, because the method only receives the BytesMut by &mut.

Is this desired behavior?

@NeoLegends NeoLegends changed the title BufMut lacks an implementation of std::io::Write BytesMut lacks an implementation of std::io::Write Mar 17, 2017
@carllerche
Copy link
Member

This seems fine to me. My only question is if BytesMut should grow as needed or return an error when it is full.

Thoughts @alexcrichton

@alexcrichton
Copy link
Contributor

Sounds like a good idea to me! I'd grow the buffer internally

@carllerche
Copy link
Member

@alexcrichton So BufMut has a writer adapter: BufMut::writer(). That implementation does not grow the buffer. It's built on BufMut::bytes_mut and remaining_mut.

@vorner
Copy link
Contributor

vorner commented Apr 17, 2017

I don't think a writer that doesn't grow the buffer is of much use. Usually, when I need a writer, I delegate the encoding or writing to some other code. And often I don't know in advance how much such code will write.

An example of code that would make sense:

use serde_cbor;
use tokio_io::io::Encoder;

struct E;

impl Encoder for E {
  ...
  fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
    serde_cbor::to_writer(dst)
  }
}

Now I have to wrap dst into a newtype that implements writer because it doesn't know how to be a writer (which isn't much work). If it didn't extend the dst as needed, I'd still need to wrap it, because I don't know the size in advance.

@carllerche
Copy link
Member

@ColinFinck
Copy link

@carllerche BytesMut still lacks an implementation of std::io::Write.
What your URL refers to is an implementation of std::fmt::Write, which is something different.

I'd say this issue is still unresolved.

@taiki-e
Copy link
Member

taiki-e commented Jul 29, 2021

@ColinFinck please see #478

@ColinFinck
Copy link

Thanks for the reference, @taiki-e
In the meantime, I'm using BufMut::writer on BytesMut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants