mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
feature: Use typed iterators in make_*iterator
(#4876)
This commit is contained in:
parent
0cbd92bab8
commit
74439a64a2
@ -15,6 +15,7 @@
|
||||
#include "attr.h"
|
||||
#include "gil.h"
|
||||
#include "options.h"
|
||||
#include "typing.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@ -2447,7 +2448,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
|
||||
typename Sentinel,
|
||||
typename ValueType = typename detail::iterator_access<Iterator>::result_type,
|
||||
typename... Extra>
|
||||
iterator make_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
typing::Iterator<ValueType> make_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
return detail::make_iterator_impl<detail::iterator_access<Iterator>,
|
||||
Policy,
|
||||
Iterator,
|
||||
@ -2465,7 +2466,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
|
||||
typename Sentinel,
|
||||
typename KeyType = typename detail::iterator_key_access<Iterator>::result_type,
|
||||
typename... Extra>
|
||||
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
typing::Iterator<KeyType> make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
return detail::make_iterator_impl<detail::iterator_key_access<Iterator>,
|
||||
Policy,
|
||||
Iterator,
|
||||
@ -2483,7 +2484,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
|
||||
typename Sentinel,
|
||||
typename ValueType = typename detail::iterator_value_access<Iterator>::result_type,
|
||||
typename... Extra>
|
||||
iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
typing::Iterator<ValueType> make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
return detail::make_iterator_impl<detail::iterator_value_access<Iterator>,
|
||||
Policy,
|
||||
Iterator,
|
||||
@ -2498,8 +2499,10 @@ iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) {
|
||||
/// `std::begin()`/`std::end()`
|
||||
template <return_value_policy Policy = return_value_policy::reference_internal,
|
||||
typename Type,
|
||||
typename ValueType = typename detail::iterator_access<
|
||||
decltype(std::begin(std::declval<Type &>()))>::result_type,
|
||||
typename... Extra>
|
||||
iterator make_iterator(Type &value, Extra &&...extra) {
|
||||
typing::Iterator<ValueType> make_iterator(Type &value, Extra &&...extra) {
|
||||
return make_iterator<Policy>(
|
||||
std::begin(value), std::end(value), std::forward<Extra>(extra)...);
|
||||
}
|
||||
@ -2508,8 +2511,10 @@ iterator make_iterator(Type &value, Extra &&...extra) {
|
||||
/// `std::begin()`/`std::end()`
|
||||
template <return_value_policy Policy = return_value_policy::reference_internal,
|
||||
typename Type,
|
||||
typename KeyType = typename detail::iterator_key_access<
|
||||
decltype(std::begin(std::declval<Type &>()))>::result_type,
|
||||
typename... Extra>
|
||||
iterator make_key_iterator(Type &value, Extra &&...extra) {
|
||||
typing::Iterator<KeyType> make_key_iterator(Type &value, Extra &&...extra) {
|
||||
return make_key_iterator<Policy>(
|
||||
std::begin(value), std::end(value), std::forward<Extra>(extra)...);
|
||||
}
|
||||
@ -2518,8 +2523,10 @@ iterator make_key_iterator(Type &value, Extra &&...extra) {
|
||||
/// `std::begin()`/`std::end()`
|
||||
template <return_value_policy Policy = return_value_policy::reference_internal,
|
||||
typename Type,
|
||||
typename ValueType = typename detail::iterator_value_access<
|
||||
decltype(std::begin(std::declval<Type &>()))>::result_type,
|
||||
typename... Extra>
|
||||
iterator make_value_iterator(Type &value, Extra &&...extra) {
|
||||
typing::Iterator<ValueType> make_value_iterator(Type &value, Extra &&...extra) {
|
||||
return make_value_iterator<Policy>(
|
||||
std::begin(value), std::end(value), std::forward<Extra>(extra)...);
|
||||
}
|
||||
|
@ -58,6 +58,15 @@ def test_generalized_iterators_simple():
|
||||
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]
|
||||
|
||||
|
||||
def test_iterator_doc_annotations():
|
||||
assert m.IntPairs.nonref.__doc__.endswith("-> Iterator[tuple[int, int]]\n")
|
||||
assert m.IntPairs.nonref_keys.__doc__.endswith("-> Iterator[int]\n")
|
||||
assert m.IntPairs.nonref_values.__doc__.endswith("-> Iterator[int]\n")
|
||||
assert m.IntPairs.simple_iterator.__doc__.endswith("-> Iterator[tuple[int, int]]\n")
|
||||
assert m.IntPairs.simple_keys.__doc__.endswith("-> Iterator[int]\n")
|
||||
assert m.IntPairs.simple_values.__doc__.endswith("-> Iterator[int]\n")
|
||||
|
||||
|
||||
def test_iterator_referencing():
|
||||
"""Test that iterators reference rather than copy their referents."""
|
||||
vec = m.VectorNonCopyableInt()
|
||||
|
Loading…
Reference in New Issue
Block a user