Add repr(transparent) where applicable

This commit is contained in:
konstin 2018-07-13 18:10:09 +02:00
parent 2ffa302a8d
commit eb613c64d9
20 changed files with 27 additions and 5 deletions

View File

@ -16,11 +16,12 @@ pub fn py3_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS
quote! {
#[no_mangle]
#[allow(non_snake_case, unused_imports)]
#[allow(non_snake_case)]
/// This autogenerated function is called by the python interpreter when importing
/// the module.
pub unsafe extern "C" fn #cb_name() -> *mut ::pyo3::ffi::PyObject {
use ::pyo3::{IntoPyPointer, ObjectProtocol};
// initialize pyo3
::pyo3::init_once();
static mut MODULE_DEF: ::pyo3::ffi::PyModuleDef = ::pyo3::ffi::PyModuleDef_INIT;

View File

@ -29,6 +29,7 @@ use python::{Python, ToPyPointer};
use objects::PyObjectRef;
/// Allows access to the underlying buffer used by a python object such as `bytes`, `bytearray` or `array.array`.
#[repr(transparent)]
pub struct PyBuffer(Box<ffi::Py_buffer>); // use Box<> because Python expects that the Py_buffer struct has a stable memory address
// PyBuffer is thread-safe: the shape of the buffer is immutable while a Py_buffer exists.
@ -521,6 +522,7 @@ impl Drop for PyBuffer {
/// `&ReadOnlyCell<T>` is basically a safe version of `*const T`:
/// The data cannot be modified through the reference, but other references may
/// be modifying the data.
#[repr(transparent)]
pub struct ReadOnlyCell<T>(cell::UnsafeCell<T>);
impl <T: Copy> ReadOnlyCell<T> {
@ -647,4 +649,3 @@ mod test {
assert_eq!(buffer.to_vec::<f32>(py).unwrap(), [10.0, 11.0, 12.0, 13.0]);
}
}

View File

@ -9,6 +9,7 @@ use ffi;
use python::{Python, ToPyPointer};
use typeob::PyTypeInfo;
#[repr(transparent)]
pub struct PyTraverseError(c_int);
/// GC support

View File

@ -14,7 +14,6 @@ use conversion::{ToPyObject, IntoPyObject, FromPyObject};
use python::{Python, IntoPyPointer, ToPyPointer};
use typeob::{PyTypeInfo, PyTypeObject};
pub struct PyToken(PhantomData<Rc<()>>);
impl PyToken {

View File

@ -5,6 +5,7 @@ use python::{Python, ToPyPointer};
use conversion::{ToPyObject, IntoPyObject, ToBorrowedObject, PyTryFrom};
/// Represents a Python `bool`.
#[repr(transparent)]
pub struct PyBool(PyObject);
pyobject_native_type!(PyBool, ffi::PyBool_Type, ffi::PyBool_Check);

View File

@ -9,6 +9,7 @@ use python::{Python, ToPyPointer};
use err::{PyResult, PyErr};
/// Represents a Python `bytearray`.
#[repr(transparent)]
pub struct PyByteArray(PyObject);
pyobject_native_type!(PyByteArray, ffi::PyByteArray_Type, ffi::PyByteArray_Check);

View File

@ -12,6 +12,7 @@ use objects::{PyObjectRef, PyList};
use err::{self, PyResult, PyErr};
/// Represents a Python `dict`.
#[repr(transparent)]
pub struct PyDict(PyObject);
pyobject_native_type!(PyDict, ffi::PyDict_Type, ffi::PyDict_Check);

View File

@ -18,6 +18,7 @@ use objectprotocol::ObjectProtocol;
/// by using [`ToPyObject`](trait.ToPyObject.html)
/// and [extract](struct.PyObject.html#method.extract)
/// with `f32`/`f64`.
#[repr(transparent)]
pub struct PyFloat(PyObject);
pyobject_native_type!(PyFloat, ffi::PyFloat_Type, ffi::PyFloat_Check);

View File

@ -13,6 +13,7 @@ use python::{Python, ToPyPointer, IntoPyPointer};
use conversion::{ToPyObject, IntoPyObject, ToBorrowedObject};
/// Represents a Python `list`.
#[repr(transparent)]
pub struct PyList(PyObject);
pyobject_native_type!(PyList, ffi::PyList_Type, ffi::PyList_Check);

View File

@ -209,6 +209,7 @@ macro_rules! pyobject_extract(
use python::ToPyPointer;
use ffi;
/// Represents general python instance.
#[repr(transparent)]
pub struct PyObjectRef(::PyObject);
pyobject_native_type_named!(PyObjectRef);
pyobject_native_type_convert!(PyObjectRef, ffi::PyBaseObject_Type, ffi::PyObject_Check);

View File

@ -17,6 +17,7 @@ use instance::PyObjectWithToken;
use err::{PyResult, PyErr};
/// Represents a Python `module` object.
#[repr(transparent)]
pub struct PyModule(PyObject);
pyobject_native_type!(PyModule, ffi::PyModule_Type, ffi::PyModule_Check);
@ -43,7 +44,7 @@ impl PyModule {
/// Loads the python code specified into a new module
/// 'code' is the raw Python you want to load into the module
/// 'file_name' is the file name to associate with the module
/// 'file_name' is the file name to associate with the module
/// (this is used when Python reports errors, for example)
/// 'module_name' is the name to give the module
#[cfg(Py_3)]

View File

@ -24,6 +24,7 @@ use super::num_common::{err_if_invalid_value, IS_LITTLE_ENDIAN};
/// by using [`ToPyObject`](trait.ToPyObject.html)
/// and [extract](struct.PyObject.html#method.extract)
/// with the primitive Rust integer types.
#[repr(transparent)]
pub struct PyInt(PyObject);
pyobject_native_type!(PyInt, ffi::PyInt_Type, ffi::PyInt_Check);

View File

@ -21,6 +21,7 @@ use super::num_common::{err_if_invalid_value, IS_LITTLE_ENDIAN};
/// by using [`ToPyObject`](trait.ToPyObject.html)
/// and [extract](struct.PyObject.html#method.extract)
/// with the primitive Rust integer types.
#[repr(transparent)]
pub struct PyLong(PyObject);
pyobject_native_type!(PyLong, ffi::PyLong_Type, ffi::PyLong_Check);

View File

@ -14,6 +14,7 @@ use objectprotocol::ObjectProtocol;
/// Represents a reference to a python object supporting the sequence protocol.
#[repr(transparent)]
pub struct PySequence(PyObject);
pyobject_native_type_named!(PySequence);
pyobject_downcast!(PySequence, ffi::PySequence_Check);

View File

@ -11,9 +11,11 @@ use err::{self, PyResult, PyErr};
/// Represents a Python `set`
#[repr(transparent)]
pub struct PySet(PyObject);
/// Represents a Python `frozenset`
#[repr(transparent)]
pub struct PyFrozenSet(PyObject);

View File

@ -12,6 +12,7 @@ use conversion::ToPyObject;
/// Represents a Python `slice`.
///
/// Only `c_long` indeces supprted at the moment by `PySlice` object.
#[repr(transparent)]
pub struct PySlice(PyObject);
pyobject_native_type!(PySlice, ffi::PySlice_Type, ffi::PySlice_Check);

View File

@ -14,6 +14,7 @@ use err::{PyResult, PyErr};
use super::PyStringData;
/// Represents a Python `string`.
#[repr(transparent)]
pub struct PyString(PyObject);
pyobject_native_type!(PyString, ffi::PyUnicode_Type, ffi::PyUnicode_Check);
@ -23,6 +24,7 @@ pyobject_native_type!(PyString, ffi::PyUnicode_Type, ffi::PyUnicode_Check);
pub use PyString as PyUnicode;
/// Represents a Python `byte` string.
#[repr(transparent)]
pub struct PyBytes(PyObject);
pyobject_native_type!(PyBytes, ffi::PyBytes_Type, ffi::PyBytes_Check);

View File

@ -16,16 +16,19 @@ use objectprotocol::ObjectProtocol;
use super::{PyObjectRef, PyStringData};
/// Represents a Python `string`.
#[repr(transparent)]
pub struct PyString(PyObject);
pyobject_native_type!(PyString, ffi::PyBaseString_Type, ffi::PyBaseString_Check);
/// Represents a Python `unicode string`.
#[repr(transparent)]
pub struct PyUnicode(PyObject);
pyobject_native_type!(PyUnicode, ffi::PyUnicode_Type, ffi::PyUnicode_Check);
/// Represents a Python `byte` string. Corresponds to `str` in Python 2
#[repr(transparent)]
pub struct PyBytes(PyObject);
pyobject_native_type!(PyBytes, ffi::PyBaseString_Type, ffi::PyString_Check);

View File

@ -13,6 +13,7 @@ use conversion::{FromPyObject, ToPyObject, IntoPyTuple, IntoPyObject, PyTryFrom}
use super::exc;
/// Represents a Python `tuple` object.
#[repr(transparent)]
pub struct PyTuple(PyObject);
pyobject_native_type!(PyTuple, ffi::PyTuple_Type, ffi::PyTuple_Check);

View File

@ -13,6 +13,7 @@ use instance::{Py, PyObjectWithToken};
use typeob::{PyTypeInfo, PyTypeObject};
/// Represents a reference to a Python `type object`.
#[repr(transparent)]
pub struct PyType(PyObject);
pyobject_native_type!(PyType, ffi::PyType_Type, ffi::PyType_Check);