chromium / subspace

A concept-centered standard library for C++20, enabling safer and more reliable products and a more modern feel for C++ code.; Also home of Subdoc the code-documentation generator.
https://suslib.cc
Apache License 2.0
89 stars 15 forks source link

Provide Tuple::as_ref and as_mut #421

Open danakj opened 10 months ago

danakj commented 10 months ago

Rust does not have this ability due to lack of variadics, though the tuple_list crate has something: https://docs.rs/tuple_list/latest/tuple_list/trait.AsTupleOfRefs.html

  constexpr auto as() const& {
    return [this]<size_t... Is>(std::index_sequence<Is...>) {
      return ::sus::Tuple<const std::remove_reference_t<Ts>&...>(
          tuple_.template at<Is>()...);
    }(std::make_index_sequence<sizeof...(Ts)>());
  }
  constexpr auto as_mut() & {
    return [this]<size_t... Is>(std::index_sequence<Is...>) {
      return ::sus::Tuple<Ts&...>(tuple_.template at_mut<Is>()...);
    }(std::make_index_sequence<sizeof...(Ts)>());
  }

These methods of Choice should be able to be implemented as just tuple_.as_ref() or tuple_.as_mut().