Merge pull request #546 from thedrow/patch-2

Add empty() to PySet and PyFrozenSet
This commit is contained in:
Yuji Kanagawa 2019-07-20 10:51:23 +09:00 committed by GitHub
commit 63fd354fe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use crate::object::PyObject;
use crate::AsPyPointer;
use crate::Python;
use crate::{ToBorrowedObject, ToPyObject};
use std::ptr;
use std::{collections, hash};
/// Represents a Python `set`
@ -28,6 +29,11 @@ impl PySet {
unsafe { py.from_owned_ptr_or_err(ffi::PySet_New(list.as_ptr())) }
}
/// Creates a new empty set
pub fn empty<'p>(py: Python<'p>) -> PyResult<&'p PySet> {
unsafe { py.from_owned_ptr_or_err(ffi::PySet_New(ptr::null_mut())) }
}
/// Remove all elements from the set.
#[inline]
pub fn clear(&self) {
@ -128,6 +134,11 @@ impl PyFrozenSet {
unsafe { py.from_owned_ptr_or_err(ffi::PyFrozenSet_New(list.as_ptr())) }
}
/// Creates a new empty frozen set
pub fn empty<'p>(py: Python<'p>) -> PyResult<&'p PySet> {
unsafe { py.from_owned_ptr_or_err(ffi::PyFrozenSet_New(ptr::null_mut())) }
}
/// Return the number of items in the set.
/// This is equivalent to len(p) on a set.
#[inline]
@ -177,6 +188,14 @@ mod test {
assert!(PySet::new(py, &[v]).is_err());
}
#[test]
fn test_set_empty() {
let gil = Python::acquire_gil();
let py = gil.python();
let set = PySet::empty(py).unwrap();
assert_eq!(0, set.len());
}
#[test]
fn test_set_len() {
let gil = Python::acquire_gil();
@ -264,6 +283,14 @@ mod test {
assert!(PyFrozenSet::new(py, &[v]).is_err());
}
#[test]
fn test_frozenset_empty() {
let gil = Python::acquire_gil();
let py = gil.python();
let set = PyFrozenSet::empty(py).unwrap();
assert_eq!(0, set.len());
}
#[test]
fn test_frozenset_contains() {
let gil = Python::acquire_gil();