Merge pull request #546 from thedrow/patch-2
Add empty() to PySet and PyFrozenSet
This commit is contained in:
commit
63fd354fe2
|
@ -8,6 +8,7 @@ use crate::object::PyObject;
|
||||||
use crate::AsPyPointer;
|
use crate::AsPyPointer;
|
||||||
use crate::Python;
|
use crate::Python;
|
||||||
use crate::{ToBorrowedObject, ToPyObject};
|
use crate::{ToBorrowedObject, ToPyObject};
|
||||||
|
use std::ptr;
|
||||||
use std::{collections, hash};
|
use std::{collections, hash};
|
||||||
|
|
||||||
/// Represents a Python `set`
|
/// Represents a Python `set`
|
||||||
|
@ -28,6 +29,11 @@ impl PySet {
|
||||||
unsafe { py.from_owned_ptr_or_err(ffi::PySet_New(list.as_ptr())) }
|
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.
|
/// Remove all elements from the set.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn clear(&self) {
|
pub fn clear(&self) {
|
||||||
|
@ -128,6 +134,11 @@ impl PyFrozenSet {
|
||||||
unsafe { py.from_owned_ptr_or_err(ffi::PyFrozenSet_New(list.as_ptr())) }
|
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.
|
/// Return the number of items in the set.
|
||||||
/// This is equivalent to len(p) on a set.
|
/// This is equivalent to len(p) on a set.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -177,6 +188,14 @@ mod test {
|
||||||
assert!(PySet::new(py, &[v]).is_err());
|
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]
|
#[test]
|
||||||
fn test_set_len() {
|
fn test_set_len() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
|
@ -264,6 +283,14 @@ mod test {
|
||||||
assert!(PyFrozenSet::new(py, &[v]).is_err());
|
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]
|
#[test]
|
||||||
fn test_frozenset_contains() {
|
fn test_frozenset_contains() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
|
|
Loading…
Reference in New Issue