Use get() instead of get_mut() where possible

This commit is contained in:
Christian Kamm 2022-07-08 09:10:08 +02:00
parent 6c7245adaa
commit 469e7fd704
2 changed files with 9 additions and 7 deletions

View File

@ -74,12 +74,11 @@ pub fn liq_token_with_token(
// The main complication here is that we can't keep the liqee_asset_position and liqee_liab_position
// borrows alive at the same time. Possibly adding get_mut_pair() would be helpful.
let (liqee_asset_position, liqee_asset_raw_index) =
liqee.tokens.get_mut(asset_token_index)?;
let (liqee_asset_position, liqee_asset_raw_index) = liqee.tokens.get(asset_token_index)?;
let liqee_assets_native = liqee_asset_position.native(&asset_bank);
require!(liqee_assets_native.is_positive(), MangoError::SomeError);
let (liqee_liab_position, liqee_liab_raw_index) = liqee.tokens.get_mut(liab_token_index)?;
let (liqee_liab_position, liqee_liab_raw_index) = liqee.tokens.get(liab_token_index)?;
let liqee_liab_native = liqee_liab_position.native(&liab_bank);
require!(liqee_liab_native.is_negative(), MangoError::SomeError);

View File

@ -121,10 +121,14 @@ impl MangoAccountTokenPositions {
}
}
pub fn get(&self, token_index: TokenIndex) -> Result<&TokenPosition> {
/// Returns
/// - the position
/// - the raw index into the token positions list (for use with get_raw/deactivate)
pub fn get(&self, token_index: TokenIndex) -> Result<(&TokenPosition, usize)> {
self.values
.iter()
.find(|p| p.is_active_for_token(token_index))
.enumerate()
.find_map(|(raw_index, p)| p.is_active_for_token(token_index).then(|| (p, raw_index)))
.ok_or_else(|| error!(MangoError::SomeError)) // TODO: not found error
}
@ -135,8 +139,7 @@ impl MangoAccountTokenPositions {
self.values
.iter_mut()
.enumerate()
.find(|(_, p)| p.is_active_for_token(token_index))
.map(|(raw_index, p)| (p, raw_index))
.find_map(|(raw_index, p)| p.is_active_for_token(token_index).then(|| (p, raw_index)))
.ok_or_else(|| error!(MangoError::SomeError)) // TODO: not found error
}