Skip to content

Commit

Permalink
Add repr(transparent) where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Jul 13, 2018
1 parent 2ffa302 commit eb613c6
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pyo3-derive-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -647,4 +649,3 @@ mod test {
assert_eq!(buffer.to_vec::<f32>(py).unwrap(), [10.0, 11.0, 12.0, 13.0]);
}
}

1 change: 1 addition & 0 deletions src/class/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ffi;
use python::{Python, ToPyPointer};
use typeob::PyTypeInfo;

#[repr(transparent)]
pub struct PyTraverseError(c_int);

/// GC support
Expand Down
1 change: 0 additions & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/objects/boolobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/floatob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/objects/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/objects/num2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/num3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/objects/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
1 change: 1 addition & 0 deletions src/objects/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/objects/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/objects/string2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/objects/typeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eb613c6

Please sign in to comment.