### Run an example Source: https://docs.rs/rdkafka/latest/src/rdkafka/lib.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Command to run an example from the examples folder. ```bash cargo run --example -- ``` -------------------------------- ### Run an example Source: https://docs.rs/rdkafka Run an example from the examples folder using cargo run with the example name and arguments. ```bash cargo run --example -- ``` -------------------------------- ### Run an example with cargo run Source: https://docs.rs/rdkafka/latest/index.html Runs an example from the examples folder using cargo run. Replace with the example's name and with any arguments. ```shell cargo run --example -- ``` -------------------------------- ### unwrap examples Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_acknowledgement_t.html?search=std%3A%3Avec Shows how to use Option::unwrap to get the contained Some value, panicking if None. The first example shows a successful case, and the second shows a panic case. ```rust let x = Some("air"); assert_eq!(x.unwrap(), "air"); ``` ```rust let x: Option<&str> = None; assert_eq!(x.unwrap(), "air"); // fails ``` -------------------------------- ### expect examples Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_acknowledgement_t.html?search=std%3A%3Avec Shows how to use Option::expect to get the contained Some value or panic with a custom message. The first example shows a successful case, the second shows a panic case, and the third shows a recommended message style. ```rust let x = Some("value"); assert_eq!(x.expect("fruits are healthy"), "value"); ``` ```rust let x: Option<&str> = None; x.expect("fruits are healthy"); // panics with `fruits are healthy` ``` ```rust let item = slice.get(0) .expect("slice should not be empty"); ``` -------------------------------- ### expect example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use expect to get the contained Ok value, panicking with a custom message if the value is Err. ```rust let x: Result = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure` ``` -------------------------------- ### into_ok example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use into_ok to get the contained Ok value without panicking, for error types that are infallible. ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } ``` -------------------------------- ### into_ok example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use into_ok to extract the Ok value from a Result that is guaranteed to be Ok. The example defines a function only_good_news that returns Result and then calls into_ok to get the String. ```rust let s: String = only_good_news().into_ok(); println!("{s}"); ``` -------------------------------- ### unwrap basic usage example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows basic usage of unwrap to get the contained Ok value, panicking if the value is Err. ```rust let x: Result = Ok(2); assert_eq!(x.unwrap(), 2); ``` -------------------------------- ### Result::ok Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of ok, converting Result to Option, showing Some for Ok and None for Err. ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` -------------------------------- ### take example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Demonstrates Option::take, which takes the value out of the option, leaving None in its place. The example shows taking from a Some and from a None. ```rust let mut x = Some(2); let y = x.take(); assert_eq!(x, None); assert_eq!(y, Some(2)); let mut x: Option = None; let y = x.take(); assert_eq!(x, None); assert_eq!(y, None); ``` -------------------------------- ### Option::into_flat_iter example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the nightly-only experimental API option_into_flat_iter, showing how to flatten an optional iterator. ```rust #![feature(option_into_flat_iter)] let o1 = Some([1, 2]); let o2 = None::<&[usize]>; assert_eq!(o1.into_flat_iter().collect::>(), [1, 2]); assert_eq!(o2.into_flat_iter().collect::>(), Vec::<&usize>::new()); ``` -------------------------------- ### Option::from example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Demonstrates moving a value into a new Some using From. ```rust let o: Option = Option::from(67); assert_eq!(Some(67), o); ``` -------------------------------- ### xor example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Demonstrates Option::xor, which returns Some if exactly one of the two options is Some, otherwise None. The example covers all four combinations of Some and None. ```rust let x = Some(2); let y: Option = None; assert_eq!(x.xor(y), Some(2)); let x: Option = None; let y = Some(2); assert_eq!(x.xor(y), Some(2)); let x = Some(2); let y = Some(2); assert_eq!(x.xor(y), None); let x: Option = None; let y: Option = None; assert_eq!(x.xor(y), None); ``` -------------------------------- ### Option::as_ref example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the as_ref method, showing how to convert &Option to Option<&T> to avoid moving the original value. ```rust let text: Option = Some("Hello, world!".to_string()); // First, cast `Option` to `Option<&String>` with `as_ref`, // then consume *that* with `map`, leaving `text` on the stack. let text_length: Option = text.as_ref().map(|s| s.len()); println!("still can print text: {text:?}"); ``` -------------------------------- ### Result::is_ok_and Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of is_ok_and, showing predicate matching on Ok values and handling of Err. ```rust let x: Result = Ok(2); assert_eq!(x.is_ok_and(|x| x > 1), true); let x: Result = Ok(0); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Err("hey"); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Ok("ownership".to_string()); assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` -------------------------------- ### Option::is_some_and example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the is_some_and method, showing how to check if an Option is Some and the value matches a predicate. ```rust let x: Option = Some(2); assert_eq!(x.is_some_and(|x| x > 1), true); let x: Option = Some(0); assert_eq!(x.is_some_and(|x| x > 1), false); let x: Option = None; assert_eq!(x.is_some_and(|x| x > 1), false); let x: Option = Some("ownership".to_string()); assert_eq!(x.as_ref().is_some_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` -------------------------------- ### Option::as_mut example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the as_mut method, showing how to convert &mut Option to Option<&mut T> and modify the value. ```rust let mut x = Some(2); match x.as_mut() { Some(v) => *v = 42, None => {}, } assert_eq!(x, Some(42)); ``` -------------------------------- ### Option::is_none_or example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the is_none_or method, showing how to check if an Option is None or the value matches a predicate. ```rust let x: Option = Some(2); assert_eq!(x.is_none_or(|x| x > 1), true); let x: Option = Some(0); assert_eq!(x.is_none_or(|x| x > 1), false); let x: Option = None; assert_eq!(x.is_none_or(|x| x > 1), true); let x: Option = Some("ownership".to_string()); assert_eq!(x.as_ref().is_none_or(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` -------------------------------- ### and_then example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.GroupResult.html Shows how to use and_then to chain fallible operations. The first example defines a function sq_then_to_string and asserts the results of chaining it. The second example uses and_then with metadata() to chain operations that may fail, demonstrating error propagation. ```rust fn sq_then_to_string(x: u32) -> Result { x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") } assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string())); assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed")); assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number")); ``` ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` -------------------------------- ### into_err example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use into_err to extract the Err value from a Result that is guaranteed to be Err. The example defines a function only_bad_news that returns Result and then calls into_err to get the String. ```rust fn only_bad_news() -> Result { Err("Oops, it failed".into()) } let error: String = only_bad_news().into_err(); println!("{error}"); ``` -------------------------------- ### rd_kafka_mock_broker_set_up Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/fn.rd_kafka_mock_broker_set_up.html?search=u32+-%3E+bool Documentation for the rd_kafka_mock_broker_set_up function, including its signature, parameters, and return type. ```APIDOC ## rd_kafka_mock_broker_set_up ### Description Sets up a mock broker in a mock cluster. This function is part of the librdkafka C API bindings and is used for testing Kafka client behavior without a real broker. ### Signature ```rust pub unsafe extern "C" fn rd_kafka_mock_broker_set_up( mcluster: *mut rd_kafka_mock_cluster_s, broker_id: i32, ) -> rd_kafka_resp_err_t ``` ### Parameters - **mcluster** (*mut rd_kafka_mock_cluster_s) - Required - Pointer to the mock cluster structure. - **broker_id** (i32) - Required - The ID of the broker to set up. ### Return Value - **rd_kafka_resp_err_t** - The response error code indicating success or failure. ``` -------------------------------- ### as_slice examples Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_acknowledgement_t.html?search=std%3A%3Avec Shows how to use Option::as_slice to get a slice of the contained value or an empty slice for None. The first example demonstrates the basic behavior, and the second shows the inverse relationship with [_]::first. ```rust assert_eq!( [Some(1234).as_slice(), None.as_slice()], [&[1234][..], &[][..]], ); ``` ```rust for i in [Some(1234_u16), None] { assert_eq!(i.as_ref(), i.as_slice().first()); } ``` -------------------------------- ### AdminClient from_config_and_context implementation Source: https://docs.rs/rdkafka/latest/src/rdkafka/admin.rs.html?search= Creates an AdminClient from a ClientConfig and context. It creates a native config, uses RDKafkaType::RD_KAFKA_PRODUCER, starts a polling thread, and returns the AdminClient. The polling thread is started with a queue and a stop flag. ```rust impl FromClientConfigAndContext for AdminClient { fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult> { let native_config = config.create_native_config()?; // librdkafka only provides consumer and producer types. We follow the // example of the Python bindings in choosing to pretend to be a // producer, as producer clients are allegedly more lightweight. [0] // // [0]: https://github.com/confluentinc/confluent-kafka-python/blob/bfb07dfbca47c256c840aaace83d3fe26c587360/confluent_kafka/src/Admin.c#L1492-L1493 let client = Client::new( config, native_config, RDKafkaType::RD_KAFKA_PRODUCER, context, )?; let queue = Arc::new(client.new_native_queue()); let should_stop = Arc::new(AtomicBool::new(false)); let handle = start_poll_thread(queue.clone(), should_stop.clone()); Ok(AdminClient { client, queue, should_stop, handle: Some(handle), }) } } ``` -------------------------------- ### Client::new Source: https://docs.rs/rdkafka/latest/rdkafka/client/struct.Client.html?search=std%3A%3Avec Creates a new Client instance with the given configuration, native configuration, client type, and context. ```APIDOC ## Client::new ### Description Creates a new `Client` given a configuration, a client type and a context. ### Method `pub fn new( config: &ClientConfig, native_config: NativeClientConfig, rd_kafka_type: RDKafkaType, context: C, ) -> KafkaResult>` ### Parameters - `config` (`&ClientConfig`) - Required - The client configuration. - `native_config` (`NativeClientConfig`) - Required - The native rdkafka-sys configuration. - `rd_kafka_type` (`RDKafkaType`) - Required - The type of the client (e.g., producer or consumer). - `context` (`C`) - Required - The client context. ### Returns `KafkaResult>` - A result containing the new `Client` or an error. ``` -------------------------------- ### unwrap_or_default example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Shows how to use Option::unwrap_or_default to get the contained value or the default value for the type. ```rust assert_eq!(x.unwrap_or_default(), 0); assert_eq!(y.unwrap_or_default(), 12); ``` -------------------------------- ### and_then example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Demonstrates the behavior of the and_then method, which calls op if the result is Ok, otherwise returns the Err value of self. The example defines a function sq_then_to_string and shows how it can be used for control flow. ```rust fn sq_then_to_string(x: u32) -> Result { x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") } assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string())); assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed")); assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number")); ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/base_consumer/struct.BaseConsumer.html?search=u32+-%3E+bool Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method N/A (Rust associated function) ### Endpoint N/A ### Parameters - **config** (`&ClientConfig`) - Required - The client configuration. - **context** (`C`) - Required - The client context. ### Returns - **KafkaResult>** - The created BaseConsumer instance. ``` -------------------------------- ### Option::zip example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Demonstrates combining two Options into a tuple of their values if both are Some. ```rust let x = Some(1); let y = Some("hi"); let z = None::; assert_eq!(x.zip(y), Some((1, "hi"))); assert_eq!(x.zip(z), None); ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html?search=std%3A%3Avec Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method Rust associated function ### Signature ```rust fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult> ``` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Return Value - **KafkaResult>** - A new `BaseConsumer` instance with the given context. ``` -------------------------------- ### expect_err example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use expect_err to get the contained Err value, panicking with a custom message if the value is Ok. ```rust let x: Result = Ok(10); x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10` ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html?search=u32+-%3E+bool Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method N/A (Rust associated function) ### Endpoint N/A (Rust associated function) ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Request Example N/A ### Response #### Success Response - **KafkaResult>** - A new BaseConsumer instance with the given context. #### Response Example N/A ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/base_consumer/struct.BaseConsumer.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult> ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Returns KafkaResult> - A new BaseConsumer instance with the given context. ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method fn from_config_and_context ### Signature ```rust fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - Client configuration. - **context** (C) - Client context. ### Returns - **KafkaResult** - Result containing the StreamConsumer instance. ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html?search= Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method Rust associated function ### Signature ```rust fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Return Value - **KafkaResult** - A new StreamConsumer instance. ``` -------------------------------- ### iter_mut example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use iter_mut to get a mutable iterator over the contained value, yielding one value if Ok, otherwise none. ```rust let mut x: Result = Ok(7); match x.iter_mut().next() { Some(v) => *v = 40, None => {}, } assert_eq!(x, Ok(40)); let mut x: Result = Err("nothing!"); assert_eq!(x.iter_mut().next(), None); ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Return Value KafkaResult - The created StreamConsumer. ``` -------------------------------- ### as_mut_slice examples Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_acknowledgement_t.html?search=std%3A%3Avec Shows how to use Option::as_mut_slice to get a mutable slice of the contained value or an empty mutable slice for None. The first example demonstrates the basic behavior, the second shows modifying the value through the slice, and the third shows the inverse relationship with [_]::first_mut. ```rust assert_eq!( [Some(1234).as_mut_slice(), None.as_mut_slice()], [&mut [1234][..], &mut [][..]], ); ``` ```rust let mut x = Some(1234); x.as_mut_slice()[0] += 1; assert_eq!(x, Some(1235)); ``` ```rust assert_eq!(Some(123).as_mut_slice().first_mut(), Some(&mut 123)) ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html?search=std%3A%3Avec Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Returns - **KafkaResult** - A new StreamConsumer instance. ``` -------------------------------- ### iter example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows how to use Result::iter to get an iterator over the possibly contained value, yielding one value if Ok and none if Err. ```rust let x: Result = Ok(7); assert_eq!(x.iter().next(), Some(&7)); let x: Result = Err("nothing!"); assert_eq!(x.iter().next(), None); ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method N/A (Rust associated function) ### Endpoint N/A ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Returns - **KafkaResult>** - A new BaseConsumer instance with the given context. ### Example ```rust let config = ClientConfig::new(); let context = MyContext::new(); let consumer = BaseConsumer::from_config_and_context(&config, context)?; ``` ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method from_config_and_context ### Signature ```rust fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult> ``` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Returns - **KafkaResult>** - A new BaseConsumer instance with the given context. ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.StreamConsumer.html Creates a client from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method N/A (Rust associated function) ### Signature ```rust fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - The client configuration. - **context** (C) - The client context. ### Returns - **KafkaResult** - Result containing the created StreamConsumer. ``` -------------------------------- ### Client::new Source: https://docs.rs/rdkafka/latest/rdkafka/client/struct.Client.html?search=u32+-%3E+bool Creates a new Client instance with the given configuration, native client config, client type, and context. ```APIDOC ## Client::new ### Description Creates a new `Client` given a configuration, a client type and a context. ### Method `pub fn new(config: &ClientConfig, native_config: NativeClientConfig, rd_kafka_type: RDKafkaType, context: C) -> KafkaResult>` ### Parameters - `config` (`&ClientConfig`) - Required - The client configuration. - `native_config` (`NativeClientConfig`) - Required - The native rdkafka-sys client configuration. - `rd_kafka_type` (`RDKafkaType`) - Required - The type of the client (e.g., producer or consumer). - `context` (`C`) - Required - The client context. ### Returns `KafkaResult>` - A result containing the new `Client` instance or an error. ``` -------------------------------- ### ClientConfig::get Source: https://docs.rs/rdkafka/latest/rdkafka/config/struct.ClientConfig.html Gets the value of a parameter in the configuration. Returns the current value set for `key`, or `None` if no value for `key` exists. Note that this method will only ever return values that were installed by a call to `ClientConfig::set`. To retrieve librdkafka’s default value for a parameter, build a `NativeClientConfig` and then call `NativeClientConfig::get` on the resulting object. ```APIDOC ## ClientConfig::get ### Description Gets the value of a parameter in the configuration. Returns the current value set for `key`, or `None` if no value for `key` exists. Note that this method will only ever return values that were installed by a call to `ClientConfig::set`. To retrieve librdkafka’s default value for a parameter, build a `NativeClientConfig` and then call `NativeClientConfig::get` on the resulting object. ### Method get ### Signature pub fn get(&self, key: &str) -> Option<&str> ### Parameters - **key** (&str) - Required - The configuration key to retrieve. ### Returns - **Option<&str>** - The current value for the key, or `None` if not set. ``` -------------------------------- ### unwrap_unchecked example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_acknowledgement_t.html?search= Demonstrates the unsafe unwrap_unchecked method. The first example shows a safe usage on Some, while the second example shows calling it on None, which is undefined behavior. ```rust let x = Some("air"); assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); ``` ```rust let x: Option<&str> = None; assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); // Undefined behavior! ``` -------------------------------- ### Client::new Source: https://docs.rs/rdkafka/latest/rdkafka/client/struct.Client.html Creates a new Client instance with the given configuration, client type, and context. ```APIDOC ## Client::new ### Description Creates a new `Client` given a configuration, a client type and a context. ### Method Constructor ### Signature ```rust pub fn new( config: &ClientConfig, native_config: NativeClientConfig, rd_kafka_type: RDKafkaType, context: C, ) -> KafkaResult> ``` ### Parameters - **config** (`&ClientConfig`) - Required - The client configuration. - **native_config** (`NativeClientConfig`) - Required - The native client configuration. - **rd_kafka_type** (`RDKafkaType`) - Required - The type of the Kafka client (e.g., producer, consumer). - **context** (`C`) - Required - The client context. ### Returns - `KafkaResult>` - The newly created `Client` instance, or an error. ``` -------------------------------- ### or example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Returns the option if it contains a value, otherwise returns optb. Eagerly evaluated. ```rust let x = Some(2); let y = None; assert_eq!(x.or(y), Some(2)); let x = None; let y = Some(100); assert_eq!(x.or(y), Some(100)); let x = Some(2); let y = Some(100); assert_eq!(x.or(y), Some(2)); let x: Option = None; let y = None; assert_eq!(x.or(y), None); ``` -------------------------------- ### Result::cloned Example Source: https://docs.rs/rdkafka/latest/rdkafka/error/type.KafkaResult.html?search=u32+-%3E+bool Example for the cloned method on Result<&T, E>. ```rust let val = 12; let x: Result<&i32, i32> = Ok(&val); assert_eq!(x, Ok(&12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` -------------------------------- ### Result::copied Example Source: https://docs.rs/rdkafka/latest/rdkafka/error/type.KafkaResult.html?search=u32+-%3E+bool Example for the copied method on Result<&T, E>. ```rust let val = 12; let x: Result<&i32, i32> = Ok(&val); assert_eq!(x, Ok(&12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` -------------------------------- ### rd_kafka_mock_broker_set_up Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/fn.rd_kafka_mock_broker_set_up.html Sets up a mock broker in a mock cluster. ```APIDOC ## Function rd_kafka_mock_broker_set_up ### Description Sets up a mock broker in a mock cluster. ### Signature ```rust pub unsafe extern "C" fn rd_kafka_mock_broker_set_up( mcluster: *mut rd_kafka_mock_cluster_s, broker_id: i32, ) -> rd_kafka_resp_err_t ``` ### Parameters - **mcluster** (pointer to rd_kafka_mock_cluster_s) - Required - Pointer to the mock cluster. - **broker_id** (i32) - Required - The ID of the broker to set up. ### Returns - **rd_kafka_resp_err_t** - Response error code indicating success or failure. ``` -------------------------------- ### Result::transpose Example Source: https://docs.rs/rdkafka/latest/rdkafka/error/type.KafkaResult.html?search=u32+-%3E+bool Example for the transpose method on Result, E>. ```rust #[derive(Debug, Eq, PartialEq)] struct SomeErr; let x: Result, SomeErr> = Ok(Some(5)); let y: Option> = Some(Ok(5)); assert_eq!(x.transpose(), y); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.StreamConsumer.html Creates a client from a client configuration using the default client context. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method N/A (Rust associated function) ### Signature ```rust fn from_config(config: &ClientConfig) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - The client configuration. ### Returns - **KafkaResult** - Result containing the created StreamConsumer. ``` -------------------------------- ### Option::is_some example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the is_some method, showing how to check if an Option is Some. ```rust let x: Option = Some(2); assert_eq!(x.is_some(), true); let x: Option = None; assert_eq!(x.is_some(), false); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/base_consumer/struct.BaseConsumer.html?search=u32+-%3E+bool Creates a client from a client configuration using the default client context. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method N/A (Rust associated function) ### Endpoint N/A ### Parameters - **config** (`&ClientConfig`) - Required - The client configuration. ### Returns - **KafkaResult** - The created BaseConsumer instance. ``` -------------------------------- ### get Source: https://docs.rs/rdkafka/latest/rdkafka/message/struct.BorrowedHeaders.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets the specified header, where the first header corresponds to index 0. ```APIDOC ## get ### Description Gets the specified header, where the first header corresponds to index 0. ### Method `fn get(&self, idx: usize) -> Header<'_, &[u8]>` ### Parameters - **idx** (usize) - Required - The index of the header to retrieve. ### Returns - `Header<'_, &[u8]>` - The header at the given index. ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a client from a client configuration using the default client context. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method N/A (Rust associated function) ### Endpoint N/A ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Returns - **KafkaResult** - A new BaseConsumer instance. ### Example ```rust let config = ClientConfig::new(); let consumer = BaseConsumer::from_config(&config)?; ``` ``` -------------------------------- ### BaseProducer::from_config_and_context implementation Source: https://docs.rs/rdkafka/latest/src/rdkafka/producer/base_producer.rs.html?search= Internal method that creates a BaseProducer from a ClientConfig and ProducerContext. Sets up custom partitioner if provided, enables required events, and creates the client. ```rust fn from_config_and_context( config: &ClientConfig, context: C, ) -> KafkaResult> { let native_config = config.create_native_config()?; let context = Arc::new(context); if context.get_custom_partitioner().is_some() { let default_topic_config = unsafe { rdsys::rd_kafka_conf_get_default_topic_conf(native_config.ptr()) }; unsafe { rdsys::rd_kafka_topic_conf_set_opaque( default_topic_config, Arc::as_ptr(&context) as *mut c_void, ) }; unsafe { rdsys::rd_kafka_topic_conf_set_partitioner_cb( default_topic_config, Some(partitioner_cb::), ) } } unsafe { rdsys::rd_kafka_conf_set_events( native_config.ptr(), rdsys::RD_KAFKA_EVENT_DR | rdsys::RD_KAFKA_EVENT_STATS | rdsys::RD_KAFKA_EVENT_ERROR | rdsys::RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH, ) }; let client = Client::new_context_arc( config, native_config, RDKafkaType::RD_KAFKA_PRODUCER, context, )?; Ok(BaseProducer::from_client(client)) } ``` -------------------------------- ### Result::cloned Example for &mut T Source: https://docs.rs/rdkafka/latest/rdkafka/error/type.KafkaResult.html?search=u32+-%3E+bool Example for the cloned method on Result<&mut T, E>. ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/base_consumer/struct.BaseConsumer.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method fn from_config(config: &ClientConfig) -> KafkaResult ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Returns KafkaResult - A new BaseConsumer instance. ``` -------------------------------- ### Result::copied Example for &mut T Source: https://docs.rs/rdkafka/latest/rdkafka/error/type.KafkaResult.html?search=u32+-%3E+bool Example for the copied method on Result<&mut T, E>. ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` -------------------------------- ### Option::is_none example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Example for the is_none method, showing how to check if an Option is None. ```rust let x: Option = Some(2); assert_eq!(x.is_none(), false); let x: Option = None; assert_eq!(x.is_none(), true); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method fn from_config ### Signature ```rust fn from_config(config: &ClientConfig) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - Client configuration. ### Returns - **KafkaResult** - Result containing the StreamConsumer instance. ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html?search= Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method Rust associated function ### Signature ```rust fn from_config(config: &ClientConfig) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Return Value - **KafkaResult** - A new StreamConsumer instance. ``` -------------------------------- ### Result::as_ref Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of as_ref, converting Result to Result of references. ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` -------------------------------- ### Result::is_err Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of is_err, showing false for Ok and true for Err. ```rust let x: Result = Ok(-3); assert_eq!(x.is_err(), false); let x: Result = Err("Some error message"); assert_eq!(x.is_err(), true); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method fn from_config(config: &ClientConfig) -> KafkaResult ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Return Value KafkaResult - The created StreamConsumer. ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html?search=std%3A%3Avec Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method fn from_config(config: &ClientConfig) -> KafkaResult ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Returns - **KafkaResult** - A new StreamConsumer instance. ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method from_config ### Signature ```rust fn from_config(config: &ClientConfig) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Returns - **KafkaResult** - A new BaseConsumer instance. ``` -------------------------------- ### Result::map Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of map, applying a function to the Ok value and printing the results. ```rust let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::().map(|i| i * 2) { Ok(n) => println!("{n}"), Err(..) => {} } } ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html?search=std%3A%3Avec Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method Rust associated function ### Signature ```rust fn from_config(config: &ClientConfig) -> KafkaResult ``` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Return Value - **KafkaResult** - A new `BaseConsumer` instance. ``` -------------------------------- ### Result::is_ok Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of is_ok on a Result, demonstrating true for Ok and false for Err. ```rust let x: Result = Ok(-3); assert_eq!(x.is_ok(), true); let x: Result = Err("Some error message"); assert_eq!(x.is_ok(), false); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/struct.BaseConsumer.html?search=u32+-%3E+bool Creates a client from a client configuration. The default client context will be used. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method N/A (Rust associated function) ### Endpoint N/A (Rust associated function) ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Request Example N/A ### Response #### Success Response - **KafkaResult** - A new BaseConsumer instance. #### Response Example N/A ``` -------------------------------- ### Result::as_mut Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of as_mut, showing mutation of Ok and Err values through mutable references. ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` -------------------------------- ### from_config_and_context Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/base_consumer/struct.BaseConsumer.html Creates a BaseConsumer from a client configuration and a client context. ```APIDOC ## from_config_and_context ### Description Creates a client from a client configuration and a client context. ### Method from_config_and_context ### Signature `fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult>` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. - **context** (C) - Required - The client context. ### Returns - **KafkaResult>** - A new `BaseConsumer` instance with the given context. ``` -------------------------------- ### expect recommended message style example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Shows the recommended style for expect messages, describing the reason you expect the Result to be Ok. ```rust let path = std::env::var("IMPORTANT_PATH") .expect("env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`"); ``` -------------------------------- ### Result::err Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of err, converting Result to Option, showing None for Ok and Some for Err. ```rust let x: Result = Ok(2); assert_eq!(x.err(), None); let x: Result = Err("Nothing here"); assert_eq!(x.err(), Some("Nothing here")); ``` -------------------------------- ### Result::is_err_and Example Source: https://docs.rs/rdkafka/latest/rdkafka/admin/type.AlterConfigsResult.html?search= Example usage of is_err_and, showing predicate matching on Err values and handling of Ok. ```rust use std::io::{Error, ErrorKind}; let x: Result = Err(Error::new(ErrorKind::NotFound, "!")); assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), true); let x: Result = Err(Error::new(ErrorKind::PermissionDenied, "!")); assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false); let x: Result = Ok(123); assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false); let x: Result = Err("ownership".to_string()); assert_eq!(x.as_ref().is_err_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` -------------------------------- ### from_config Source: https://docs.rs/rdkafka/latest/rdkafka/consumer/base_consumer/struct.BaseConsumer.html Creates a BaseConsumer from a client configuration using the default context. ```APIDOC ## from_config ### Description Creates a client from a client configuration. The default client context will be used. ### Method from_config ### Signature `fn from_config(config: &ClientConfig) -> KafkaResult` ### Parameters - **config** (&ClientConfig) - Required - The client configuration. ### Returns - **KafkaResult** - A new `BaseConsumer` instance. ``` -------------------------------- ### flatten method example Source: https://docs.rs/rdkafka/latest/rdkafka/error/type.KafkaResult.html?search= Example for the flatten method on Result, E>, which removes one level of nesting. ```rust let x: Result, u32> = Ok(Ok("hello")); assert_eq!(Ok("hello"), x.flatten()); let x: Result, u32> = Ok(Err(6)); assert_eq!(Err(6), x.flatten()); let x: Result, u32> = Err(6); assert_eq!(Err(6), x.flatten()); ``` -------------------------------- ### ClientConfig::new Source: https://docs.rs/rdkafka/latest/rdkafka/config/struct.ClientConfig.html?search= Creates a new empty ClientConfig instance. ```APIDOC ## ClientConfig::new ### Description Creates a new empty configuration. ### Method `pub fn new() -> ClientConfig` ### Parameters None. ### Returns A new `ClientConfig` instance with no parameters set. ``` -------------------------------- ### ok_or example Source: https://docs.rs/rdkafka/latest/rdkafka/bindings/type.rd_kafka_interceptor_f_on_broker_state_change_t.html Shows how to use Option::ok_or to transform an Option into a Result, mapping Some to Ok and None to Err. ```rust let x = Some("foo"); assert_eq!(x.ok_or(0), Ok("foo")); let x: Option<&str> = None; assert_eq!(x.ok_or(0), Err(0)); ``` -------------------------------- ### ClientConfig create_with_context method Source: https://docs.rs/rdkafka/latest/src/rdkafka/config.rs.html?search=u32+-%3E+bool Uses the current configuration and the provided context to create a new Consumer or Producer. ```rust pub fn create_with_context(&self, context: C) -> KafkaResult where C: ClientContext, T: FromClientConfigAndContext, { T::from_config_and_context(self, context) } ```