Skip to content

Commit f162ca2

Browse files
authored
docs(ffi): generate FFI documentation (#2447)
1 parent 4c946af commit f162ca2

File tree

10 files changed

+73
-4
lines changed

10 files changed

+73
-4
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,4 @@ jobs:
179179
uses: actions-rs/cargo@v1
180180
with:
181181
command: rustdoc
182-
args: --features full -- --cfg docsrs -D broken-intra-doc-links
182+
args: --features full,ffi -- --cfg docsrs --cfg hyper_unstable_ffi -D broken-intra-doc-links

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ nightly = []
118118
__internal_happy_eyeballs_tests = []
119119

120120
[package.metadata.docs.rs]
121-
features = ["full"]
122-
rustdoc-args = ["--cfg", "docsrs"]
121+
features = ["ffi", "full"]
122+
rustdoc-args = ["--cfg", "docsrs", "--cfg", "hyper_unstable_ffi"]
123123

124124
[package.metadata.playground]
125125
features = ["full"]

src/ffi/body.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use super::task::{hyper_context, hyper_task, hyper_task_return_type, AsTaskType}
1010
use super::{UserDataPointer, HYPER_ITER_CONTINUE};
1111
use crate::body::{Body, Bytes, HttpBody as _};
1212

13+
/// A streaming HTTP body.
1314
pub struct hyper_body(pub(super) Body);
1415

16+
/// A buffer of bytes that is sent or received on a `hyper_body`.
1517
pub struct hyper_buf(pub(super) Bytes);
1618

1719
pub(crate) struct UserBody {

src/ffi/client.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ use super::http_types::{hyper_request, hyper_response};
1010
use super::io::hyper_io;
1111
use super::task::{hyper_executor, hyper_task, hyper_task_return_type, AsTaskType, WeakExec};
1212

13+
/// An options builder to configure an HTTP client connection.
1314
pub struct hyper_clientconn_options {
1415
builder: conn::Builder,
1516
/// Use a `Weak` to prevent cycles.
1617
exec: WeakExec,
1718
}
1819

20+
/// An HTTP client connection handle.
21+
///
22+
/// These are used to send a request on a single connection. It's possible to
23+
/// send multiple requests on a single connection, such as when HTTP/1
24+
/// keep-alive or HTTP/2 is used.
1925
pub struct hyper_clientconn {
2026
tx: conn::SendRequest<crate::Body>,
2127
}

src/ffi/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use libc::size_t;
22

3+
/// A more detailed error object returned by some hyper functions.
34
pub struct hyper_error(crate::Error);
45

6+
/// A return code for many of hyper's methods.
57
#[repr(C)]
68
pub enum hyper_code {
79
/// All is well.

src/ffi/http_types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ use super::HYPER_ITER_CONTINUE;
99
use crate::header::{HeaderName, HeaderValue};
1010
use crate::{Body, HeaderMap, Method, Request, Response, Uri};
1111

12+
/// An HTTP request.
1213
pub struct hyper_request(pub(super) Request<Body>);
1314

15+
/// An HTTP response.
1416
pub struct hyper_response(pub(super) Response<Body>);
1517

18+
/// An HTTP header map.
19+
///
20+
/// These can be part of a request or response.
1621
#[derive(Default)]
1722
pub struct hyper_headers {
1823
pub(super) headers: HeaderMap,

src/ffi/io.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ use tokio::io::{AsyncRead, AsyncWrite};
77

88
use super::task::hyper_context;
99

10+
/// Sentinal value to return from a read or write callback that the operation
11+
/// is pending.
1012
pub const HYPER_IO_PENDING: size_t = 0xFFFFFFFF;
13+
/// Sentinal value to return from a read or write callback that the operation
14+
/// has errored.
1115
pub const HYPER_IO_ERROR: size_t = 0xFFFFFFFE;
1216

1317
type hyper_io_read_callback =
1418
extern "C" fn(*mut c_void, *mut hyper_context<'_>, *mut u8, size_t) -> size_t;
1519
type hyper_io_write_callback =
1620
extern "C" fn(*mut c_void, *mut hyper_context<'_>, *const u8, size_t) -> size_t;
1721

22+
/// An IO object used to represent a socket or similar concept.
1823
pub struct hyper_io {
1924
read: hyper_io_read_callback,
2025
write: hyper_io_write_callback,

src/ffi/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
// We have a lot of c-types in here, stop warning about their names!
22
#![allow(non_camel_case_types)]
3+
// fmt::Debug isn't helpful on FFI types
4+
#![allow(missing_debug_implementations)]
35
// unreachable_pub warns `#[no_mangle] pub extern fn` in private mod.
46
#![allow(unreachable_pub)]
57

8+
//! # hyper C API
9+
//!
10+
//! This part of the documentation describes the C API for hyper. That is, how
11+
//! to *use* the hyper library in C code. This is **not** a regular Rust
12+
//! module, and thus it is not accessible in Rust.
13+
//!
14+
//! ## Unstable
15+
//!
16+
//! The C API of hyper is currently **unstable**, which means it's not part of
17+
//! the semver contract as the rest of the Rust API is. Because of that, it's
18+
//! only accessible if `--cfg hyper_unstable_ffi` is passed to `rustc` when
19+
//! compiling. The easiest way to do that is setting the `RUSTFLAGS`
20+
//! environment variable.
21+
//!
22+
//! ## Building
23+
//!
24+
//! The C API is part of the Rust library, but isn't compiled by default. Using
25+
//! `cargo`, it can be compiled with the following command:
26+
//!
27+
//! ```notrust
28+
//! RUSTFLAGS="--cfg hyper_unstable_ffi" cargo build --features client,http1,http2,ffi
29+
//! ```
30+
631
// We may eventually allow the FFI to be enabled without `client` or `http1`,
732
// that is why we don't auto enable them as `ffi = ["client", "http1"]` in
833
// the `Cargo.toml`.
@@ -29,16 +54,29 @@ mod http_types;
2954
mod io;
3055
mod task;
3156

57+
pub use self::body::*;
58+
pub use self::client::*;
59+
pub use self::error::*;
60+
pub use self::http_types::*;
61+
pub use self::io::*;
62+
pub use self::task::*;
63+
3264
pub(crate) use self::body::UserBody;
3365
pub(crate) use self::http_types::{HeaderCaseMap, ReasonPhrase};
3466

67+
/// Return in iter functions to continue iterating.
3568
pub const HYPER_ITER_CONTINUE: libc::c_int = 0;
69+
/// Return in iter functions to stop iterating.
3670
#[allow(unused)]
3771
pub const HYPER_ITER_BREAK: libc::c_int = 1;
3872

73+
/// An HTTP Version that is unspecified.
3974
pub const HYPER_HTTP_VERSION_NONE: libc::c_int = 0;
75+
/// The HTTP/1.0 version.
4076
pub const HYPER_HTTP_VERSION_1_0: libc::c_int = 10;
77+
/// The HTTP/1.1 version.
4178
pub const HYPER_HTTP_VERSION_1_1: libc::c_int = 11;
79+
/// The HTTP/2 version.
4280
pub const HYPER_HTTP_VERSION_2: libc::c_int = 20;
4381

4482
struct UserDataPointer(*mut std::ffi::c_void);

src/ffi/task.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ use super::UserDataPointer;
1717
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
1818
type BoxAny = Box<dyn AsTaskType + Send + Sync>;
1919

20+
/// Return in a poll function to indicate it was ready.
2021
pub const HYPER_POLL_READY: c_int = 0;
22+
/// Return in a poll function to indicate it is still pending.
23+
///
24+
/// The passed in `hyper_waker` should be registered to wake up the task at
25+
/// some later point.
2126
pub const HYPER_POLL_PENDING: c_int = 1;
27+
/// Return in a poll function indicate an error.
2228
pub const HYPER_POLL_ERROR: c_int = 3;
2329

30+
/// A task executor for `hyper_task`s.
2431
pub struct hyper_executor {
2532
/// The executor of all task futures.
2633
///
@@ -47,6 +54,7 @@ pub(crate) struct WeakExec(Weak<hyper_executor>);
4754

4855
struct ExecWaker(AtomicBool);
4956

57+
/// An async task.
5058
pub struct hyper_task {
5159
future: BoxFuture<BoxAny>,
5260
output: Option<BoxAny>,
@@ -57,12 +65,15 @@ struct TaskFuture {
5765
task: Option<Box<hyper_task>>,
5866
}
5967

68+
/// An async context for a task that contains the related waker.
6069
pub struct hyper_context<'a>(Context<'a>);
6170

71+
/// A waker that is saved and used to waken a pending task.
6272
pub struct hyper_waker {
6373
waker: std::task::Waker,
6474
}
6575

76+
/// A descriptor for what type a `hyper_task` value is.
6677
#[repr(C)]
6778
pub enum hyper_task_return_type {
6879
/// The value of this task is null (does not imply an error).

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub mod service;
8989
pub mod upgrade;
9090

9191
#[cfg(feature = "ffi")]
92-
mod ffi;
92+
pub mod ffi;
9393

9494
cfg_proto! {
9595
mod headers;

0 commit comments

Comments
 (0)