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

getters should return references to types that are not Copy #208

Open
tdelabro opened this issue Mar 27, 2024 · 0 comments
Open

getters should return references to types that are not Copy #208

tdelabro opened this issue Mar 27, 2024 · 0 comments

Comments

@tdelabro
Copy link
Contributor

macro_rules! implement_deploy_account_tx_getters {
    ($(($field:ident, $field_type:ty)),*) => {
        $(
            pub fn $field(&self) -> $field_type {
                match self {
                    Self::V1(tx) => tx.$field.clone(),
                    Self::V3(tx) => tx.$field.clone(),
                }
            }
        )*
    };
}

...

impl DeployAccountTransaction {
    implement_deploy_account_tx_getters!(
        (class_hash, ClassHash),
        (constructor_calldata, Calldata),
        (contract_address_salt, ContractAddressSalt),
        (nonce, Nonce),
        (signature, TransactionSignature)
    );

Calling clone is not a problem for types that implement copy, but for types that don't. Like TransactionSignature, it means that a clone operation always occurs, even when the caller has no need for ownership of the value and could have used a reference.
This has a performance cost and is overall a bad design for an API: the method should not assume anything about the caller intentions

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

No branches or pull requests

1 participant