From 7ed8fcc09d51b71eabf6b0633bf416bccd98e219 Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Thu, 6 Jun 2024 10:53:18 +0300 Subject: [PATCH] docs: add documentation for component client commit-id:d3030c87 --- crates/mempool_infra/src/component_client.rs | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/crates/mempool_infra/src/component_client.rs b/crates/mempool_infra/src/component_client.rs index 020bd6bde..7a4aeb675 100644 --- a/crates/mempool_infra/src/component_client.rs +++ b/crates/mempool_infra/src/component_client.rs @@ -2,6 +2,55 @@ use tokio::sync::mpsc::{channel, Sender}; use crate::component_definitions::ComponentRequestAndResponseSender; +/// The `ComponentClient` struct is a generic client for sending component requests and receiving +/// responses asynchronously. +/// +/// # Type Parameters +/// - `Request`: The type of the request. This type must implement both `Send` and `Sync` traits. +/// - `Response`: The type of the response. This type must implement both `Send` and `Sync` traits. +/// +/// # Fields +/// - `tx`: An asynchronous sender channel for transmitting +/// `ComponentRequestAndResponseSender` messages. +/// +/// # Example +/// ```rust +/// // Example usage of the ComponentClient +/// use tokio::sync::mpsc::Sender; +/// +/// use crate::starknet_mempool_infra::component_client::ComponentClient; +/// use crate::starknet_mempool_infra::component_definitions::ComponentRequestAndResponseSender; +/// +/// // Define your request and response types +/// struct MyRequest { +/// pub content: String, +/// } +/// +/// struct MyResponse { +/// content: String, +/// } +/// +/// #[tokio::main] +/// async fn main() { +/// // Create a channel for sending requests and receiving responses +/// let (tx, _rx) = tokio::sync::mpsc::channel::< +/// ComponentRequestAndResponseSender, +/// >(100); +/// +/// // Instantiate the client. +/// let client = ComponentClient::new(tx); +/// +/// // Instantiate a request. +/// let request = MyRequest { content: "Hello, world!".to_string() }; +/// +/// // Send the request; typically, the client should await for a response. +/// client.send(request); +/// } +/// ``` +/// +/// # Notes +/// - The `ComponentClient` struct is designed to work in an asynchronous environment, utilizing +/// Tokio's async runtime and channels. #[derive(Clone)] pub struct ComponentClient where