Skip to main content

new

Function new 

Source
pub fn new<F>(function: F) -> impl Callback
Expand description

Returns a Callback that calls function, standing in for a Java lambda implementing Callback: function is on_completion, and every other method keeps its default. A method Java adds later as an overload of onCompletion gets its own factory, with_<parameter>, taking a function of that overload’s shape. The closure’s parameter types are inferred:

use confluent_kafka::producer::{callback, Callback};

let callback: Box<dyn Callback> = Box::new(callback::new(|metadata, error| {
    if let Some(error) = error {
        eprintln!("send failed: {error}");
    } else if let Some(metadata) = metadata {
        println!("sent at offset {}", metadata.offset());
    }
}));

Every FnOnce(Option<&RecordMetadata>, Option<&Error>) + Send + Sync also implements Callback directly, but then the closure’s parameter types must be written out: Rust infers a closure’s signature only from an Fn bound, not from a trait it gets through a blanket impl.

§Arguments

  • function - the body of on_completion, called once with the record’s metadata and error.