Skip to content

Commit cc12e74

Browse files
committed
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
1 parent 9aa70f7 commit cc12e74

File tree

6 files changed

+18
-8
lines changed

6 files changed

+18
-8
lines changed

src/ffi/code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ffi::pyport::Py_ssize_t;
33
use std::os::raw::{c_char, c_int, c_uchar, c_void};
44

55
#[cfg(Py_3_8)]
6-
pub enum _PyOpcache {}
6+
opqque_struct!(_PyOpcache);
77

88
#[repr(C)]
99
#[derive(Copy, Clone)]

src/ffi/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
#![cfg_attr(Py_LIMITED_API, allow(unused_imports))]
33
#![cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
44

5+
// Until `extern type` is stabilized, use the recommended approach to
6+
// model opaque types:
7+
// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
8+
macro_rules! opaque_struct {
9+
($name:ident) => {
10+
#[repr(C)]
11+
pub struct $name([u8; 0]);
12+
};
13+
}
14+
515
pub use self::bltinmodule::*;
616
pub use self::boolobject::*;
717
pub use self::bytearrayobject::*;
@@ -165,7 +175,7 @@ pub mod structmember; // TODO supports PEP-384 only; needs adjustment for Python
165175
pub mod frameobject;
166176
#[cfg(Py_LIMITED_API)]
167177
pub mod frameobject {
168-
pub enum PyFrameObject {}
178+
opaque_struct!(PyFrameObject);
169179
}
170180

171181
pub(crate) mod datetime;

src/ffi/object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub type vectorcallfunc = unsafe extern "C" fn(
262262

263263
#[cfg(Py_LIMITED_API)]
264264
mod typeobject {
265-
pub enum PyTypeObject {}
265+
opaque_struct!(PyTypeObject);
266266
}
267267

268268
#[cfg(not(Py_LIMITED_API))]

src/ffi/pyarena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub enum PyArena {}
1+
opaque_struct!(PyArena);

src/ffi/pythonrun.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct PyCompilerFlags {
1515
}
1616

1717
#[cfg(not(Py_LIMITED_API))]
18-
pub enum _mod {}
18+
opaque_struct!(_mod);
1919

2020
#[cfg(not(Py_LIMITED_API))]
2121
extern "C" {
@@ -90,8 +90,8 @@ extern "C" {
9090
) -> *mut _mod;
9191
}
9292

93-
pub enum symtable {}
94-
pub enum _node {}
93+
opaque_struct!(symtable);
94+
opaque_struct!(_node);
9595

9696
#[inline]
9797
pub unsafe fn PyParser_SimpleParseString(s: *const c_char, b: c_int) -> *mut _node {

src/ffi/weakrefobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ffi::object::*;
22
use std::os::raw::c_int;
33

4-
pub enum PyWeakReference {}
4+
opaque_struct!(PyWeakReference);
55

66
extern "C" {
77
static mut _PyWeakref_RefType: PyTypeObject;

0 commit comments

Comments
 (0)