Skip to main content

Module kafka_future

Module kafka_future 

Source
Expand description

A flexible future for asynchronous Kafka operations.

Translated from org.apache.kafka.common.KafkaFuture.

In Java, KafkaFuture<T> is an abstract class implementing Future<T>. In the Producer API, Java’s Producer.send() returns Future<RecordMetadata>, hiding the internal FutureRecordMetadata behind the Future interface.

In Rust, KafkaFuture<T> serves as the concrete return type that hides internal implementations behind a public, type-erased interface.

KafkaFuture::then_apply takes a BaseFunction: a closure, or a value from BaseFunction_new. Rust infers a closure’s parameter type only from an Fn bound, not from a trait the closure gets through a blanket impl, so a closure whose body calls a method on its argument writes the argument’s type out (|v: T| ..). A function held as a trait object (Box<dyn BaseFunction<T, R>>, Arc<..>) is not itself a BaseFunction; it is passed as move |v| f.apply(v):

use confluent_kafka::common::KafkaFuture;
use confluent_kafka::common::kafka_future::{self, BaseFunction};

let future = KafkaFuture::completed_future(Ok("four".to_string()));

// The body calls `len()` on `v`, so its type is written out.
let len = future.then_apply(|v: String| Ok(v.len()));
assert_eq!(len.get().await.unwrap(), 4);

// A stored function is forwarded to through a closure.
let f: Box<dyn BaseFunction<String, usize>> = Box::new(kafka_future::BaseFunction_new(|v: String| Ok(v.len())));
let len = future.then_apply(move |v| f.apply(v));
assert_eq!(len.get().await.unwrap(), 4);

Structs§

KafkaFuture
A flexible future which supports call chaining and other asynchronous programming patterns.

Traits§

BaseFunction
A function which takes objects of type A and returns objects of type B.

Functions§

BaseFunction_new
A BaseFunction that calls function, standing in for a Java lambda implementing KafkaFuture.BaseFunction. A method Java adds later as an overload of apply gets its own factory, BaseFunction_with_<parameter>.