-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Reuse Framed buffers with new codec #717
Comments
I have this workaround: fn set_codec<T: AsyncRead + AsyncWrite, C1, C2: Encoder + Decoder>(framed: Framed<T, C1>, codec: C2) -> Framed<T, C2> {
let parts1 = framed.into_parts();
let mut parts2 = Framed::new(parts1.io, codec).into_parts();
parts2.read_buf = parts1.read_buf;
parts2.write_buf = parts1.write_buf;
Framed::from_parts(parts2)
} But it seems like |
An interesting case. It feels pretty specialized, but also that it should be better supported. We could possibly add Thoughts? |
Yep, Here is where I ended up using my workaround: |
One issue @seanmonstar raises is that you might want to take ownership of data form the first codec and move it into the second codec. My proposed API would not solve that. |
We could add |
I think the solution for this will be to switch to using |
@carllerche can you expand a bit on how the |
Refs: #2428 A BufReader will contain the buffer as well as the stream. This type can be removed from the codec and passed to a new one. I am going to close this issue as it is relatively low priority and is blocked on #2428. However, once #2428 happens (soonish), anyone should feel free to attempt this work. |
I'm not aware of any blockers. |
I'm attempting to use multiple codecs on a single stream: my protocol (WebSockets) has an initial handshake before the data frames are processed. I have two codecs for this, one for the handshake and one for the data frames.
After the handshake codec is done, I'd like to reuse the read and write buffers from the
Framed
with a new codec. In the deprecatedtokio_io::codec
codebase I can do this by callingFramed::from_parts(old_framed.into_parts(), new_codec)
. But in #394 the codec instance was moved inside theFramedParts
struct andFramed::from_parts
can no longer take thenew_codec
parameter.How should I be doing this in terms of
tokio_codec
functions?The text was updated successfully, but these errors were encountered: