feature: Use typed iterators in make_*iterator (#4876)

This commit is contained in:
Sergei Izmailov 2023-10-18 04:04:46 +09:00 committed by GitHub
parent 0cbd92bab8
commit 74439a64a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -15,6 +15,7 @@
#include "attr.h" #include "attr.h"
#include "gil.h" #include "gil.h"
#include "options.h" #include "options.h"
#include "typing.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@ -2447,7 +2448,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
typename Sentinel, typename Sentinel,
typename ValueType = typename detail::iterator_access<Iterator>::result_type, typename ValueType = typename detail::iterator_access<Iterator>::result_type,
typename... Extra> 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>, return detail::make_iterator_impl<detail::iterator_access<Iterator>,
Policy, Policy,
Iterator, Iterator,
@ -2465,7 +2466,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
typename Sentinel, typename Sentinel,
typename KeyType = typename detail::iterator_key_access<Iterator>::result_type, typename KeyType = typename detail::iterator_key_access<Iterator>::result_type,
typename... Extra> 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>, return detail::make_iterator_impl<detail::iterator_key_access<Iterator>,
Policy, Policy,
Iterator, Iterator,
@ -2483,7 +2484,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
typename Sentinel, typename Sentinel,
typename ValueType = typename detail::iterator_value_access<Iterator>::result_type, typename ValueType = typename detail::iterator_value_access<Iterator>::result_type,
typename... Extra> 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>, return detail::make_iterator_impl<detail::iterator_value_access<Iterator>,
Policy, Policy,
Iterator, Iterator,
@ -2498,8 +2499,10 @@ iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) {
/// `std::begin()`/`std::end()` /// `std::begin()`/`std::end()`
template <return_value_policy Policy = return_value_policy::reference_internal, template <return_value_policy Policy = return_value_policy::reference_internal,
typename Type, typename Type,
typename ValueType = typename detail::iterator_access<
decltype(std::begin(std::declval<Type &>()))>::result_type,
typename... Extra> typename... Extra>
iterator make_iterator(Type &value, Extra &&...extra) { typing::Iterator<ValueType> make_iterator(Type &value, Extra &&...extra) {
return make_iterator<Policy>( return make_iterator<Policy>(
std::begin(value), std::end(value), std::forward<Extra>(extra)...); 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()` /// `std::begin()`/`std::end()`
template <return_value_policy Policy = return_value_policy::reference_internal, template <return_value_policy Policy = return_value_policy::reference_internal,
typename Type, typename Type,
typename KeyType = typename detail::iterator_key_access<
decltype(std::begin(std::declval<Type &>()))>::result_type,
typename... Extra> 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>( return make_key_iterator<Policy>(
std::begin(value), std::end(value), std::forward<Extra>(extra)...); 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()` /// `std::begin()`/`std::end()`
template <return_value_policy Policy = return_value_policy::reference_internal, template <return_value_policy Policy = return_value_policy::reference_internal,
typename Type, typename Type,
typename ValueType = typename detail::iterator_value_access<
decltype(std::begin(std::declval<Type &>()))>::result_type,
typename... Extra> 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>( return make_value_iterator<Policy>(
std::begin(value), std::end(value), std::forward<Extra>(extra)...); std::begin(value), std::end(value), std::forward<Extra>(extra)...);
} }

View File

@ -58,6 +58,15 @@ def test_generalized_iterators_simple():
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5] 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(): def test_iterator_referencing():
"""Test that iterators reference rather than copy their referents.""" """Test that iterators reference rather than copy their referents."""
vec = m.VectorNonCopyableInt() vec = m.VectorNonCopyableInt()