Skip to content

Commit

Permalink
fix(ffi): add repr(C) to hyper_context
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr3 committed May 5, 2024
1 parent c62ea80 commit c5cc1e9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions capi/cbindgen.toml
Expand Up @@ -13,3 +13,9 @@ sys_includes = ["stdint.h", "stddef.h", "stdbool.h"]
cpp_compat = true
documentation_style = "c"
documentation_length = "short"

[export]
include = ["hyper_context_ffi"]

[export.rename]
hyper_context_ffi = "hyper_context"
8 changes: 4 additions & 4 deletions capi/include/hyper.h
Expand Up @@ -194,15 +194,15 @@ typedef struct hyper_waker hyper_waker;

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

typedef int (*hyper_body_data_callback)(void*, struct hyper_context*, struct hyper_buf**);
typedef int (*hyper_body_data_callback)(void*, hyper_context*, struct hyper_buf**);

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

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

typedef size_t (*hyper_io_read_callback)(void*, struct hyper_context*, uint8_t*, size_t);
typedef size_t (*hyper_io_read_callback)(void*, hyper_context*, uint8_t*, size_t);

typedef size_t (*hyper_io_write_callback)(void*, struct hyper_context*, const uint8_t*, size_t);
typedef size_t (*hyper_io_write_callback)(void*, hyper_context*, const uint8_t*, size_t);

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -525,7 +525,7 @@ void *hyper_task_userdata(struct hyper_task *task);
/*
Creates a waker associated with the task context.
*/
struct hyper_waker *hyper_context_waker(struct hyper_context *cx);
struct hyper_waker *hyper_context_waker(hyper_context *cx);

/*
Free a waker.
Expand Down
14 changes: 13 additions & 1 deletion src/ffi/task.rs
Expand Up @@ -127,8 +127,20 @@ struct TaskFuture {
/// its only purpose is to provide access to the waker. See `hyper_waker`.
///
/// Corresponding Rust type: <https://doc.rust-lang.org/std/task/struct.Context.html>
///
/// We ignore this type in cbindgen because we want to expose an opaque type. See hyper_context_ffi.
/// cbindgen:ignore
#[repr(C)] // we transmute references
pub struct hyper_context<'a>(Context<'a>);

/// An async context for a task that contains the related waker.
///
/// This creates an opaque type for use in cbindgen. Use
/// `hyper_context` in Rust code.
#[doc(hidden)]
#[allow(dead_code)]
pub struct hyper_context_ffi<'a>(Context<'a>);

/// A waker that is saved and used to waken a pending task.
///
/// This is provided to `hyper_io`'s read and write callbacks via `hyper_context`
Expand Down Expand Up @@ -500,7 +512,7 @@ where

impl hyper_context<'_> {
pub(crate) fn wrap<'a, 'b>(cx: &'a mut Context<'b>) -> &'a mut hyper_context<'b> {
// A struct with only one field has the same layout as that field.
// A repr(C) newtype has the same layout as its only field.
unsafe { std::mem::transmute::<&mut Context<'_>, &mut hyper_context<'_>>(cx) }
}
}
Expand Down
1 change: 1 addition & 0 deletions src/proto/h2/mod.rs
Expand Up @@ -384,6 +384,7 @@ fn h2_to_io_error(e: h2::Error) -> std::io::Error {
}
}

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

impl<B> UpgradedSendStream<B>
Expand Down

0 comments on commit c5cc1e9

Please sign in to comment.