-
-
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
codec: change Encoder to take &Item #1746
Conversation
As discussed here: tokio-rs/tokio-io#54. |
Since this is an input argument, then |
I agree with @Marwes' comment — I think the current design of this trait may be an artifact of older versions of the |
So I've changed the error[E0716]: temporary value dropped while borrowed
--> tokio-util/tests/length_delimited.rs:399:14
|
399 | let io = length_delimited::Builder::new()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
...
405 | });
| - temporary value is freed at the end of this statement
406 | pin_mut!(io);
| ------------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) This seems to happen because the
but neither of these approaches had the desired effect. Any suggestions? |
I did yet another attempt with using |
tokio-util/src/codec/encoder.rs
Outdated
@@ -18,5 +18,5 @@ pub trait Encoder { | |||
/// This method will encode `item` into the byte buffer provided by `dst`. | |||
/// The `dst` provided is an internal buffer of the `Framed` instance and | |||
/// will be written out when possible. | |||
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error>; | |||
fn encode(&mut self, item: &Item, dst: &mut BytesMut) -> Result<(), Self::Error>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't think about the lifetime issues :/ I am not able to decide on this obviously but &Item
with Item
as an argument seems like the way to go 👍
Do you have some kind of use case where you'd prefer to pass ownership? |
No, just thought it would work for both cases. |
Ping! Any further feedback before I spend time on rebasing this? |
This seems good to me. I can queue it up to be merged w/ tokio-util 0.3 (which can be sooner than later). |
78add5f
to
9cf3149
Compare
(Rebase not quite finished, will continue later, pending taiki-e/pin-project#170.) |
It looks like |
@carllerche ping? The question here is if it would be okay to revert to pin-project instead of pin-project-lite for tokio-util in order to make this work. |
I would rather avoid either then (use unsafe). |
@taiki-e that's great, thanks! So I rebased this on top of current master, keeping the use of pin-project-lite intact. However, iterating on this I question the change going from IMO, the current situation, where the |
I wonder if it makes sense to make |
@LucioFranco do you think that would fix the issues from #1746 (comment)? This feels like we're going back and forth. Also, if |
Passing |
But in that approach can I also pass in a |
Also the requirement to parametrize the |
Sure. See
That is only because |
The main downside is that some |
How is that a downside, exactly? |
If you accidentally pass something that does not implement It is probably not that big of an issue here but in combine |
c45296c
to
8bd8a83
Compare
I think this is ready for an actual review now. @Marwes thanks for your changes. I've squashed our changes together, but added you as a co-author to the resulting commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall just two questions
@@ -546,12 +543,12 @@ impl Decoder for LengthDelimitedCodec { | |||
} | |||
} | |||
|
|||
impl Encoder for LengthDelimitedCodec { | |||
type Item = Bytes; | |||
impl<T> Encoder<T> for LengthDelimitedCodec where T: AsRef<[u8]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may make sense to keep this Encoder<Bytes>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think so? This is strictly more general.
3aa5a94
to
0a705a8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks mostly good just a few questions, once we get this merged I'll get a 0.3
release out. Sorry for the delay!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I want to see LengthDelimitedCodec
take a Bytes
as well. Then we can get this release out, sorry for the super long delay on all this!
This allows passing references into `Encoder`s, which are going to encode the item into a given `BytesMut` anyway. Co-authored-by: Markus Westerlind <markus.westerlind@distilnetworks.com>
Since the encoded item is to be encoded into a given
BytesMut
, taking ownership is not necessary here.