From b853994e7a53dea60679b29faedbd50673cabf4a Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 22 Aug 2025 03:04:59 +0300 Subject: [PATCH] libutil: Replace hand-rolled enumerate with std::views::{zip,iota} It would have been nice to use std::views::enumerate here, but it uses a signed difference type for the value_type: > value_type = std::tuple> zip + iota has the same semantics as the code used to have, so there's no behavior change here. --- src/libutil/include/nix/util/util.hh | 53 ++++------------------------ 1 file changed, 6 insertions(+), 47 deletions(-) diff --git a/src/libutil/include/nix/util/util.hh b/src/libutil/include/nix/util/util.hh index dd6294c2a..561550c41 100644 --- a/src/libutil/include/nix/util/util.hh +++ b/src/libutil/include/nix/util/util.hh @@ -4,13 +4,13 @@ #include "nix/util/types.hh" #include "nix/util/error.hh" #include "nix/util/logging.hh" +#include "nix/util/strings.hh" #include #include #include #include - -#include "nix/util/strings.hh" +#include namespace nix { @@ -300,53 +300,12 @@ struct MaintainCount /** * A Rust/Python-like enumerate() iterator adapter. - * - * Borrowed from http://reedbeta.com/blog/python-like-enumerate-in-cpp17. */ -template< - typename T, - typename TIter = decltype(std::begin(std::declval())), - typename = decltype(std::end(std::declval()))> -constexpr auto enumerate(T && iterable) +template +constexpr auto enumerate(R && range) { - struct iterator - { - size_t i; - TIter iter; - - constexpr bool operator!=(const iterator & other) const - { - return iter != other.iter; - } - - constexpr void operator++() - { - ++i; - ++iter; - } - - constexpr auto operator*() const - { - return std::tie(i, *iter); - } - }; - - struct iterable_wrapper - { - T iterable; - - constexpr auto begin() - { - return iterator{0, std::begin(iterable)}; - } - - constexpr auto end() - { - return iterator{0, std::end(iterable)}; - } - }; - - return iterable_wrapper{std::forward(iterable)}; + /* Not std::views::enumerate because it uses difference_type for the index. */ + return std::views::zip(std::views::iota(size_t{0}), std::forward(range)); } /**