Implement FromPyObject for HashSet and BTreeSet

This commit is contained in:
Árni Dagur 2020-03-29 20:52:37 -04:00
parent 6bc5207d4f
commit 97aca504a3
2 changed files with 36 additions and 1 deletions

View File

@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased
### Added
* `FromPyObject` implementations for `HashSet` and `BTreeSet`. [#842](https://github.com/PyO3/pyo3/pull/842)
## [0.9.1]
### Fixed

View File

@ -4,8 +4,11 @@
use crate::err::{self, PyErr, PyResult};
use crate::internal_tricks::Unsendable;
use crate::{
ffi, AsPyPointer, PyAny, PyNativeType, PyObject, Python, ToBorrowedObject, ToPyObject,
ffi, AsPyPointer, FromPyObject, PyAny, PyNativeType, PyObject, PyTryFrom, Python,
ToBorrowedObject, ToPyObject,
};
use std::cmp;
use std::collections::{BTreeSet, HashSet};
use std::{collections, hash, ptr};
/// Represents a Python `set`
@ -181,6 +184,35 @@ where
}
}
impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>
where
K: FromPyObject<'source> + cmp::Eq + hash::Hash,
S: hash::BuildHasher + Default,
{
fn extract(ob: &'source PyAny) -> Result<Self, PyErr> {
let set = <PySet as PyTryFrom>::try_from(ob)?;
let mut ret = HashSet::default();
for k in set.iter() {
ret.insert(K::extract(k)?);
}
Ok(ret)
}
}
impl<'source, K> FromPyObject<'source> for BTreeSet<K>
where
K: FromPyObject<'source> + cmp::Ord,
{
fn extract(ob: &'source PyAny) -> Result<Self, PyErr> {
let set = <PySet as PyTryFrom>::try_from(ob)?;
let mut ret = BTreeSet::default();
for k in set.iter() {
ret.insert(K::extract(k)?);
}
Ok(ret)
}
}
impl PyFrozenSet {
/// Creates a new frozenset.
///