Skip to content

Commit a602808

Browse files
authored
refactor(ffi): Removed need for cbindgen type renames (#2442)
Fixes #2428
1 parent 0b11eee commit a602808

File tree

5 files changed

+52
-57
lines changed

5 files changed

+52
-57
lines changed

capi/cbindgen.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@ documentation_style = "c"
77

88
[parse.expand]
99
crates = ["hyper-capi"]
10-
11-
[export.rename]
12-
"Exec" = "hyper_executor"
13-
"Io" = "hyper_io"
14-
"Task" = "hyper_task"

src/ffi/body.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::task::{Context, Poll};
66
use http::HeaderMap;
77
use libc::{c_int, size_t};
88

9-
use super::task::{hyper_context, hyper_task_return_type, AsTaskType, Task};
9+
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

@@ -57,11 +57,11 @@ ffi_fn! {
5757
///
5858
/// This does not consume the `hyper_body *`, so it may be used to again.
5959
/// However, it MUST NOT be used or freed until the related task completes.
60-
fn hyper_body_data(body: *mut hyper_body) -> *mut Task {
60+
fn hyper_body_data(body: *mut hyper_body) -> *mut hyper_task {
6161
// This doesn't take ownership of the Body, so don't allow destructor
6262
let mut body = ManuallyDrop::new(unsafe { Box::from_raw(body) });
6363

64-
Box::into_raw(Task::boxed(async move {
64+
Box::into_raw(hyper_task::boxed(async move {
6565
body.0.data().await.map(|res| res.map(hyper_buf))
6666
}))
6767
}
@@ -78,15 +78,15 @@ ffi_fn! {
7878
/// chunks as they are received, or `HYPER_ITER_BREAK` to cancel.
7979
///
8080
/// This will consume the `hyper_body *`, you shouldn't use it anymore or free it.
81-
fn hyper_body_foreach(body: *mut hyper_body, func: hyper_body_foreach_callback, userdata: *mut c_void) -> *mut Task {
81+
fn hyper_body_foreach(body: *mut hyper_body, func: hyper_body_foreach_callback, userdata: *mut c_void) -> *mut hyper_task {
8282
if body.is_null() {
8383
return ptr::null_mut();
8484
}
8585

8686
let mut body = unsafe { Box::from_raw(body) };
8787
let userdata = UserDataPointer(userdata);
8888

89-
Box::into_raw(Task::boxed(async move {
89+
Box::into_raw(hyper_task::boxed(async move {
9090
while let Some(item) = body.0.data().await {
9191
let chunk = item?;
9292
if HYPER_ITER_CONTINUE != func(userdata.0, &hyper_buf(chunk)) {

src/ffi/client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::rt::Executor as _;
77

88
use super::error::hyper_code;
99
use super::http_types::{hyper_request, hyper_response};
10-
use super::io::Io;
11-
use super::task::{hyper_task_return_type, AsTaskType, Exec, Task, WeakExec};
10+
use super::io::hyper_io;
11+
use super::task::{hyper_executor, hyper_task, hyper_task_return_type, AsTaskType, WeakExec};
1212

1313
pub struct hyper_clientconn_options {
1414
builder: conn::Builder,
@@ -30,7 +30,7 @@ ffi_fn! {
3030
///
3131
/// The returned `hyper_task *` must be polled with an executor until the
3232
/// handshake completes, at which point the value can be taken.
33-
fn hyper_clientconn_handshake(io: *mut Io, options: *mut hyper_clientconn_options) -> *mut Task {
33+
fn hyper_clientconn_handshake(io: *mut hyper_io, options: *mut hyper_clientconn_options) -> *mut hyper_task {
3434
if io.is_null() {
3535
return std::ptr::null_mut();
3636
}
@@ -41,7 +41,7 @@ ffi_fn! {
4141
let options = unsafe { Box::from_raw(options) };
4242
let io = unsafe { Box::from_raw(io) };
4343

44-
Box::into_raw(Task::boxed(async move {
44+
Box::into_raw(hyper_task::boxed(async move {
4545
options.builder.handshake::<_, crate::Body>(io)
4646
.await
4747
.map(|(tx, conn)| {
@@ -59,7 +59,7 @@ ffi_fn! {
5959
///
6060
/// Returns a task that needs to be polled until it is ready. When ready, the
6161
/// task yields a `hyper_response *`.
62-
fn hyper_clientconn_send(conn: *mut hyper_clientconn, req: *mut hyper_request) -> *mut Task {
62+
fn hyper_clientconn_send(conn: *mut hyper_clientconn, req: *mut hyper_request) -> *mut hyper_task {
6363
if conn.is_null() {
6464
return std::ptr::null_mut();
6565
}
@@ -78,7 +78,7 @@ ffi_fn! {
7878
fut.await.map(hyper_response::wrap)
7979
};
8080

81-
Box::into_raw(Task::boxed(fut))
81+
Box::into_raw(hyper_task::boxed(fut))
8282
}
8383
}
8484

@@ -118,11 +118,11 @@ ffi_fn! {
118118
/// Set the client background task executor.
119119
///
120120
/// This does not consume the `options` or the `exec`.
121-
fn hyper_clientconn_options_exec(opts: *mut hyper_clientconn_options, exec: *const Exec) {
121+
fn hyper_clientconn_options_exec(opts: *mut hyper_clientconn_options, exec: *const hyper_executor) {
122122
let opts = unsafe { &mut *opts };
123123

124124
let exec = unsafe { Arc::from_raw(exec) };
125-
let weak_exec = Exec::downgrade(&exec);
125+
let weak_exec = hyper_executor::downgrade(&exec);
126126
std::mem::forget(exec);
127127

128128
opts.builder.executor(weak_exec.clone());

src/ffi/io.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type hyper_io_read_callback =
1515
type hyper_io_write_callback =
1616
extern "C" fn(*mut c_void, *mut hyper_context<'_>, *const u8, size_t) -> size_t;
1717

18-
pub struct Io {
18+
pub struct hyper_io {
1919
read: hyper_io_read_callback,
2020
write: hyper_io_write_callback,
2121
userdata: *mut c_void,
@@ -26,8 +26,8 @@ ffi_fn! {
2626
///
2727
/// The read and write functions of this transport should be set with
2828
/// `hyper_io_set_read` and `hyper_io_set_write`.
29-
fn hyper_io_new() -> *mut Io {
30-
Box::into_raw(Box::new(Io {
29+
fn hyper_io_new() -> *mut hyper_io {
30+
Box::into_raw(Box::new(hyper_io {
3131
read: read_noop,
3232
write: write_noop,
3333
userdata: std::ptr::null_mut(),
@@ -40,7 +40,7 @@ ffi_fn! {
4040
///
4141
/// This is typically only useful if you aren't going to pass ownership
4242
/// of the IO handle to hyper, such as with `hyper_clientconn_handshake()`.
43-
fn hyper_io_free(io: *mut Io) {
43+
fn hyper_io_free(io: *mut hyper_io) {
4444
drop(unsafe { Box::from_raw(io) });
4545
}
4646
}
@@ -49,7 +49,7 @@ ffi_fn! {
4949
/// Set the user data pointer for this IO to some value.
5050
///
5151
/// This value is passed as an argument to the read and write callbacks.
52-
fn hyper_io_set_userdata(io: *mut Io, data: *mut c_void) {
52+
fn hyper_io_set_userdata(io: *mut hyper_io, data: *mut c_void) {
5353
unsafe { &mut *io }.userdata = data;
5454
}
5555
}
@@ -71,7 +71,7 @@ ffi_fn! {
7171
///
7272
/// If there is an irrecoverable error reading data, then `HYPER_IO_ERROR`
7373
/// should be the return value.
74-
fn hyper_io_set_read(io: *mut Io, func: hyper_io_read_callback) {
74+
fn hyper_io_set_read(io: *mut hyper_io, func: hyper_io_read_callback) {
7575
unsafe { &mut *io }.read = func;
7676
}
7777
}
@@ -90,7 +90,7 @@ ffi_fn! {
9090
///
9191
/// If there is an irrecoverable error reading data, then `HYPER_IO_ERROR`
9292
/// should be the return value.
93-
fn hyper_io_set_write(io: *mut Io, func: hyper_io_write_callback) {
93+
fn hyper_io_set_write(io: *mut hyper_io, func: hyper_io_write_callback) {
9494
unsafe { &mut *io }.write = func;
9595
}
9696
}
@@ -115,7 +115,7 @@ extern "C" fn write_noop(
115115
0
116116
}
117117

118-
impl AsyncRead for Io {
118+
impl AsyncRead for hyper_io {
119119
fn poll_read(
120120
self: Pin<&mut Self>,
121121
cx: &mut Context<'_>,
@@ -141,7 +141,7 @@ impl AsyncRead for Io {
141141
}
142142
}
143143

144-
impl AsyncWrite for Io {
144+
impl AsyncWrite for hyper_io {
145145
fn poll_write(
146146
self: Pin<&mut Self>,
147147
cx: &mut Context<'_>,
@@ -169,5 +169,5 @@ impl AsyncWrite for Io {
169169
}
170170
}
171171

172-
unsafe impl Send for Io {}
173-
unsafe impl Sync for Io {}
172+
unsafe impl Send for hyper_io {}
173+
unsafe impl Sync for hyper_io {}

src/ffi/task.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub const HYPER_POLL_READY: c_int = 0;
2121
pub const HYPER_POLL_PENDING: c_int = 1;
2222
pub const HYPER_POLL_ERROR: c_int = 3;
2323

24-
pub struct Exec {
24+
pub struct hyper_executor {
2525
/// The executor of all task futures.
2626
///
2727
/// There should never be contention on the mutex, as it is only locked
@@ -38,23 +38,23 @@ pub struct Exec {
3838
spawn_queue: Mutex<Vec<TaskFuture>>,
3939

4040
/// This is used to track when a future calls `wake` while we are within
41-
/// `Exec::poll_next`.
41+
/// `hyper_executor::poll_next`.
4242
is_woken: Arc<ExecWaker>,
4343
}
4444

4545
#[derive(Clone)]
46-
pub(crate) struct WeakExec(Weak<Exec>);
46+
pub(crate) struct WeakExec(Weak<hyper_executor>);
4747

4848
struct ExecWaker(AtomicBool);
4949

50-
pub struct Task {
50+
pub struct hyper_task {
5151
future: BoxFuture<BoxAny>,
5252
output: Option<BoxAny>,
5353
userdata: UserDataPointer,
5454
}
5555

5656
struct TaskFuture {
57-
task: Option<Box<Task>>,
57+
task: Option<Box<hyper_task>>,
5858
}
5959

6060
pub struct hyper_context<'a>(Context<'a>);
@@ -85,29 +85,29 @@ pub(crate) trait IntoDynTaskType {
8585
fn into_dyn_task_type(self) -> BoxAny;
8686
}
8787

88-
// ===== impl Exec =====
88+
// ===== impl hyper_executor =====
8989

90-
impl Exec {
91-
fn new() -> Arc<Exec> {
92-
Arc::new(Exec {
90+
impl hyper_executor {
91+
fn new() -> Arc<hyper_executor> {
92+
Arc::new(hyper_executor {
9393
driver: Mutex::new(FuturesUnordered::new()),
9494
spawn_queue: Mutex::new(Vec::new()),
9595
is_woken: Arc::new(ExecWaker(AtomicBool::new(false))),
9696
})
9797
}
9898

99-
pub(crate) fn downgrade(exec: &Arc<Exec>) -> WeakExec {
99+
pub(crate) fn downgrade(exec: &Arc<hyper_executor>) -> WeakExec {
100100
WeakExec(Arc::downgrade(exec))
101101
}
102102

103-
fn spawn(&self, task: Box<Task>) {
103+
fn spawn(&self, task: Box<hyper_task>) {
104104
self.spawn_queue
105105
.lock()
106106
.unwrap()
107107
.push(TaskFuture { task: Some(task) });
108108
}
109109

110-
fn poll_next(&self) -> Option<Box<Task>> {
110+
fn poll_next(&self) -> Option<Box<hyper_task>> {
111111
// Drain the queue first.
112112
self.drain_queue();
113113

@@ -169,21 +169,21 @@ impl WeakExec {
169169
impl crate::rt::Executor<BoxFuture<()>> for WeakExec {
170170
fn execute(&self, fut: BoxFuture<()>) {
171171
if let Some(exec) = self.0.upgrade() {
172-
exec.spawn(Task::boxed(fut));
172+
exec.spawn(hyper_task::boxed(fut));
173173
}
174174
}
175175
}
176176

177177
ffi_fn! {
178178
/// Creates a new task executor.
179-
fn hyper_executor_new() -> *const Exec {
180-
Arc::into_raw(Exec::new())
179+
fn hyper_executor_new() -> *const hyper_executor {
180+
Arc::into_raw(hyper_executor::new())
181181
}
182182
}
183183

184184
ffi_fn! {
185185
/// Frees an executor and any incomplete tasks still part of it.
186-
fn hyper_executor_free(exec: *const Exec) {
186+
fn hyper_executor_free(exec: *const hyper_executor) {
187187
drop(unsafe { Arc::from_raw(exec) });
188188
}
189189
}
@@ -193,7 +193,7 @@ ffi_fn! {
193193
///
194194
/// The executor takes ownership of the task, it should not be accessed
195195
/// again unless returned back to the user with `hyper_executor_poll`.
196-
fn hyper_executor_push(exec: *const Exec, task: *mut Task) -> hyper_code {
196+
fn hyper_executor_push(exec: *const hyper_executor, task: *mut hyper_task) -> hyper_code {
197197
if exec.is_null() || task.is_null() {
198198
return hyper_code::HYPERE_INVALID_ARG;
199199
}
@@ -211,7 +211,7 @@ ffi_fn! {
211211
/// If ready, returns a task from the executor that has completed.
212212
///
213213
/// If there are no ready tasks, this returns `NULL`.
214-
fn hyper_executor_poll(exec: *const Exec) -> *mut Task {
214+
fn hyper_executor_poll(exec: *const hyper_executor) -> *mut hyper_task {
215215
// We only want an `&Arc` in here, so wrap in a `ManuallyDrop` so we
216216
// don't accidentally trigger a ref_dec of the Arc.
217217
let exec = unsafe { &*exec };
@@ -222,15 +222,15 @@ ffi_fn! {
222222
}
223223
}
224224

225-
// ===== impl Task =====
225+
// ===== impl hyper_task =====
226226

227-
impl Task {
228-
pub(crate) fn boxed<F>(fut: F) -> Box<Task>
227+
impl hyper_task {
228+
pub(crate) fn boxed<F>(fut: F) -> Box<hyper_task>
229229
where
230230
F: Future + Send + 'static,
231231
F::Output: IntoDynTaskType + Send + Sync + 'static,
232232
{
233-
Box::new(Task {
233+
Box::new(hyper_task {
234234
future: Box::pin(async move { fut.await.into_dyn_task_type() }),
235235
output: None,
236236
userdata: UserDataPointer(ptr::null_mut()),
@@ -246,7 +246,7 @@ impl Task {
246246
}
247247

248248
impl Future for TaskFuture {
249-
type Output = Box<Task>;
249+
type Output = Box<hyper_task>;
250250

251251
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
252252
match Pin::new(&mut self.task.as_mut().unwrap().future).poll(cx) {
@@ -262,7 +262,7 @@ impl Future for TaskFuture {
262262

263263
ffi_fn! {
264264
/// Free a task.
265-
fn hyper_task_free(task: *mut Task) {
265+
fn hyper_task_free(task: *mut hyper_task) {
266266
drop(unsafe { Box::from_raw(task) });
267267
}
268268
}
@@ -274,7 +274,7 @@ ffi_fn! {
274274
/// this task.
275275
///
276276
/// Use `hyper_task_type` to determine the type of the `void *` return value.
277-
fn hyper_task_value(task: *mut Task) -> *mut c_void {
277+
fn hyper_task_value(task: *mut hyper_task) -> *mut c_void {
278278
if task.is_null() {
279279
return ptr::null_mut();
280280
}
@@ -297,7 +297,7 @@ ffi_fn! {
297297

298298
ffi_fn! {
299299
/// Query the return type of this task.
300-
fn hyper_task_type(task: *mut Task) -> hyper_task_return_type {
300+
fn hyper_task_type(task: *mut hyper_task) -> hyper_task_return_type {
301301
if task.is_null() {
302302
// instead of blowing up spectacularly, just say this null task
303303
// doesn't have a value to retrieve.
@@ -313,7 +313,7 @@ ffi_fn! {
313313
///
314314
/// This value will be passed to task callbacks, and can be checked later
315315
/// with `hyper_task_userdata`.
316-
fn hyper_task_set_userdata(task: *mut Task, userdata: *mut c_void) {
316+
fn hyper_task_set_userdata(task: *mut hyper_task, userdata: *mut c_void) {
317317
if task.is_null() {
318318
return;
319319
}
@@ -324,7 +324,7 @@ ffi_fn! {
324324

325325
ffi_fn! {
326326
/// Retrieve the userdata that has been set via `hyper_task_set_userdata`.
327-
fn hyper_task_userdata(task: *mut Task) -> *mut c_void {
327+
fn hyper_task_userdata(task: *mut hyper_task) -> *mut c_void {
328328
if task.is_null() {
329329
return ptr::null_mut();
330330
}

0 commit comments

Comments
 (0)