Implementation:Fede1024 Rust rdkafka FFI Type Aliases
| Knowledge Sources | |
|---|---|
| Domains | FFI, Error_Handling, Kafka_Protocol |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Rust-friendly type aliases, error code enums, and API key enums that wrap raw librdkafka C bindings into idiomatic Rust types used throughout the rdkafka ecosystem.
Description
The types module in rdkafka-sys provides three categories of exports: (1) roughly 30 pub type aliases that give readable names to opaque C struct types (e.g., RDKafka for rd_kafka_t, RDKafkaConf for rd_kafka_conf_t), (2) the RDKafkaErrorCode enum with ~180 non-exhaustive variants covering all Kafka error codes with Display, Error, and From<RDKafkaRespErr> implementations, and (3) the RDKafkaApiKey enum with 59 variants representing Kafka protocol API request types.
Usage
These types are the bridge between raw FFI and the safe rdkafka crate. Use RDKafkaErrorCode when handling errors from librdkafka operations. The type aliases are used internally when declaring function signatures. RDKafkaApiKey is used for protocol-level request identification in mock clusters and testing.
Code Reference
Source Location
- Repository: Fede1024_Rust_rdkafka
- File: rdkafka-sys/src/types.rs
- Lines: 1-589
Signature
// Type aliases (selected)
pub type RDKafka = bindings::rd_kafka_t;
pub type RDKafkaConf = bindings::rd_kafka_conf_t;
pub type RDKafkaMessage = bindings::rd_kafka_message_t;
pub type RDKafkaTopic = bindings::rd_kafka_topic_t;
pub type RDKafkaTopicPartitionList = bindings::rd_kafka_topic_partition_list_t;
pub type RDKafkaMetadata = bindings::rd_kafka_metadata_t;
pub type RDKafkaHeaders = bindings::rd_kafka_headers_t;
pub type RDKafkaMockCluster = bindings::rd_kafka_mock_cluster_t;
// Enum re-exports
pub type RDKafkaType = bindings::rd_kafka_type_t;
pub type RDKafkaConfRes = bindings::rd_kafka_conf_res_t;
pub type RDKafkaRespErr = bindings::rd_kafka_resp_err_t;
// Error code enum (~180 variants)
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RDKafkaErrorCode {
#[doc = "Received message is incorrect"]
BadMessage = -199,
#[doc = "Bad/unknown compression"]
BadCompression = -198,
#[doc = "Broker is going away"]
Destroy = -197,
#[doc = "Generic failure"]
Fail = -196,
// ... ~180 variants total
#[doc = "Success"]
NoError = 0,
#[doc = "Offset out of range"]
OffsetOutOfRange = 1,
// ... broker error codes
}
// API key enum (59 variants)
#[non_exhaustive]
#[repr(i16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
pub enum RDKafkaApiKey {
Produce = 0,
Fetch = 1,
ListOffsets = 2,
Metadata = 3,
// ... 59 variants total
}
Import
use rdkafka_sys::{RDKafkaErrorCode, RDKafkaApiKey, RDKafka, RDKafkaConf};
// or via the high-level crate:
use rdkafka::error::RDKafkaErrorCode;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Raw C enum value | RDKafkaRespErr | Yes | C error code from librdkafka function calls |
Outputs
| Name | Type | Description |
|---|---|---|
| RDKafkaErrorCode | enum | Idiomatic Rust error code with Display and Error traits |
| Type aliases | pub type | ~30 readable type names for C opaque structs |
| RDKafkaApiKey | enum | 59-variant enum for Kafka protocol API request types |
Usage Examples
Converting C Error Codes
use rdkafka_sys::{RDKafkaErrorCode, RDKafkaRespErr};
// Convert from C enum to Rust enum
let c_err = RDKafkaRespErr::RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART;
let rust_err: RDKafkaErrorCode = c_err.into();
assert_eq!(rust_err, RDKafkaErrorCode::UnknownTopicOrPartition);
// Display provides human-readable description
println!("Error: {}", rust_err);
// Output: "Error: Unknown topic or partition"
Using API Keys for Mock Cluster
use rdkafka_sys::RDKafkaApiKey;
let api_key = RDKafkaApiKey::Produce;
let wire_value: i16 = api_key.into();
assert_eq!(wire_value, 0);
let fetch_key = RDKafkaApiKey::Fetch;
let wire_value: i16 = fetch_key.into();
assert_eq!(wire_value, 1);