#`cannot move out of dereference of`

1 messages · Page 1 of 1 (latest)

humble basin
#

Plz send code in text

#

Rather than a screenshot

vestal copper
#

ok.1 sec

#
let global_state = Arc::new(Mutex::new(state));

        // ------------------------------------------------
        // For every new block, refresh our reserves.
        // ------------------------------------------------
        tokio::spawn(async move {
            loop {
                let mut g_state = global_state.blocking_lock();
                let x = update_state(&querier, &groups.1, g_state.0).await;
                // state_tx.send(update).unwrap();
                sleep(Duration::from_secs(2));
            }
        });

and

pub async fn update_state(
    querier: &Querier<Provider<Http>>,
    addresses: &Vec<Address>,
    mut groups: BTreeMap<(Address, Address), BTreeMap<Dex, Pair>>,
) -> (BTreeMap<(Address, Address), BTreeMap<Dex, Pair>>, U256) {
    // Get all pair reserves + index.
    // [0] = reserves, [1] = index.
    let results = querier.get_data(addresses.to_vec()).call().await.unwrap();
    let mut reserves_counter = 0;

    // Store reserves in mappings...
    for group in &mut groups {
        for pair in group.1 {
            // println!("group: {:?} -- pair: {:?}", group.0, pair);
            pair.1.reserves = Reserves {
                reserves0: results.0[reserves_counter].reserves_0.into(),
                reserves1: results.0[reserves_counter].reserves_1.into(),
            };
            reserves_counter += 1;
        }
    }

    (groups, results.1)
}
vocal reef
#

You are passing a BTreeMap by value into update_state, which by itself is questionable but even if this was desired, you can't do that in your case because A) that BTreeMap is part of a tuple, which becomes problematic when you try to move only a part of and B) that tuple itself is behind a Deref of a MutexGuard, as the compiler helpfully tells you, which you never can move out of because then what would be left in the mutex?

#

What you likely want instead is to pass a mutable reference to the BTreeMap to your function:

pub async fn update_state(
    querier: &Querier<Provider<Http>>,
    addresses: &Vec<Address>,
    groups: &mut BTreeMap<(Address, Address), BTreeMap<Dex, Pair>>,
) -> U256 {
    // Get all pair reserves + index.
    // [0] = reserves, [1] = index.
    let results = querier.get_data(addresses.to_vec()).call().await.unwrap();
    let mut reserves_counter = 0;

    // Store reserves in mappings...
    for group in groups {
        for pair in group.1 {
            // println!("group: {:?} -- pair: {:?}", group.0, pair);
            pair.1.reserves = Reserves {
                reserves0: results.0[reserves_counter].reserves_0.into(),
                reserves1: results.0[reserves_counter].reserves_1.into(),
            };
            reserves_counter += 1;
        }
    }

    results.1
}

And only return the other thing

#

Then just call

let x = update_state(&querier, &groups.1, &mut g_state.0);
#

If you actually want a new BTreeMap however, and keep the original in the mutex, then you have to clone it

vestal copper
#

no need to actually return

vestal copper