refactor(ffi): Removed need for cbindgen type renames (#2442)

Fixes hyperium/hyper#2428
This commit is contained in:
CfirTsabari
2021-02-23 02:12:06 +02:00
committed by GitHub
parent 0b11eee9bd
commit a60280873b
5 changed files with 52 additions and 57 deletions

View File

@@ -21,7 +21,7 @@ pub const HYPER_POLL_READY: c_int = 0;
pub const HYPER_POLL_PENDING: c_int = 1;
pub const HYPER_POLL_ERROR: c_int = 3;
pub struct Exec {
pub struct hyper_executor {
/// The executor of all task futures.
///
/// There should never be contention on the mutex, as it is only locked
@@ -38,23 +38,23 @@ pub struct Exec {
spawn_queue: Mutex<Vec<TaskFuture>>,
/// This is used to track when a future calls `wake` while we are within
/// `Exec::poll_next`.
/// `hyper_executor::poll_next`.
is_woken: Arc<ExecWaker>,
}
#[derive(Clone)]
pub(crate) struct WeakExec(Weak<Exec>);
pub(crate) struct WeakExec(Weak<hyper_executor>);
struct ExecWaker(AtomicBool);
pub struct Task {
pub struct hyper_task {
future: BoxFuture<BoxAny>,
output: Option<BoxAny>,
userdata: UserDataPointer,
}
struct TaskFuture {
task: Option<Box<Task>>,
task: Option<Box<hyper_task>>,
}
pub struct hyper_context<'a>(Context<'a>);
@@ -85,29 +85,29 @@ pub(crate) trait IntoDynTaskType {
fn into_dyn_task_type(self) -> BoxAny;
}
// ===== impl Exec =====
// ===== impl hyper_executor =====
impl Exec {
fn new() -> Arc<Exec> {
Arc::new(Exec {
impl hyper_executor {
fn new() -> Arc<hyper_executor> {
Arc::new(hyper_executor {
driver: Mutex::new(FuturesUnordered::new()),
spawn_queue: Mutex::new(Vec::new()),
is_woken: Arc::new(ExecWaker(AtomicBool::new(false))),
})
}
pub(crate) fn downgrade(exec: &Arc<Exec>) -> WeakExec {
pub(crate) fn downgrade(exec: &Arc<hyper_executor>) -> WeakExec {
WeakExec(Arc::downgrade(exec))
}
fn spawn(&self, task: Box<Task>) {
fn spawn(&self, task: Box<hyper_task>) {
self.spawn_queue
.lock()
.unwrap()
.push(TaskFuture { task: Some(task) });
}
fn poll_next(&self) -> Option<Box<Task>> {
fn poll_next(&self) -> Option<Box<hyper_task>> {
// Drain the queue first.
self.drain_queue();
@@ -169,21 +169,21 @@ impl WeakExec {
impl crate::rt::Executor<BoxFuture<()>> for WeakExec {
fn execute(&self, fut: BoxFuture<()>) {
if let Some(exec) = self.0.upgrade() {
exec.spawn(Task::boxed(fut));
exec.spawn(hyper_task::boxed(fut));
}
}
}
ffi_fn! {
/// Creates a new task executor.
fn hyper_executor_new() -> *const Exec {
Arc::into_raw(Exec::new())
fn hyper_executor_new() -> *const hyper_executor {
Arc::into_raw(hyper_executor::new())
}
}
ffi_fn! {
/// Frees an executor and any incomplete tasks still part of it.
fn hyper_executor_free(exec: *const Exec) {
fn hyper_executor_free(exec: *const hyper_executor) {
drop(unsafe { Arc::from_raw(exec) });
}
}
@@ -193,7 +193,7 @@ ffi_fn! {
///
/// The executor takes ownership of the task, it should not be accessed
/// again unless returned back to the user with `hyper_executor_poll`.
fn hyper_executor_push(exec: *const Exec, task: *mut Task) -> hyper_code {
fn hyper_executor_push(exec: *const hyper_executor, task: *mut hyper_task) -> hyper_code {
if exec.is_null() || task.is_null() {
return hyper_code::HYPERE_INVALID_ARG;
}
@@ -211,7 +211,7 @@ ffi_fn! {
/// If ready, returns a task from the executor that has completed.
///
/// If there are no ready tasks, this returns `NULL`.
fn hyper_executor_poll(exec: *const Exec) -> *mut Task {
fn hyper_executor_poll(exec: *const hyper_executor) -> *mut hyper_task {
// We only want an `&Arc` in here, so wrap in a `ManuallyDrop` so we
// don't accidentally trigger a ref_dec of the Arc.
let exec = unsafe { &*exec };
@@ -222,15 +222,15 @@ ffi_fn! {
}
}
// ===== impl Task =====
// ===== impl hyper_task =====
impl Task {
pub(crate) fn boxed<F>(fut: F) -> Box<Task>
impl hyper_task {
pub(crate) fn boxed<F>(fut: F) -> Box<hyper_task>
where
F: Future + Send + 'static,
F::Output: IntoDynTaskType + Send + Sync + 'static,
{
Box::new(Task {
Box::new(hyper_task {
future: Box::pin(async move { fut.await.into_dyn_task_type() }),
output: None,
userdata: UserDataPointer(ptr::null_mut()),
@@ -246,7 +246,7 @@ impl Task {
}
impl Future for TaskFuture {
type Output = Box<Task>;
type Output = Box<hyper_task>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match Pin::new(&mut self.task.as_mut().unwrap().future).poll(cx) {
@@ -262,7 +262,7 @@ impl Future for TaskFuture {
ffi_fn! {
/// Free a task.
fn hyper_task_free(task: *mut Task) {
fn hyper_task_free(task: *mut hyper_task) {
drop(unsafe { Box::from_raw(task) });
}
}
@@ -274,7 +274,7 @@ ffi_fn! {
/// this task.
///
/// Use `hyper_task_type` to determine the type of the `void *` return value.
fn hyper_task_value(task: *mut Task) -> *mut c_void {
fn hyper_task_value(task: *mut hyper_task) -> *mut c_void {
if task.is_null() {
return ptr::null_mut();
}
@@ -297,7 +297,7 @@ ffi_fn! {
ffi_fn! {
/// Query the return type of this task.
fn hyper_task_type(task: *mut Task) -> hyper_task_return_type {
fn hyper_task_type(task: *mut hyper_task) -> hyper_task_return_type {
if task.is_null() {
// instead of blowing up spectacularly, just say this null task
// doesn't have a value to retrieve.
@@ -313,7 +313,7 @@ ffi_fn! {
///
/// This value will be passed to task callbacks, and can be checked later
/// with `hyper_task_userdata`.
fn hyper_task_set_userdata(task: *mut Task, userdata: *mut c_void) {
fn hyper_task_set_userdata(task: *mut hyper_task, userdata: *mut c_void) {
if task.is_null() {
return;
}
@@ -324,7 +324,7 @@ ffi_fn! {
ffi_fn! {
/// Retrieve the userdata that has been set via `hyper_task_set_userdata`.
fn hyper_task_userdata(task: *mut Task) -> *mut c_void {
fn hyper_task_userdata(task: *mut hyper_task) -> *mut c_void {
if task.is_null() {
return ptr::null_mut();
}