Implementation:Fede1024 Rust rdkafka SmolRuntime Adapter
| Knowledge Sources | |
|---|---|
| Domains | Async_Runtime, Runtime_Abstraction, Integration |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Example implementation of the AsyncRuntime trait that integrates rust-rdkafka with the smol async runtime, demonstrating a zero-allocation delay type approach.
Description
The runtime_smol example defines a custom SmolRuntime struct that implements rdkafka::util::AsyncRuntime. It maps spawn to smol::spawn(task).detach() and delay_for to smol::Timer::after(duration).map(|_| ()), using future::Map<smol::Timer, fn(Instant)> as the Delay associated type (avoiding heap allocation). The main function uses smol::block_on to drive the async block, creating a FutureProducer and StreamConsumer parameterized with SmolRuntime.
Usage
Use this as a reference when integrating rust-rdkafka with the smol runtime. The zero-allocation delay type pattern (Map<Timer, fn(Instant)>) demonstrates how to avoid boxing futures when the timer type is known at compile time.
Code Reference
Source Location
- Repository: Fede1024_Rust_rdkafka
- File: examples/runtime_smol.rs
- Lines: 1-109
Signature
pub struct SmolRuntime;
impl AsyncRuntime for SmolRuntime {
type Delay = future::Map<smol::Timer, fn(Instant)>;
fn spawn<T>(task: T)
where
T: Future<Output = ()> + Send + 'static,
{
smol::spawn(task).detach();
}
fn delay_for(duration: Duration) -> Self::Delay {
smol::Timer::after(duration).map(|_| ())
}
}
Import
use rdkafka::util::AsyncRuntime;
use futures_util::future::{self, FutureExt};
use smol::Timer;
use std::time::{Duration, Instant};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| task | impl Future<Output = ()> + Send + 'static | Yes | Async task to spawn on smol executor |
| duration | Duration | Yes | Sleep duration for delay_for |
Outputs
| Name | Type | Description |
|---|---|---|
| Delay | future::Map<smol::Timer, fn(Instant)> | Zero-allocation timer future from smol |
Usage Examples
Using SmolRuntime with Producer and Consumer
use rdkafka::config::ClientConfig;
use rdkafka::consumer::StreamConsumer;
use rdkafka::producer::FutureProducer;
// Define the runtime adapter
pub struct SmolRuntime;
impl rdkafka::util::AsyncRuntime for SmolRuntime {
type Delay = futures_util::future::Map<smol::Timer, fn(std::time::Instant)>;
fn spawn<T: Future<Output = ()> + Send + 'static>(task: T) {
smol::spawn(task).detach();
}
fn delay_for(duration: Duration) -> Self::Delay {
use futures_util::FutureExt;
smol::Timer::after(duration).map(|_| ())
}
}
// Use smol::block_on as the executor
smol::block_on(async {
let producer: FutureProducer<_, SmolRuntime> = ClientConfig::new()
.set("bootstrap.servers", "localhost:9092")
.create()
.expect("Producer creation failed");
let consumer: StreamConsumer<_, SmolRuntime> = ClientConfig::new()
.set("bootstrap.servers", "localhost:9092")
.set("group.id", "my-group")
.create()
.expect("Consumer creation failed");
});