Resolve some clippy complains

This commit is contained in:
kngwyu 2019-12-23 16:52:16 +09:00
parent d5cff058ef
commit ea51756933
7 changed files with 36 additions and 26 deletions

View file

@ -162,7 +162,7 @@ impl PyObject {
where
D: FromPyObject<'p>,
{
FromPyObject::extract(self.as_ref(py).into())
FromPyObject::extract(self.as_ref(py))
}
/// Retrieves an attribute value.

View file

@ -307,12 +307,21 @@ pub struct PyClassInitializer<T: PyTypeInfo> {
super_init: Option<*mut PyClassInitializer<T::BaseType>>,
}
impl<T: PyTypeInfo> Default for PyClassInitializer<T> {
fn default() -> Self {
Self {
init: None,
super_init: None,
}
}
}
impl<T: PyTypeInfo> PyClassInitializer<T> {
/// Construct `PyClassInitializer` for specified value `value`.
///
/// Same as
/// ```ignore
/// let mut init = PyClassInitializer::<T>();
/// let mut init = PyClassInitializer::default::<T>();
/// init.init(value);
/// ```
pub fn from_value(value: T) -> Self {
@ -322,17 +331,8 @@ impl<T: PyTypeInfo> PyClassInitializer<T> {
}
}
/// Make new `PyClassInitializer` with empty values.
pub fn new() -> Self {
PyClassInitializer {
init: None,
super_init: None,
}
}
#[must_use]
#[doc(hiddden)]
pub fn init_class(self, shell: &mut T::ConcreteLayout) -> PyResult<()> {
fn init_class(self, shell: &mut T::ConcreteLayout) -> PyResult<()> {
macro_rules! raise_err {
($name: path) => {
return Err(PyErr::new::<RuntimeError, _>(format!(
@ -369,7 +369,7 @@ impl<T: PyTypeInfo> PyClassInitializer<T> {
if let Some(super_init) = self.super_init {
return unsafe { &mut *super_init };
}
let super_init = Box::into_raw(Box::new(PyClassInitializer::new()));
let super_init = Box::into_raw(Box::new(PyClassInitializer::default()));
self.super_init = Some(super_init);
unsafe { &mut *super_init }
}

View file

@ -1,5 +1,4 @@
//! This module contains additional fields pf pyclass
// TODO(kngwyu): Add vectorcall support
use crate::{ffi, Python};
const POINTER_SIZE: isize = std::mem::size_of::<*mut ffi::PyObject>() as _;
@ -8,7 +7,7 @@ const POINTER_SIZE: isize = std::mem::size_of::<*mut ffi::PyObject>() as _;
pub trait PyClassDict {
const OFFSET: Option<isize> = None;
fn new() -> Self;
fn clear_dict(&mut self, _py: Python) {}
unsafe fn clear_dict(&mut self, _py: Python) {}
private_decl! {}
}
@ -16,7 +15,7 @@ pub trait PyClassDict {
pub trait PyClassWeakRef {
const OFFSET: Option<isize> = None;
fn new() -> Self;
fn clear_weakrefs(&mut self, _obj: *mut ffi::PyObject, _py: Python) {}
unsafe fn clear_weakrefs(&mut self, _obj: *mut ffi::PyObject, _py: Python) {}
private_decl! {}
}
@ -47,9 +46,9 @@ impl PyClassDict for PyClassDictSlot {
fn new() -> Self {
Self(std::ptr::null_mut())
}
fn clear_dict(&mut self, _py: Python) {
if self.0 != std::ptr::null_mut() {
unsafe { ffi::PyDict_Clear(self.0) }
unsafe fn clear_dict(&mut self, _py: Python) {
if !self.0.is_null() {
ffi::PyDict_Clear(self.0)
}
}
}
@ -64,9 +63,9 @@ impl PyClassWeakRef for PyClassWeakRefSlot {
fn new() -> Self {
Self(std::ptr::null_mut())
}
fn clear_weakrefs(&mut self, obj: *mut ffi::PyObject, _py: Python) {
if self.0 != std::ptr::null_mut() {
unsafe { ffi::PyObject_ClearWeakRefs(obj) }
unsafe fn clear_weakrefs(&mut self, obj: *mut ffi::PyObject, _py: Python) {
if !self.0.is_null() {
ffi::PyObject_ClearWeakRefs(obj)
}
}
}

View file

@ -7,7 +7,11 @@ use crate::types::{PyAny, PyType};
use crate::{ffi, AsPyPointer, Python};
use std::ptr::NonNull;
/// TODO: write document
/// `T: PyObjectLayout<U>` represents that `T` is a concrete representaion of `U` in Python heap.
/// E.g., `PyClassShell` is a concrete representaion of all `pyclass`es, and `ffi::PyObject`
/// is of `PyAny`.
///
/// This trait is intended to be used internally.
pub trait PyObjectLayout<T: PyTypeInfo> {
const NEED_INIT: bool = false;
const IS_NATIVE_TYPE: bool = true;
@ -19,6 +23,8 @@ pub trait PyObjectLayout<T: PyTypeInfo> {
unsafe fn internal_ref_cast(obj: &PyAny) -> &T {
&*(obj as *const _ as *const T)
}
#[allow(clippy::mut_from_ref)]
unsafe fn internal_mut_cast(obj: &PyAny) -> &mut T {
&mut *(obj as *const _ as *const T as *mut T)
}
@ -27,6 +33,11 @@ pub trait PyObjectLayout<T: PyTypeInfo> {
unsafe fn py_drop(&mut self, _py: Python) {}
}
/// `T: PyObjectSizedLayout<U>` represents `T` is not a instance of
/// [`PyVarObject`](https://docs.python.org/3.8/c-api/structures.html?highlight=pyvarobject#c.PyVarObject).
/// , in addition that `T` is a concrete representaion of `U`.
///
/// `pyclass`es need this trait for their base class.
pub trait PyObjectSizedLayout<T: PyTypeInfo>: PyObjectLayout<T> + Sized {}
/// Our custom type flags

View file

@ -207,7 +207,7 @@ mod test {
let s = "Hello Python";
let py_string = s.to_object(py);
let s2: &str = FromPyObject::extract(py_string.as_ref(py).into()).unwrap();
let s2: &str = FromPyObject::extract(py_string.as_ref(py)).unwrap();
assert_eq!(s, s2);
}

View file

@ -119,7 +119,7 @@ impl<'a> Iterator for PyTupleIterator<'a> {
if self.index < self.slice.len() {
let item = self.slice[self.index].as_ref(self.py);
self.index += 1;
Some(item.into())
Some(item)
} else {
None
}

View file

@ -91,7 +91,7 @@ struct InvalidSubClass2 {
impl InvalidSubClass2 {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::new()
PyClassInitializer::default()
}
}