Skip to content

Commit c5cc1e9

Browse files
committed
fix(ffi): add repr(C) to hyper_context
See mozilla/cbindgen#579 (comment)
1 parent c62ea80 commit c5cc1e9

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

capi/cbindgen.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ sys_includes = ["stdint.h", "stddef.h", "stdbool.h"]
1313
cpp_compat = true
1414
documentation_style = "c"
1515
documentation_length = "short"
16+
17+
[export]
18+
include = ["hyper_context_ffi"]
19+
20+
[export.rename]
21+
hyper_context_ffi = "hyper_context"

capi/include/hyper.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ typedef struct hyper_waker hyper_waker;
194194

195195
typedef int (*hyper_body_foreach_callback)(void*, const struct hyper_buf*);
196196

197-
typedef int (*hyper_body_data_callback)(void*, struct hyper_context*, struct hyper_buf**);
197+
typedef int (*hyper_body_data_callback)(void*, hyper_context*, struct hyper_buf**);
198198

199199
typedef void (*hyper_request_on_informational_callback)(void*, struct hyper_response*);
200200

201201
typedef int (*hyper_headers_foreach_callback)(void*, const uint8_t*, size_t, const uint8_t*, size_t);
202202

203-
typedef size_t (*hyper_io_read_callback)(void*, struct hyper_context*, uint8_t*, size_t);
203+
typedef size_t (*hyper_io_read_callback)(void*, hyper_context*, uint8_t*, size_t);
204204

205-
typedef size_t (*hyper_io_write_callback)(void*, struct hyper_context*, const uint8_t*, size_t);
205+
typedef size_t (*hyper_io_write_callback)(void*, hyper_context*, const uint8_t*, size_t);
206206

207207
#ifdef __cplusplus
208208
extern "C" {
@@ -525,7 +525,7 @@ void *hyper_task_userdata(struct hyper_task *task);
525525
/*
526526
Creates a waker associated with the task context.
527527
*/
528-
struct hyper_waker *hyper_context_waker(struct hyper_context *cx);
528+
struct hyper_waker *hyper_context_waker(hyper_context *cx);
529529

530530
/*
531531
Free a waker.

src/ffi/task.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,20 @@ struct TaskFuture {
127127
/// its only purpose is to provide access to the waker. See `hyper_waker`.
128128
///
129129
/// Corresponding Rust type: <https://doc.rust-lang.org/std/task/struct.Context.html>
130+
///
131+
/// We ignore this type in cbindgen because we want to expose an opaque type. See hyper_context_ffi.
132+
/// cbindgen:ignore
133+
#[repr(C)] // we transmute references
130134
pub struct hyper_context<'a>(Context<'a>);
131135

136+
/// An async context for a task that contains the related waker.
137+
///
138+
/// This creates an opaque type for use in cbindgen. Use
139+
/// `hyper_context` in Rust code.
140+
#[doc(hidden)]
141+
#[allow(dead_code)]
142+
pub struct hyper_context_ffi<'a>(Context<'a>);
143+
132144
/// A waker that is saved and used to waken a pending task.
133145
///
134146
/// This is provided to `hyper_io`'s read and write callbacks via `hyper_context`
@@ -500,7 +512,7 @@ where
500512

501513
impl hyper_context<'_> {
502514
pub(crate) fn wrap<'a, 'b>(cx: &'a mut Context<'b>) -> &'a mut hyper_context<'b> {
503-
// A struct with only one field has the same layout as that field.
515+
// A repr(C) newtype has the same layout as its only field.
504516
unsafe { std::mem::transmute::<&mut Context<'_>, &mut hyper_context<'_>>(cx) }
505517
}
506518
}

src/proto/h2/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ fn h2_to_io_error(e: h2::Error) -> std::io::Error {
384384
}
385385
}
386386

387+
#[repr(transparent)]
387388
struct UpgradedSendStream<B>(SendStream<SendBuf<Neutered<B>>>);
388389

389390
impl<B> UpgradedSendStream<B>

0 commit comments

Comments
 (0)