Implement FromPyObject for HashSet and BTreeSet
This commit is contained in:
parent
6bc5207d4f
commit
97aca504a3
|
@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
* `FromPyObject` implementations for `HashSet` and `BTreeSet`. [#842](https://github.com/PyO3/pyo3/pull/842)
|
||||||
|
|
||||||
## [0.9.1]
|
## [0.9.1]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
@ -4,8 +4,11 @@
|
||||||
use crate::err::{self, PyErr, PyResult};
|
use crate::err::{self, PyErr, PyResult};
|
||||||
use crate::internal_tricks::Unsendable;
|
use crate::internal_tricks::Unsendable;
|
||||||
use crate::{
|
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};
|
use std::{collections, hash, ptr};
|
||||||
|
|
||||||
/// Represents a Python `set`
|
/// 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 {
|
impl PyFrozenSet {
|
||||||
/// Creates a new frozenset.
|
/// Creates a new frozenset.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in New Issue