Use rust's type system for special integer parameters - #135
Open
chikko80 wants to merge 1 commit into
Open
Conversation
Collaborator
|
"
These are great goals for the project. I think this is a good direction for the project." |
dhonig
reviewed
Sep 23, 2025
| CORRELATION_ID, | ||
| CLIENT_ID, | ||
| 1, | ||
| prelude::RequiredAcks::Leader, |
Collaborator
chikko80
commented
Sep 23, 2025
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub enum NumPartitions { | ||
| Default, |
Author
There was a problem hiding this comment.
I thinking about renaming that to BrokerDefault, to make it more clear
chikko80
commented
Sep 23, 2025
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub enum ReplicationFactor { | ||
| Default, |
Author
There was a problem hiding this comment.
I thinking about renaming that to BrokerDefault, to make it more clear
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I’ve recently started using this library and noticed that it doesn’t make good use of Rust’s type system to describe the API. I’ll probably be working on my own fork most of the time, since I plan to introduce more significant changes. However, I was wondering if this prerequisite change might also be of interest for the main API.
If this is not the direction you want to take this project, please feel free to close this MR, or let me know how we could make it compatible.
Please see the MR description below:
Title
Refactor sentinel integers to typed enums; update Admin API for topic creation
Summary
This PR replaces several raw integer “special values” with strongly typed enums and adjusts the Admin API to use a typed
NewTopicstructure. This improves correctness, readability, and discoverability, and prevents misuse of magic numbers.Motivation
-1,-2) scattered across the protocol and builder APIs.Key Changes
prelude::RequiredAcksenum:None (0),Leader (1),All (-1).protocol::produce::request::ProduceRequestfieldrequired_acks: RequiredAcks.producer::ProduceParamsandproducer::producesignature.producer_builder::ProducerBuilder::required_acks(RequiredAcks).ToBytemapping to the correct INT16 values.protocol::list_offsets::request::ReplicaIdenum:NormalConsumer (-1) | Broker(i32).protocol::list_offsets::request::TimestampQueryenum:Latest (-1) | Earliest (-2) | At(i64)..new/.addsignatures to use enums.consumer_builder::list_offsets(...)and related tests to use typed enums.protocol::create_topics::request::NumPartitions:Default (-1) | Count(i32).protocol::create_topics::request::ReplicationFactor:Default (-1) | Factor(i16).protocol::create_topics::request::Topicand.add(...)to accept these enums.protocol::create_topics::NewTopic(inmod.rs) with fields:topic_name: Stringnum_partitions: request::NumPartitionsreplication_factor: request::ReplicationFactoradmin::create_topicssignature to acceptVec<NewTopic>and pass through values.NewTopicinpreludefor convenience.Affected Files (high-level)
src/lib.rs: re-exportRequiredAcks,NewTopicand expose protocol modules in prelude.src/producer.rs,src/producer_builder.rs,src/protocol/produce/request.rs.src/protocol/list_offsets/request.rs,src/consumer_builder.rs,src/protocol/list_offsets/mod.rs(tests).src/protocol/create_topics/request.rs,src/protocol/create_topics/mod.rs(NewTopic),src/admin.rs.Backward Compatibility / Breaking Changes
ProducerBuilder::required_acks(i16)→ProducerBuilder::required_acks(RequiredAcks).producer::produce(..., required_acks: i16, ...)→required_acks: RequiredAcks.protocol::ProduceRequest::new(required_acks: i16, ...)→RequiredAcks.protocol::ListOffsetsRequest::new(..., replica_id: i32)→ReplicaId.protocol::ListOffsetsRequest::add(..., timestamp: i64)→TimestampQuery.protocol::create_topics::request::Topicfields and.add(...)now use enums.admin::create_topics(..., HashMap<&str, i32>)→create_topics(..., Vec<NewTopic>).Migration Guide
.required_acks(1)with.required_acks(RequiredAcks::Leader).produce(...)calls to passRequiredAcks.ReplicaId::NormalConsumerorReplicaId::Broker(id).TimestampQuery::{Latest, Earliest, At(ms)}instead of rawi64.HashMap<&str, i32>withVec<NewTopic>:Testing
cargo check --testssuccessfully.Rationale & Alternatives
Checklist