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

Remove unnecessary generics from encode and decode functions #103

Closed
wants to merge 1 commit into from

Conversation

eblocha
Copy link

@eblocha eblocha commented Jan 26, 2025

The RecordBatchEncoder::encode and RecordBatchEncoder::decode functions have a generic for the compression function closure.

This makes calling these APIs require naming these generics, when this is not necessary.

For example, to call RecordBatchDecoder::decode, you have to do this:

RecordBatchDecoder::decode::<
    bytes::Bytes,
    fn(&mut bytes::Bytes, Compression) -> anyhow::Result<bytes::Bytes>,
>(&mut records)

Or you get this error:

error[E0283]: type annotations needed
   --> src/clients/consumer.rs:368:52
    |
368 | ...                   let Ok(part_records) = RecordBatchDecoder::decode(&mut part_records)
    |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `F` declared on the associated function `decode`
    |
    = note: multiple `impl`s satisfying `for<'a> _: Fn(&'a mut bytes::Bytes, Compression)` found in the following crates: `alloc`, `core`:
            - impl<A, F> Fn<A> for &F
              where A: std::marker::Tuple, F: Fn<A>, F: ?Sized;
            - impl<Args, F, A> Fn<Args> for Box<F, A>
              where Args: std::marker::Tuple, F: Fn<Args>, A: Allocator, F: ?Sized;
note: required by a bound in `kafka_protocol::records::RecordBatchDecoder::decode`
   --> /home/eb/.cargo/registry/src/index.crates.io-6f17d22bba15001f/kafka-protocol-0.14.0/src/records.rs:512:12
    |
510 |     pub fn decode<B: ByteBuf, F>(buf: &mut B) -> Result<Vec<Record>>
    |            ------ required by a bound in this associated function
511 |     where
512 |         F: Fn(&mut bytes::Bytes, Compression) -> Result<B>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RecordBatchDecoder::decode`
help: consider specifying the generic arguments
    |
368 |                             let Ok(part_records) = RecordBatchDecoder::decode::<bytes::Bytes, F>(&mut part_records)
    |                                                                              +++++++++++++++++++

We can remove the generic for the closure and the compiler can now infer the types:

RecordBatchDecoder::decode(&mut part_records)

@eblocha eblocha force-pushed the simplify-record-codec branch from f38fe70 to 500f3a7 Compare January 26, 2025 03:20
@eblocha
Copy link
Author

eblocha commented Jan 26, 2025

Whoops, just saw this is a dupe!

@eblocha eblocha closed this Jan 26, 2025
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

Successfully merging this pull request may close these issues.

1 participant