tonic/lib.rs
1//! A Rust implementation of [gRPC], a high performance, open source, general
2//! RPC framework that puts mobile and HTTP/2 first.
3//!
4//! [`tonic`] is a gRPC over HTTP/2 implementation focused on **high
5//! performance**, **interoperability**, and **flexibility**. This library was
6//! created to have first class support of async/await and to act as a core building
7//! block for production systems written in Rust.
8//!
9//! # Examples
10//!
11//! Examples can be found in the [`tonic-examples`] crate.
12//!
13//! # Getting Started
14//!
15//! Follow the instructions in the [`tonic-build`] crate documentation.
16//!
17//! # Feature Flags
18//!
19//! - `transport`: Enables the fully featured, batteries included client and server
20//! implementation based on [`hyper`], [`tower`] and [`tokio`]. This enables `server`
21//! and `channel` features. Enabled by default.
22//! - `server`: Enables just the full featured server portion of the `transport` feature.
23//! - `channel`: Enables just the full featured channel portion of the `transport` feature.
24//! - `router`: Enables the [`axum`] based service router. Enabled by default.
25//! - `codegen`: Enables all the required exports and optional dependencies required
26//! for [`tonic-build`]. Enabled by default.
27//! - `tls-ring`: Enables the [`rustls`] based TLS options for the `transport` feature using
28//! the [`ring`] libcrypto provider. Not enabled by default.
29//! - `tls-aws-lc`: Enables the [`rustls`] based TLS options for the `transport` feature using
30//! the [`aws-lc-rs`] libcrypto provider. Not enabled by default.
31//! - `tls-native-roots`: Adds system trust roots to [`rustls`]-based gRPC clients using the
32//! [`rustls-native-certs`] crate. Not enabled by default.
33//! - `tls-webpki-roots`: Add the standard trust roots from the [`webpki-roots`] crate to
34//! `rustls`-based gRPC clients. Not enabled by default.
35//! - `tls-connect-info`: Adds additional implementations of [`Connected`]
36//! on common TLS connectors. Not enabled by default, unless any of the other `tls-*`
37//! features are enabled. This feature is useful for when trying to use a custom
38//! TLS connector with `connect_with_connector` without enabling any `tls-*` features.
39//! - `gzip`: Enables compressing requests, responses, and streams. Depends on [`flate2`].
40//! Not enabled by default.
41//! - `deflate`: Enables compressing requests, responses, and streams. Depends on [`flate2`].
42//! Not enabled by default.
43//! - `zstd`: Enables compressing requests, responses, and streams. Depends on [`zstd`].
44//! Not enabled by default.
45//!
46//! # Structure
47//!
48//! ## Generic implementation
49//!
50//! The main goal of [`tonic`] is to provide a generic gRPC implementation over HTTP/2
51//! framing. This means at the lowest level this library provides the ability to use
52//! a generic HTTP/2 implementation with different types of gRPC encodings formats. Generally,
53//! some form of codegen should be used instead of interacting directly with the items in
54//! [`client`] and [`server`].
55//!
56//! ## Transport
57//!
58//! The [`transport`] module contains a fully featured HTTP/2.0 [`Channel`] (gRPC terminology)
59//! and [`Server`]. These implementations are built on top of [`tokio`], [`hyper`] and [`tower`].
60//! It also provides many of the features that the core gRPC libraries provide such as load balancing,
61//! tls, timeouts, and many more. This implementation can also be used as a reference implementation
62//! to build even more feature rich clients and servers. This module also provides the ability to
63//! enable TLS using [`rustls`], via the `tls` feature flag.
64//!
65//! # Code generated client/server configuration
66//!
67//! ## Max Message Size
68//!
69//! Currently, both servers and clients can be configured to set the max message encoding and
70//! decoding size. This will ensure that an incoming gRPC message will not exhaust the systems
71//! memory. By default, the decoding message limit is `4MB` and the encoding limit is `usize::MAX`.
72//!
73//! [gRPC]: https://grpc.io
74//! [`tonic`]: https://github.com/hyperium/tonic
75//! [`tokio`]: https://docs.rs/tokio
76//! [`hyper`]: https://docs.rs/hyper
77//! [`tower`]: https://docs.rs/tower
78//! [`tonic-build`]: https://docs.rs/tonic-build
79//! [`ring`]: https://docs.rs/ring
80//! [`aws-lc-rs`]: https://docs.rs/aws-lc-rs
81//! [`tonic-examples`]: https://github.com/hyperium/tonic/tree/master/examples
82//! [`Codec`]: codec/trait.Codec.html
83//! [`Channel`]: transport/struct.Channel.html
84//! [`Server`]: transport/struct.Server.html
85//! [`Connected`]: transport/server/trait.Connected.html
86//! [`rustls`]: https://docs.rs/rustls
87//! [`client`]: client/index.html
88//! [`transport`]: transport/index.html
89//! [`rustls-native-certs`]: https://docs.rs/rustls-native-certs
90//! [`webpki-roots`]: https://docs.rs/webpki-roots
91//! [`flate2`]: https://docs.rs/flate2
92//! [`zstd`]: https://docs.rs/zstd
93
94#![recursion_limit = "256"]
95#![doc(
96 html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg"
97)]
98#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
99#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
100#![cfg_attr(docsrs, feature(doc_auto_cfg))]
101
102pub mod body;
103pub mod client;
104pub mod codec;
105pub mod metadata;
106pub mod server;
107pub mod service;
108
109#[cfg(any(feature = "server", feature = "channel"))]
110pub mod transport;
111
112mod extensions;
113mod macros;
114mod request;
115mod response;
116mod status;
117mod util;
118
119/// A re-export of [`async-trait`](https://docs.rs/async-trait) for use with codegen.
120#[cfg(feature = "codegen")]
121pub use async_trait::async_trait;
122
123#[doc(inline)]
124pub use codec::Streaming;
125pub use extensions::GrpcMethod;
126pub use http::Extensions;
127pub use request::{IntoRequest, IntoStreamingRequest, Request};
128pub use response::Response;
129pub use status::{Code, ConnectError, Status, TimeoutExpired};
130
131pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
132
133#[doc(hidden)]
134#[cfg(feature = "codegen")]
135pub mod codegen;
136
137/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
138/// By default, the Err value is of type [`Status`] but this can be overridden if desired.
139pub type Result<T, E = Status> = std::result::Result<T, E>;