diff --git a/src/class/sequence.rs b/src/class/sequence.rs index 279d69e0eb9..af6bd87853b 100644 --- a/src/class/sequence.rs +++ b/src/class/sequence.rs @@ -146,7 +146,7 @@ where T: PySequenceProtocol<'p>, { fn tp_as_sequence() -> Option { - return Some(ffi::PySequenceMethods { + Some(ffi::PySequenceMethods { sq_length: Self::sq_length(), sq_concat: Self::sq_concat(), sq_repeat: Self::sq_repeat(), @@ -157,7 +157,7 @@ where sq_contains: Self::sq_contains(), sq_inplace_concat: Self::sq_inplace_concat(), sq_inplace_repeat: Self::sq_inplace_repeat(), - }); + }) } } diff --git a/src/types/list.rs b/src/types/list.rs index 900a269bf27..0f5f081cd6b 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -20,10 +20,7 @@ pyobject_native_type!(PyList, ffi::PyList_Type, ffi::PyList_Check); impl PyList { /// Construct a new list with the given elements. - pub fn new<'p, T, U>( - py: Python<'p>, - elements: impl IntoIterator, - ) -> &'p PyList + pub fn new(py: Python<'_>, elements: impl IntoIterator) -> &PyList where T: ToPyObject, U: ExactSizeIterator, diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 98153544a3d..edabd30623f 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -27,6 +27,12 @@ impl PySequence { } } + #[inline] + pub fn is_empty(&self) -> PyResult { + let len = self.len()?; + Ok(len == 0) + } + /// Return the concatenation of o1 and o2. Equivalent to python `o1 + o2` #[inline] pub fn concat(&self, other: &PySequence) -> PyResult<&PySequence> { @@ -656,4 +662,18 @@ mod test { let seq_from = unsafe { ::try_from_unchecked(type_ptr) }; assert!(seq_from.list().is_ok()); } + + #[test] + fn test_is_empty() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let list = vec![1].to_object(py); + let seq = list.cast_as::(py).unwrap(); + assert_eq!(seq.is_empty().unwrap(), false); + let vec: Vec = Vec::new(); + let empty_list = vec.to_object(py); + let empty_seq = empty_list.cast_as::(py).unwrap(); + assert_eq!(empty_seq.is_empty().unwrap(), true); + } + }