hyper/ffi/mod.rs
1// We have a lot of c-types in here, stop warning about their names!
2#![allow(non_camel_case_types)]
3// fmt::Debug isn't helpful on FFI types
4#![allow(missing_debug_implementations)]
5// unreachable_pub warns `#[no_mangle] pub extern fn` in private mod.
6#![allow(unreachable_pub)]
7
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`, staring with `1.64.0`, it can be compiled with the following command:
26//!
27//! ```notrust
28//! RUSTFLAGS="--cfg hyper_unstable_ffi" cargo rustc --crate-type cdylib --features client,http1,http2,ffi
29//! ```
30
31// We may eventually allow the FFI to be enabled without `client` or `http1`,
32// that is why we don't auto enable them as `ffi = ["client", "http1"]` in
33// the `Cargo.toml`.
34//
35// But for now, give a clear message that this compile error is expected.
36#[cfg(not(all(feature = "client", feature = "http1")))]
37compile_error!("The `ffi` feature currently requires the `client` and `http1` features.");
38
39#[cfg(not(hyper_unstable_ffi))]
40compile_error!(
41 "\
42 The `ffi` feature is unstable, and requires the \
43 `RUSTFLAGS='--cfg hyper_unstable_ffi'` environment variable to be set.\
44"
45);
46
47#[macro_use]
48mod macros;
49
50mod body;
51mod client;
52mod error;
53mod http_types;
54mod io;
55mod task;
56
57pub use self::body::*;
58pub use self::client::*;
59pub use self::error::*;
60pub use self::http_types::*;
61pub use self::io::*;
62pub use self::task::*;
63
64/// Return in iter functions to continue iterating.
65pub const HYPER_ITER_CONTINUE: std::ffi::c_int = 0;
66/// Return in iter functions to stop iterating.
67#[allow(unused)]
68pub const HYPER_ITER_BREAK: std::ffi::c_int = 1;
69
70/// An HTTP Version that is unspecified.
71pub const HYPER_HTTP_VERSION_NONE: std::ffi::c_int = 0;
72/// The HTTP/1.0 version.
73pub const HYPER_HTTP_VERSION_1_0: std::ffi::c_int = 10;
74/// The HTTP/1.1 version.
75pub const HYPER_HTTP_VERSION_1_1: std::ffi::c_int = 11;
76/// The HTTP/2 version.
77pub const HYPER_HTTP_VERSION_2: std::ffi::c_int = 20;
78
79#[derive(Clone)]
80struct UserDataPointer(*mut std::ffi::c_void);
81
82// We don't actually know anything about this pointer, it's up to the user
83// to do the right thing.
84unsafe impl Send for UserDataPointer {}
85unsafe impl Sync for UserDataPointer {}
86
87/// cbindgen:ignore
88static VERSION_CSTR: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
89
90// `core::ffi::c_size_t` is a nightly-only experimental API.
91// https://github.com/rust-lang/rust/issues/88345
92type size_t = usize;
93
94ffi_fn! {
95 /// Returns a static ASCII (null terminated) string of the hyper version.
96 fn hyper_version() -> *const std::ffi::c_char {
97 VERSION_CSTR.as_ptr() as _
98 } ?= std::ptr::null()
99}