Skip to content
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

[Enhancement]: must_use_xor to Force Using One of the Functions but not all in a Struct #128264

Open
wiseaidev opened this issue Jul 27, 2024 · 1 comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@wiseaidev
Copy link

Hello,

I would like to propose a new macro, must_use_xor, similar to the existing must_use macro. This macro would ensure that users are required to invoke at least one of the functions annotated with it in a struct.

Rationale

The must_use macro is effective for ensuring that certain return values are not ignored, but it doesn't enforce that a choice between multiple functions is made. The must_use_xor macro would address this by enforcing that at least one of the annotated functions must be called, thus ensuring critical functionality is utilized.

Example

struct Dummy {
    value: i32,
}

impl Dummy {
    #[must_use_xor]
    fn set_value(&mut self, value: i32) {
        self.value = value;
    }

    #[must_use_xor]
    fn reset_value(&mut self) {
        self.value = 0;
    }
}

fn main() {
    let mut dummy = Dummy { value: 10 };

    // one of the `must_use_xor` functions must be called
    dummy.set_value(20);
}

Additional Context

I have recently come across the need for such a macro to enforce the Anchor client to call either signer or signers: coral-xyz/anchor#3116. However, one of the maintainers currently prefers using only signers as it aligns with the ts client, so there is no need for this macro in that context. Nevertheless, it would be beneficial to introduce such a macro for future use cases where enforcing the use of one among several critical functions is necessary.

Thanks for considering this feature request. I believe must_use_xor would be a valuable addition to the Rust language.

Best!

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 27, 2024
@workingjubilee
Copy link
Member

How would this combine with constructor functions? Are you still allowed to pass these objects around?

@workingjubilee workingjubilee added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 27, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

4 participants