diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-20 05:34:51 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-20 05:34:51 +0800 |
| commit | 332ee84b59f33e3a3f1cb109e02a1c0c9fe108cd (patch) | |
| tree | fef6adc9cc40c54c9aa9b2113c7a70d9d291f56e | |
| parent | bf593d41fccc7cda9ae7b557f549d8c6e007c042 (diff) | |
Add output protocol trait and implementations
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/output_protocol.rs | 50 | ||||
| -rw-r--r-- | src/output_protocol/ipc.rs | 14 | ||||
| -rw-r--r-- | src/output_protocol/stderr.rs | 14 | ||||
| -rw-r--r-- | src/output_protocol/stdout.rs | 14 | ||||
| -rw-r--r-- | src/output_protocol/tcp.rs | 14 | ||||
| -rw-r--r-- | src/output_protocol/udp.rs | 14 | ||||
| -rw-r--r-- | src/output_protocol/udp_broadcast.rs | 14 |
8 files changed, 137 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 9e14dc6..cc8d946 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ mod args; pub use args::*; +mod output_protocol; +pub use output_protocol::*; + fn main() { println!("Hello, world!"); } diff --git a/src/output_protocol.rs b/src/output_protocol.rs new file mode 100644 index 0000000..87e2400 --- /dev/null +++ b/src/output_protocol.rs @@ -0,0 +1,50 @@ +use std::sync::Arc; + +pub mod ipc; +pub mod stderr; +pub mod stdout; +pub mod tcp; +pub mod udp; +pub mod udp_broadcast; + +pub use ipc::*; +pub use stderr::*; +pub use stdout::*; +pub use tcp::*; +pub use udp::*; +pub use udp_broadcast::*; + +/// Defines the protocol for an output channel that can be initialized and used to send messages. +/// +/// Implementors of this trait provide the underlying mechanism for message delivery, +/// such as writing to stdout, a file, or a network socket. +/// +/// # Type Parameters +/// +/// The trait methods return `impl Future<Output = ()> + Send + Sync`, allowing +/// asynchronous implementations that can be shared across threads. +pub trait OutputProtocol { + /// Initializes the output channel. + /// + /// This method consumes `self`, indicating it should be called once during setup + /// to prepare the output resource (e.g., opening a file, establishing a connection). + /// + /// # Returns + /// + /// A future that resolves when initialization is complete. + fn init(self) -> impl Future<Output = ()> + Send + Sync; + + /// Sends a message string through the output channel. + /// + /// This method takes `self: Arc<Self>`, allowing the output channel to be shared + /// across multiple concurrent senders (e.g., in multi-threaded or async contexts). + /// + /// # Arguments + /// + /// * `str` - The string message to send. + /// + /// # Returns + /// + /// A future that resolves when the message has been sent. + fn send(self: Arc<Self>, str: &str) -> impl Future<Output = ()> + Send + Sync; +} diff --git a/src/output_protocol/ipc.rs b/src/output_protocol/ipc.rs new file mode 100644 index 0000000..c9c88c1 --- /dev/null +++ b/src/output_protocol/ipc.rs @@ -0,0 +1,14 @@ +use crate::OutputProtocol; + +#[derive(Debug, Default)] +pub struct IPCOutputProtocol {} + +impl OutputProtocol for IPCOutputProtocol { + async fn init(self) { + todo!() + } + + async fn send(self: std::sync::Arc<Self>, _str: &str) { + todo!() + } +} diff --git a/src/output_protocol/stderr.rs b/src/output_protocol/stderr.rs new file mode 100644 index 0000000..fabd390 --- /dev/null +++ b/src/output_protocol/stderr.rs @@ -0,0 +1,14 @@ +use crate::OutputProtocol; + +#[derive(Debug, Default)] +pub struct StandardErrorProtocol; + +impl OutputProtocol for StandardErrorProtocol { + async fn init(self) { + // No initialization needed + } + + async fn send(self: std::sync::Arc<Self>, str: &str) { + eprintln!("{}", str); + } +} diff --git a/src/output_protocol/stdout.rs b/src/output_protocol/stdout.rs new file mode 100644 index 0000000..3640507 --- /dev/null +++ b/src/output_protocol/stdout.rs @@ -0,0 +1,14 @@ +use crate::OutputProtocol; + +#[derive(Debug, Default)] +pub struct StandardOutputProtocol; + +impl OutputProtocol for StandardOutputProtocol { + async fn init(self) { + // No initialization needed + } + + async fn send(self: std::sync::Arc<Self>, str: &str) { + println!("{}", str); + } +} diff --git a/src/output_protocol/tcp.rs b/src/output_protocol/tcp.rs new file mode 100644 index 0000000..c2490e6 --- /dev/null +++ b/src/output_protocol/tcp.rs @@ -0,0 +1,14 @@ +use crate::OutputProtocol; + +#[derive(Debug, Default)] +pub struct TCPOutputProtocol {} + +impl OutputProtocol for TCPOutputProtocol { + async fn init(self) { + todo!() + } + + async fn send(self: std::sync::Arc<Self>, _str: &str) { + todo!() + } +} diff --git a/src/output_protocol/udp.rs b/src/output_protocol/udp.rs new file mode 100644 index 0000000..f1e5876 --- /dev/null +++ b/src/output_protocol/udp.rs @@ -0,0 +1,14 @@ +use crate::OutputProtocol; + +#[derive(Debug, Default)] +pub struct UDPOutputProtocol {} + +impl OutputProtocol for UDPOutputProtocol { + async fn init(self) { + todo!() + } + + async fn send(self: std::sync::Arc<Self>, _str: &str) { + todo!() + } +} diff --git a/src/output_protocol/udp_broadcast.rs b/src/output_protocol/udp_broadcast.rs new file mode 100644 index 0000000..5a5aa57 --- /dev/null +++ b/src/output_protocol/udp_broadcast.rs @@ -0,0 +1,14 @@ +use crate::OutputProtocol; + +#[derive(Debug, Default)] +pub struct UDPBroadcastOutputProtocol {} + +impl OutputProtocol for UDPBroadcastOutputProtocol { + async fn init(self) { + todo!() + } + + async fn send(self: std::sync::Arc<Self>, _str: &str) { + todo!() + } +} |
