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§
- Kafka
Future - A flexible future which supports call chaining and other asynchronous programming patterns.
Traits§
- Base
Function - A function which takes objects of type
Aand returns objects of typeB.
Functions§
- Base
Function_ new - A
BaseFunctionthat callsfunction, standing in for a Java lambda implementingKafkaFuture.BaseFunction. A method Java adds later as an overload ofapplygets its own factory,BaseFunction_with_<parameter>.