Update rustdoc

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 5792c6757d
commit c9afe76edc
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -9,6 +9,19 @@ use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
#[derive(Clone)] #[derive(Clone)]
/// A builder for the IPC server or client.
/// ```rust
///use rmp_ipc::IPCBuilder;
///IPCBuilder::new()
/// .address("127.0.0.1:2020")
/// // register callback
/// .on("ping", |_ctx, _event| Box::pin(async move {
/// println!("Received ping event.");
/// Ok(())
/// }))
/// // can also be build_client which would return an emitter for events
/// .build_server().await.unwrap();
/// ```
pub struct IPCBuilder { pub struct IPCBuilder {
handler: EventHandler, handler: EventHandler,
address: Option<String>, address: Option<String>,

@ -5,12 +5,16 @@ use crate::ipc::context::Context;
use crate::ipc::stream_emitter::StreamEmitter; use crate::ipc::stream_emitter::StreamEmitter;
use tokio::net::TcpStream; use tokio::net::TcpStream;
/// The IPC Client to connect to an IPC Server.
/// Use the [IPCBuilder](crate::builder::IPCBuilder) to create the client.
/// Usually one does not need to use the IPCClient object directly.
pub struct IPCClient { pub struct IPCClient {
pub(crate) handler: EventHandler, pub(crate) handler: EventHandler,
} }
impl IPCClient { impl IPCClient {
/// Connects to a given address and returns an emitter for events to that address /// Connects to a given address and returns an emitter for events to that address.
/// Invoked by [IPCBuilder::build_client](crate::builder::IPCBuilder::build_client)
pub async fn connect(self, address: &str) -> Result<StreamEmitter> { pub async fn connect(self, address: &str) -> Result<StreamEmitter> {
let stream = TcpStream::connect(address).await?; let stream = TcpStream::connect(address).await?;
let (mut read_half, write_half) = stream.into_split(); let (mut read_half, write_half) = stream.into_split();

@ -1,11 +1,26 @@
use crate::ipc::stream_emitter::StreamEmitter; use crate::ipc::stream_emitter::StreamEmitter;
/// An object provided to each callback function.
/// Currently it only holds the event emitter to emit response events in event callbacks.
/// ```rust
/// use rmp_ipc::context::Context;
/// use rmp_ipc::Event;
/// use rmp_ipc::error::Result;
///
/// async fn my_callback(ctx: &Context, _event: Event) -> Result<()> {
/// // use the emitter on the context object to emit events
/// // inside callbacks
/// ctx.emitter.emit("ping", ()).await?;
/// Ok(())
/// }
/// ```
pub struct Context { pub struct Context {
/// The event emitter
pub emitter: StreamEmitter, pub emitter: StreamEmitter,
} }
impl Context { impl Context {
pub fn new(emitter: StreamEmitter) -> Self { pub(crate) fn new(emitter: StreamEmitter) -> Self {
Self { emitter } Self { emitter }
} }
} }

@ -6,11 +6,16 @@ use crate::ipc::stream_emitter::StreamEmitter;
use std::sync::Arc; use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
/// The IPC Server listening for connections.
/// Use the [IPCBuilder](crate::builder::IPCBuilder) to create a server.
/// Usually one does not need to use the IPCServer object directly.
pub struct IPCServer { pub struct IPCServer {
pub(crate) handler: EventHandler, pub(crate) handler: EventHandler,
} }
impl IPCServer { impl IPCServer {
/// Starts the IPC Server.
/// Invoked by [IPCBuilder::build_server](crate::builder::IPCBuilder::build_server)
pub async fn start(self, address: &str) -> Result<()> { pub async fn start(self, address: &str) -> Result<()> {
let listener = TcpListener::bind(address).await?; let listener = TcpListener::bind(address).await?;
let handler = Arc::new(self.handler); let handler = Arc::new(self.handler);
@ -27,7 +32,7 @@ impl IPCServer {
} }
/// Handles a single tcp connection /// Handles a single tcp connection
pub async fn handle_connection(handler: Arc<EventHandler>, stream: TcpStream) { async fn handle_connection(handler: Arc<EventHandler>, stream: TcpStream) {
let (mut read_half, write_half) = stream.into_split(); let (mut read_half, write_half) = stream.into_split();
let emitter = StreamEmitter::new(write_half); let emitter = StreamEmitter::new(write_half);
let ctx = Context::new(StreamEmitter::clone(&emitter)); let ctx = Context::new(StreamEmitter::clone(&emitter));

@ -1,5 +1,35 @@
//! This project provides an ipc server and client implementation using //! This project provides an ipc server and client implementation using
//! messagepack. All calls are asynchronous and event based. //! messagepack. All calls are asynchronous and event based.
//! Client Example:
//! ```rust
//! use rmp_ipc::IPCBuilder;
//! // create the client
//! let emitter = IPCBuilder::new()
//! .address("127.0.0.1:2020")
//! // register callback
//! .on("ping", |_ctx, _event| Box::pin(async move {
//! println!("Received ping event.");
//! Ok(())
//! }))
//! .build_client().await.unwrap();
//!
//! // emit an initial event
//! emitter.emit("ping", ()).await?;
//! ```
//!
//! Server Example:
//! ```rust
//! use rmp_ipc::IPCBuilder;
//! // create the server
//! IPCBuilder::new()
//! .address("127.0.0.1:2020")
//! // register callback
//! .on("ping", |_ctx, _event| Box::pin(async move {
//! println!("Received ping event.");
//! Ok(())
//! }))
//! .build_server().await.unwrap();
//! ```
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

Loading…
Cancel
Save