fidl_server/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! A library for implementing FIDL servers.
//!
//! This crate allows you to implement a FIDL server by writing a single
//! function that handles an incoming request.
//!
//! # Example
//!
//! The easiest approach is to call `serve_detached` with a request stream and a
//! handler function. This will spawn a background task for each client and
//! automatically log errors:
//!
//! ```
//! fs.dir("svc").add_fidl_service(|stream: LogRequestStream| {
//! serve_detached(stream, |request| {
//! let LogRequest::Log { message } = request;
//! info!(message);
//! Ok(())
//! });
//! });
//! ```
//!
//! You can also run a server in the current task with `serve`. In this case,
//! you need to deal with the returned error yourself:
//!
//! ```
//! if let Err(err) = serve(stream, handler).await {
//! error!("{:?}", err);
//! }
//! ```
//!
//! Instead of passing a function, you can also pass any object that implements
//! the `RequestHandler` trait. For example:
//!
//! ```
//! struct LogHandler;
//!
//! impl RequestHandler<LogMarker> for LogHandler {
//! fn handle_request(&self, request: LogRequest) -> Result<(), Error> {
//! let LogRequest::Log { message } = request;
//! info!(message);
//! Ok(())
//! }
//! }
//!
//! // Somewhere else...
//! fs.dir("svc").add_fidl_service(|stream: LogRequestStream| {
//! serve_detached(stream, LogHandler);
//! });
//!
//! For asynchronous request handling you can alternatively use the following:
//!
//! * `AsyncRequestHandler`
//! * `serve_async`
//! * `serve_async_detached`.
//! ```
#![deny(missing_docs)]
mod server;
pub use server::*;