Implementation:Fede1024 Rust rdkafka FFI Bindings
| Knowledge Sources | |
|---|---|
| Domains | FFI, Kafka_Protocol, Systems_Programming |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Auto-generated Rust FFI binding declarations for the entire librdkafka C API, providing raw extern function signatures, struct layouts, enum types, and constants required for all Kafka operations from Rust.
Description
The bindings module is generated by rust-bindgen (v0.72.1) from librdkafka's C headers (rdkafka.h and rdkafka_mock.h). It declares approximately 500+ extern "C" function prototypes, 80+ struct and enum type definitions, and numerous constants. Opaque C structs (e.g., rd_kafka_t, rd_kafka_conf_t) are represented as zero-sized marker types, while data-carrying structs (e.g., rd_kafka_message_t, rd_kafka_metadata_t) have full field layouts. This module is the foundational FFI layer through which every interaction with the native librdkafka library flows.
Usage
This module is used internally by the rdkafka-sys crate and re-exported at the crate root. End users of the higher-level rdkafka crate rarely interact with these bindings directly. Use this when you need raw access to librdkafka C functions not yet wrapped by the safe Rust API, or when implementing custom FFI extensions.
Code Reference
Source Location
- Repository: Fede1024_Rust_rdkafka
- File: rdkafka-sys/src/bindings.rs
- Lines: 1-3909
Signature
// Key opaque type aliases
pub type rd_kafka_t = rd_kafka_s; // Kafka client handle
pub type rd_kafka_conf_t = rd_kafka_conf_s; // Configuration handle
pub type rd_kafka_topic_t = rd_kafka_topic_s; // Topic handle
pub type rd_kafka_queue_t = rd_kafka_queue_s; // Event queue handle
// Key data-carrying struct
#[repr(C)]
pub struct rd_kafka_message_s {
pub err: rd_kafka_resp_err_t,
pub rkt: *mut rd_kafka_topic_t,
pub partition: i32,
pub payload: *mut c_void,
pub len: usize,
pub key: *mut c_void,
pub key_len: usize,
pub offset: i64,
pub _private: *mut c_void,
}
// Error code enum (~180 variants)
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, TryFromPrimitive)]
pub enum rd_kafka_resp_err_t {
RD_KAFKA_RESP_ERR__BEGIN = -200,
RD_KAFKA_RESP_ERR_NO_ERROR = 0,
// ... 180+ error codes
}
// Example extern functions
extern "C" {
pub fn rd_kafka_version() -> c_int;
pub fn rd_kafka_version_str() -> *const c_char;
pub fn rd_kafka_new(
type_: rd_kafka_type_t,
conf: *mut rd_kafka_conf_t,
errstr: *mut c_char,
errstr_size: usize,
) -> *mut rd_kafka_t;
pub fn rd_kafka_produce(
rkt: *mut rd_kafka_topic_t,
partition: i32,
msgflags: c_int,
payload: *mut c_void,
len: usize,
key: *const c_void,
keylen: usize,
msg_opaque: *mut c_void,
) -> c_int;
}
Import
use rdkafka_sys::*;
// or selectively:
use rdkafka_sys::bindings::{rd_kafka_t, rd_kafka_conf_t, rd_kafka_resp_err_t};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| C header files | rdkafka.h, rdkafka_mock.h | Yes | Source headers for bindgen generation |
| librdkafka native library | Shared/static library | Yes | Linked at compile time via build.rs |
Outputs
| Name | Type | Description |
|---|---|---|
| Opaque type aliases | Rust type aliases | Zero-sized marker types for C handles (rd_kafka_t, rd_kafka_conf_t, etc.) |
| Data structs | #[repr(C)] structs | Memory-layout-compatible Rust structs (rd_kafka_message_t, rd_kafka_metadata_t, etc.) |
| Enum types | #[repr(u32/i32)] enums | C enum mappings (rd_kafka_resp_err_t, rd_kafka_type_t, etc.) |
| Extern functions | extern "C" fn | ~500 function declarations for the full librdkafka API |
| Constants | pub const | Offset constants, message flags, event type flags, purge flags |
Usage Examples
Checking librdkafka Version
use rdkafka_sys::bindings::{rd_kafka_version, rd_kafka_version_str};
use std::ffi::CStr;
fn check_version() {
unsafe {
let version_int = rd_kafka_version();
let version_str = CStr::from_ptr(rd_kafka_version_str())
.to_str()
.unwrap();
println!("librdkafka version: {} ({})", version_str, version_int);
}
}
Using Offset Constants
use rdkafka_sys::{
RD_KAFKA_OFFSET_BEGINNING,
RD_KAFKA_OFFSET_END,
RD_KAFKA_OFFSET_STORED,
};
// Start consuming from the beginning of a partition
let offset = RD_KAFKA_OFFSET_BEGINNING; // -2
// Start consuming from the end
let offset = RD_KAFKA_OFFSET_END; // -1
// Resume from the last stored offset
let offset = RD_KAFKA_OFFSET_STORED; // -1000