Skip to content

Better rvalue handling for NamedTuple and Result #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2025
Merged

Conversation

liuzicheng1987
Copy link
Contributor

No description provided.

@liuzicheng1987 liuzicheng1987 changed the title Better lvalue handling for NamedTuple and Result Better rvalue handling for NamedTuple and Result Jun 9, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @liuzicheng1987, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

gemini-code-assist here, providing a summary of this pull request. This PR focuses on improving the handling of lvalues and rvalues within the rfl::NamedTuple and rfl::Result classes. The primary goal appears to be enabling more efficient move operations when these objects are used as rvalues, by adding appropriate rvalue reference (&&) and lvalue reference (& or const&) qualifiers to member functions and using std::move where necessary.

Highlights

  • Improved Move Semantics: Member functions in both NamedTuple and Result classes have been updated with rvalue reference (&&) qualifiers. This allows these methods to be called on temporary objects (rvalues), enabling the compiler to potentially optimize operations by moving resources instead of copying.
  • Explicit Lvalue/Rvalue Overloads: Many methods now have distinct overloads for lvalue references (& or const&) and rvalue references (&&). This provides more precise control over how the object's state is handled based on its value category.
  • Consistent std::move Usage: Within the new rvalue-qualified member functions, std::move(*this) or std::move on member variables is used to correctly propagate the rvalue-ness, ensuring that move constructors and move assignment operators are invoked where appropriate.

Changelog

Click here to see the changelog
  • include/rfl/NamedTuple.hpp
    • Added std::forward to the move constructor (Diff 1).
    • Added && and const& ref qualifiers to add methods and used std::move(*this) in the rvalue overload (Diff 1, 2, 3).
    • Added && and const& ref qualifiers to and_then methods and used std::move(*this).fields() in the rvalue overload (Diff 4).
    • Added && and const& ref qualifiers to fields methods and used std::move(*this).make_fields() in the rvalue overload (Diff 5).
    • Added && and const& ref qualifiers to replace methods and used std::move(*this) in the rvalue overloads (Diff 6, 7, 8, 9).
    • Added && and const& ref qualifiers to transform methods and used std::move(*this).fields() in the rvalue overload (Diff 10).
    • Added && and const& ref qualifiers to add_tuple methods and used std::move(*this).add() in the rvalue overload (Diff 11).
    • Added && ref qualifier to the non-const make_fields method and used std::move on tuple elements (Diff 12).
    • Added const& ref qualifier to the const make_fields method (Diff 13).
    • Added && and const& ref qualifiers to replace_value methods and used std::move(values_) in the rvalue overload (Diff 14).
    • Added && and const& ref qualifiers to replace_tuple methods and used std::move(*this).replace() in the rvalue overload (Diff 15).
  • include/rfl/Result.hpp
    • Added && and const& ref qualifiers to and_then methods and used std::move(*this).get_t() and std::move(*this).get_err() in the rvalue overload (Diff 1).
    • Added &&, &, and const& ref qualifiers to operator* and used std::move(*this).get_t() in the rvalue overload (Diff 2).
    • Added && and const& ref qualifiers to or_else methods and used std::move(*this).get_t() and std::move(*this).get_err() in the rvalue overload (Diff 4).
    • Added && and const& ref qualifiers to transform methods, used std::move(*this).get_t() and std::move(*this).get_err() in the rvalue overload, and changed typename std::invoke_result<F, T>::type to std::invoke_result_t<F, T> (Diff 5).
    • Added && and const& ref qualifiers to value methods and used std::move(*this).get_t() in the rvalue overload (Diff 6).
    • Added && and const& ref qualifiers to value_or and error_or methods, added & ref qualifier to error(), and used std::move where appropriate in rvalue overloads (Diff 7).
    • Added &&, &, and const& ref qualifiers to get_t and get_err methods and used std::move in the rvalue overloads (Diff 8).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances rvalue handling for rfl::NamedTuple and rfl::Result by consistently applying ref-qualifiers (&&, &, const&) to member functions and ensuring that rvalue instances of these types can have their resources moved out correctly. The changes are generally well-implemented and follow modern C++ best practices for move semantics.

Key improvements include:

  • Consistent use of && ref-qualifiers on methods intended for rvalue objects, paired with std::move(*this) to propagate rvalue-ness.
  • Correct const& qualifiers for lvalue/const lvalue overloads.
  • For NamedTuple, the make_fields() && overload now correctly moves values from the internal values_ tuple.
  • For Result, operator* &&, value() &&, and the internal get_t() && / get_err() && methods now correctly return rvalue references (T&&, Error&&) and use std::move to enable moving out the contained value or error.

Overall, these are excellent changes that improve the efficiency and expressiveness of these classes when dealing with temporary objects or explicitly moved instances. I have a few suggestions for further refinement, primarily concerning API consistency in rfl::Result.

Summary of Findings

  • API Completeness for Result::error(): The Result class is missing the Error& error() & overload, which is part of the std::expected interface and would provide a way to get a non-const lvalue reference to the error from an lvalue Result.
  • Callable Handling Consistency in Result: The rvalue-qualified methods or_else and transform in Result take callables by const F&, while and_then takes F&&. Aligning these to all take F&& and use std::forward would improve API consistency and flexibility for rvalue callables.
  • Rvalue Semantics Implementation: The core changes for rvalue handling in both NamedTuple and Result (ref-qualifiers, std::move(*this), std::move on internal members, rvalue reference return types) are well-implemented and significantly improve move semantics.

Merge Readiness

The pull request introduces substantial improvements to rvalue handling for NamedTuple and Result. The core changes are solid. There are a few suggestions for API consistency and completeness in Result.hpp (medium severity) that I recommend addressing before merging. Once these are considered, the PR should be in excellent shape. As an AI, I am not authorized to approve pull requests; please ensure further review and approval by authorized team members.

@liuzicheng1987 liuzicheng1987 merged commit a0fe397 into main Jun 9, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant