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