Skip to content

Functions that don't mutate &RefMut<T> can take &T #73

@nevi-me

Description

@nevi-me

Hi

Functions like the below don't mutate the RefMut that they're provided, and so they could be simplified by taking a reference.

pub fn meets_initial_margin_requirement(
    user: &User,
-   user_positions: &RefMut<UserPositions>,
+   user_positions: &UserPositions,
    markets: &Ref<Markets>,
) ...

The benefit of taking a reference is that it simplifies the function requirements for someone who wants to call the function from outside of your program.
Otherwise one has to use bytemuck to get the type, deref to copy, then create a new RefCell (unless I missed something).

Anchor has a check to prevent trying to get a RefMut from an external program, rightly as it wouldn't own the account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions