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;
#[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 {
handler: EventHandler,
address: Option<String>,

@ -5,12 +5,16 @@ use crate::ipc::context::Context;
use crate::ipc::stream_emitter::StreamEmitter;
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(crate) handler: EventHandler,
}
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> {
let stream = TcpStream::connect(address).await?;
let (mut read_half, write_half) = stream.into_split();

@ -1,11 +1,26 @@
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 {
/// The event emitter
pub emitter: StreamEmitter,
}
impl Context {
pub fn new(emitter: StreamEmitter) -> Self {
pub(crate) fn new(emitter: StreamEmitter) -> Self {
Self { emitter }
}
}

@ -6,11 +6,16 @@ use crate::ipc::stream_emitter::StreamEmitter;
use std::sync::Arc;
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(crate) handler: EventHandler,
}
impl IPCServer {
/// Starts the IPC Server.
/// Invoked by [IPCBuilder::build_server](crate::builder::IPCBuilder::build_server)
pub async fn start(self, address: &str) -> Result<()> {
let listener = TcpListener::bind(address).await?;
let handler = Arc::new(self.handler);
@ -27,7 +32,7 @@ impl IPCServer {
}
/// 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 emitter = StreamEmitter::new(write_half);
let ctx = Context::new(StreamEmitter::clone(&emitter));

@ -1,5 +1,35 @@
//! This project provides an ipc server and client implementation using
//! 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)]
mod tests;

Loading…
Cancel
Save