Skip to content

Commit

Permalink
chore(dependencies): remove libc crate from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Oct 29, 2023
1 parent 8eb2cff commit 21eab01
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 43 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ http-body-util = { version = "=0.1.0-rc.3", optional = true }
httparse = { version = "1.8", optional = true }
httpdate = { version = "1.0", optional = true }
itoa = { version = "1", optional = true }
libc = { version = "0.2", optional = true }
tracing = { version = "0.1", default-features = false, features = ["std"], optional = true }
want = { version = "0.3", optional = true }

Expand Down Expand Up @@ -82,7 +81,7 @@ client = ["dep:want"]
server = ["dep:httpdate"]

# C-API support (currently unstable (no semver))
ffi = ["dep:libc", "dep:http-body-util"]
ffi = ["dep:http-body-util"]

# Utilize tracing (currently unstable)
tracing = ["dep:tracing"]
Expand Down
8 changes: 4 additions & 4 deletions src/ffi/body.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::ffi::c_void;
use std::ffi::{c_int, c_void};
use std::mem::ManuallyDrop;
use std::ptr;
use std::task::{Context, Poll};

use http_body_util::BodyExt as _;
use libc::{c_int, size_t};

use super::task::{hyper_context, hyper_task, hyper_task_return_type, AsTaskType};
use super::{UserDataPointer, HYPER_ITER_CONTINUE};
use crate::body::{Bytes, Frame, Incoming as IncomingBody};
use crate::ffi::c_size_t;

/// A streaming HTTP body.
pub struct hyper_body(pub(super) IncomingBody);
Expand Down Expand Up @@ -214,7 +214,7 @@ ffi_fn! {
/// `hyper_buf_free`.
///
/// This returns `NULL` if allocating a new buffer fails.
fn hyper_buf_copy(buf: *const u8, len: size_t) -> *mut hyper_buf {
fn hyper_buf_copy(buf: *const u8, len: c_size_t) -> *mut hyper_buf {
let slice = unsafe {
std::slice::from_raw_parts(buf, len)
};
Expand All @@ -237,7 +237,7 @@ ffi_fn! {

ffi_fn! {
/// Get the length of the bytes this buffer contains.
fn hyper_buf_len(buf: *const hyper_buf) -> size_t {
fn hyper_buf_len(buf: *const hyper_buf) -> c_size_t {
unsafe { (*buf).0.len() }
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/ffi/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::ffi::c_int;
use std::ptr;
use std::sync::Arc;

use libc::c_int;

use crate::client::conn;
use crate::rt::Executor as _;

Expand Down
4 changes: 2 additions & 2 deletions src/ffi/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libc::size_t;
use crate::ffi::c_size_t;

/// A more detailed error object returned by some hyper functions.
pub struct hyper_error(crate::Error);
Expand Down Expand Up @@ -78,7 +78,7 @@ ffi_fn! {
/// store.
///
/// The return value is number of bytes that were written to `dst`.
fn hyper_error_print(err: *const hyper_error, dst: *mut u8, dst_len: size_t) -> size_t {
fn hyper_error_print(err: *const hyper_error, dst: *mut u8, dst_len: c_size_t) -> c_size_t {
let dst = unsafe {
std::slice::from_raw_parts_mut(dst, dst_len)
};
Expand Down
27 changes: 14 additions & 13 deletions src/ffi/http_types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::ffi::{c_int, c_void};

use bytes::Bytes;
use libc::{c_int, size_t};
use std::ffi::c_void;

use super::body::hyper_body;
use super::error::hyper_code;
use super::task::{hyper_task_return_type, AsTaskType};
use super::{UserDataPointer, HYPER_ITER_CONTINUE};
use crate::body::Incoming as IncomingBody;
use crate::ext::{HeaderCaseMap, OriginalHeaderOrder, ReasonPhrase};
use crate::ffi::c_size_t;
use crate::header::{HeaderName, HeaderValue};
use crate::{HeaderMap, Method, Request, Response, Uri};

Expand Down Expand Up @@ -57,7 +58,7 @@ ffi_fn! {

ffi_fn! {
/// Set the HTTP Method of the request.
fn hyper_request_set_method(req: *mut hyper_request, method: *const u8, method_len: size_t) -> hyper_code {
fn hyper_request_set_method(req: *mut hyper_request, method: *const u8, method_len: c_size_t) -> hyper_code {
let bytes = unsafe {
std::slice::from_raw_parts(method, method_len as usize)
};
Expand Down Expand Up @@ -89,7 +90,7 @@ ffi_fn! {
/// have been parsed/set are sent when the connection type is HTTP/2.
///
/// To set each slot explicitly, use `hyper_request_set_uri_parts`.
fn hyper_request_set_uri(req: *mut hyper_request, uri: *const u8, uri_len: size_t) -> hyper_code {
fn hyper_request_set_uri(req: *mut hyper_request, uri: *const u8, uri_len: c_size_t) -> hyper_code {
let bytes = unsafe {
std::slice::from_raw_parts(uri, uri_len as usize)
};
Expand Down Expand Up @@ -117,11 +118,11 @@ ffi_fn! {
fn hyper_request_set_uri_parts(
req: *mut hyper_request,
scheme: *const u8,
scheme_len: size_t,
scheme_len: c_size_t,
authority: *const u8,
authority_len: size_t,
authority_len: c_size_t,
path_and_query: *const u8,
path_and_query_len: size_t
path_and_query_len: c_size_t
) -> hyper_code {
let mut builder = Uri::builder();
if !scheme.is_null() {
Expand Down Expand Up @@ -280,7 +281,7 @@ ffi_fn! {
/// Get the length of the reason-phrase of this response.
///
/// Use `hyper_response_reason_phrase()` to get the buffer pointer.
fn hyper_response_reason_phrase_len(resp: *const hyper_response) -> size_t {
fn hyper_response_reason_phrase_len(resp: *const hyper_response) -> c_size_t {
non_null!(&*resp ?= 0).reason_phrase().len()
}
}
Expand Down Expand Up @@ -371,7 +372,7 @@ unsafe impl AsTaskType for hyper_response {
// ===== impl Headers =====

type hyper_headers_foreach_callback =
extern "C" fn(*mut c_void, *const u8, size_t, *const u8, size_t) -> c_int;
extern "C" fn(*mut c_void, *const u8, c_size_t, *const u8, c_size_t) -> c_int;

impl hyper_headers {
pub(super) fn get_or_default(ext: &mut http::Extensions) -> &mut hyper_headers {
Expand Down Expand Up @@ -453,7 +454,7 @@ ffi_fn! {
/// Sets the header with the provided name to the provided value.
///
/// This overwrites any previous value set for the header.
fn hyper_headers_set(headers: *mut hyper_headers, name: *const u8, name_len: size_t, value: *const u8, value_len: size_t) -> hyper_code {
fn hyper_headers_set(headers: *mut hyper_headers, name: *const u8, name_len: c_size_t, value: *const u8, value_len: c_size_t) -> hyper_code {
let headers = non_null!(&mut *headers ?= hyper_code::HYPERE_INVALID_ARG);
match unsafe { raw_name_value(name, name_len, value, value_len) } {
Ok((name, value, orig_name)) => {
Expand All @@ -472,7 +473,7 @@ ffi_fn! {
///
/// If there were already existing values for the name, this will append the
/// new value to the internal list.
fn hyper_headers_add(headers: *mut hyper_headers, name: *const u8, name_len: size_t, value: *const u8, value_len: size_t) -> hyper_code {
fn hyper_headers_add(headers: *mut hyper_headers, name: *const u8, name_len: c_size_t, value: *const u8, value_len: c_size_t) -> hyper_code {
let headers = non_null!(&mut *headers ?= hyper_code::HYPERE_INVALID_ARG);

match unsafe { raw_name_value(name, name_len, value, value_len) } {
Expand All @@ -499,9 +500,9 @@ impl Default for hyper_headers {

unsafe fn raw_name_value(
name: *const u8,
name_len: size_t,
name_len: c_size_t,
value: *const u8,
value_len: size_t,
value_len: c_size_t,
) -> Result<(HeaderName, HeaderValue, Bytes), hyper_code> {
let name = std::slice::from_raw_parts(name, name_len);
let orig_name = Bytes::copy_from_slice(name);
Expand Down
21 changes: 10 additions & 11 deletions src/ffi/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ use std::ffi::c_void;
use std::pin::Pin;
use std::task::{Context, Poll};

use crate::rt::{Read, Write};
use libc::size_t;

use super::task::hyper_context;
use crate::ffi::c_size_t;
use crate::rt::{Read, Write};

/// Sentinel value to return from a read or write callback that the operation
/// is pending.
pub const HYPER_IO_PENDING: size_t = 0xFFFFFFFF;
pub const HYPER_IO_PENDING: c_size_t = 0xFFFFFFFF;
/// Sentinel value to return from a read or write callback that the operation
/// has errored.
pub const HYPER_IO_ERROR: size_t = 0xFFFFFFFE;
pub const HYPER_IO_ERROR: c_size_t = 0xFFFFFFFE;

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

/// An IO object used to represent a socket or similar concept.
pub struct hyper_io {
Expand Down Expand Up @@ -108,8 +107,8 @@ extern "C" fn read_noop(
_userdata: *mut c_void,
_: *mut hyper_context<'_>,
_buf: *mut u8,
_buf_len: size_t,
) -> size_t {
_buf_len: c_size_t,
) -> c_size_t {
0
}

Expand All @@ -118,8 +117,8 @@ extern "C" fn write_noop(
_userdata: *mut c_void,
_: *mut hyper_context<'_>,
_buf: *const u8,
_buf_len: size_t,
) -> size_t {
_buf_len: c_size_t,
) -> c_size_t {
0
}

Expand Down
18 changes: 11 additions & 7 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ pub use self::io::*;
pub use self::task::*;

/// Return in iter functions to continue iterating.
pub const HYPER_ITER_CONTINUE: libc::c_int = 0;
pub const HYPER_ITER_CONTINUE: std::ffi::c_int = 0;
/// Return in iter functions to stop iterating.
#[allow(unused)]
pub const HYPER_ITER_BREAK: libc::c_int = 1;
pub const HYPER_ITER_BREAK: std::ffi::c_int = 1;

/// An HTTP Version that is unspecified.
pub const HYPER_HTTP_VERSION_NONE: libc::c_int = 0;
pub const HYPER_HTTP_VERSION_NONE: std::ffi::c_int = 0;
/// The HTTP/1.0 version.
pub const HYPER_HTTP_VERSION_1_0: libc::c_int = 10;
pub const HYPER_HTTP_VERSION_1_0: std::ffi::c_int = 10;
/// The HTTP/1.1 version.
pub const HYPER_HTTP_VERSION_1_1: libc::c_int = 11;
pub const HYPER_HTTP_VERSION_1_1: std::ffi::c_int = 11;
/// The HTTP/2 version.
pub const HYPER_HTTP_VERSION_2: libc::c_int = 20;
pub const HYPER_HTTP_VERSION_2: std::ffi::c_int = 20;

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

Expand All @@ -86,9 +86,13 @@ unsafe impl Sync for UserDataPointer {}
/// cbindgen:ignore
static VERSION_CSTR: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");

// `core::ffi::c_size_t` is a nightly-only experimental API.
// https://github.com/rust-lang/rust/issues/88345
type c_size_t = usize;

ffi_fn! {
/// Returns a static ASCII (null terminated) string of the hyper version.
fn hyper_version() -> *const libc::c_char {
fn hyper_version() -> *const std::ffi::c_char {
VERSION_CSTR.as_ptr() as _
} ?= std::ptr::null()
}
3 changes: 1 addition & 2 deletions src/ffi/task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_void;
use std::ffi::{c_int, c_void};
use std::future::Future;
use std::pin::Pin;
use std::ptr;
Expand All @@ -9,7 +9,6 @@ use std::sync::{
use std::task::{Context, Poll};

use futures_util::stream::{FuturesUnordered, Stream};
use libc::c_int;

use super::error::hyper_code;
use super::UserDataPointer;
Expand Down

0 comments on commit 21eab01

Please sign in to comment.