From 374380842a8256a59111872b3e8d510306935c0a Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 12 Dec 2020 09:40:40 +0100 Subject: [PATCH] ffi: use recommended stable way to represent an opaque C struct After `extern { type ... }` has stabilized for a while, this can be replaced. For now, I used a macro since it is much easier to spot the locations to touch at that time. fixes #1311 --- src/ffi/code.rs | 2 +- src/ffi/mod.rs | 12 +++++++++++- src/ffi/object.rs | 2 +- src/ffi/pyarena.rs | 2 +- src/ffi/pythonrun.rs | 6 +++--- src/ffi/weakrefobject.rs | 2 +- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ffi/code.rs b/src/ffi/code.rs index 25c656ebd25..f516a0bd2ed 100644 --- a/src/ffi/code.rs +++ b/src/ffi/code.rs @@ -3,7 +3,7 @@ use crate::ffi::pyport::Py_ssize_t; use std::os::raw::{c_char, c_int, c_uchar, c_void}; #[cfg(Py_3_8)] -pub enum _PyOpcache {} +opaque_struct!(_PyOpcache); #[repr(C)] #[derive(Copy, Clone)] diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 2edb27685f2..98ce4ea36c4 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -2,6 +2,16 @@ #![cfg_attr(Py_LIMITED_API, allow(unused_imports))] #![cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))] +// Until `extern type` is stabilized, use the recommended approach to +// model opaque types: +// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs +macro_rules! opaque_struct { + ($name:ident) => { + #[repr(C)] + pub struct $name([u8; 0]); + }; +} + pub use self::bltinmodule::*; pub use self::boolobject::*; pub use self::bytearrayobject::*; @@ -165,7 +175,7 @@ pub mod structmember; // TODO supports PEP-384 only; needs adjustment for Python pub mod frameobject; #[cfg(Py_LIMITED_API)] pub mod frameobject { - pub enum PyFrameObject {} + opaque_struct!(PyFrameObject); } pub(crate) mod datetime; diff --git a/src/ffi/object.rs b/src/ffi/object.rs index 99fc3798c47..5cd0aef6640 100644 --- a/src/ffi/object.rs +++ b/src/ffi/object.rs @@ -262,7 +262,7 @@ pub type vectorcallfunc = unsafe extern "C" fn( #[cfg(Py_LIMITED_API)] mod typeobject { - pub enum PyTypeObject {} + opaque_struct!(PyTypeObject); } #[cfg(not(Py_LIMITED_API))] diff --git a/src/ffi/pyarena.rs b/src/ffi/pyarena.rs index bfe42fdb398..87d5f28a7a5 100644 --- a/src/ffi/pyarena.rs +++ b/src/ffi/pyarena.rs @@ -1 +1 @@ -pub enum PyArena {} +opaque_struct!(PyArena); diff --git a/src/ffi/pythonrun.rs b/src/ffi/pythonrun.rs index 51697581399..e403bd028cd 100644 --- a/src/ffi/pythonrun.rs +++ b/src/ffi/pythonrun.rs @@ -15,7 +15,7 @@ pub struct PyCompilerFlags { } #[cfg(not(Py_LIMITED_API))] -pub enum _mod {} +opaque_struct!(_mod); #[cfg(not(Py_LIMITED_API))] extern "C" { @@ -90,8 +90,8 @@ extern "C" { ) -> *mut _mod; } -pub enum symtable {} -pub enum _node {} +opaque_struct!(symtable); +opaque_struct!(_node); #[inline] pub unsafe fn PyParser_SimpleParseString(s: *const c_char, b: c_int) -> *mut _node { diff --git a/src/ffi/weakrefobject.rs b/src/ffi/weakrefobject.rs index b1013152ce8..65ef356253a 100644 --- a/src/ffi/weakrefobject.rs +++ b/src/ffi/weakrefobject.rs @@ -1,7 +1,7 @@ use crate::ffi::object::*; use std::os::raw::c_int; -pub enum PyWeakReference {} +opaque_struct!(PyWeakReference); extern "C" { static mut _PyWeakref_RefType: PyTypeObject;