#Tokio Sync Mutex type err

73 messages · Page 1 of 1 (latest)

summer steppe
#

You can’t move out of a MutexGuard is the issue

dry wren
#

Ah gotcha

summer steppe
#

Would it work for shadow_update_state to take groups as an &mut instead?

dry wren
#

yeah probably

#

i just dont want to update the original var groups

#

its meant to just clone it

#

or update it then revert it at the end of the fn

summer steppe
#

Ah, then just do groups.clone() at the call site instead?

#

Yeah, whatever works better for your code

dry wren
#

ok cool, ima try that real quick

#

ty

summer steppe
#

If yoy clone at global_state, because of the way Arc works, it’ll just create a new Arc pointing to the same data

dry wren
#

ah fek

#

what could i do to work around it?

summer steppe
#

Instead, you want to lock global_state, and call clone on the MutexGuard; MutexGuard doesn’t implement Clone, but it does implement Deref;
So basically since MutexGuard<T> implements Deref<Target=T>, the compiler can treat &MutexGuard<T> as &T, so you can call .clone() on the MutexGuard and it’ll pop off a clone of BTreeMap

dry wren
#

oops sorry same ss

summer steppe
#

Well, that particular error is because you used global_state inside the async move block in the tokio::spawn earlier on; that is a case where you do want to clone the arc, and then use the cloned arc inside the async move

#

Async move means that anything that the future captures from outside the async move will actually be moved in, regardless of whether you move it inside that block or not

dry wren
#

my goal with that one was to update the global_state from the thread

#

then use that updated state with this loop

summer steppe
#

Ah. Well, it would indeed work like that, although it’s possible that the calls won’t be interleaved nicely (not sure if tokio mutexes are fair or not)

summer steppe
#

Move what back?

dry wren
#

then i can clone the arc again in the other loop?

summer steppe
#

Don’t need to clone it a second time, you can use the original instance for the other loop as long as you don’t move it somewhere else

dry wren
#

ah okay

#

so i should just clone it outside of the thread

#

and then use that clone

summer steppe
#

Yeah basically

dry wren
#

got it

#

ima try that

dry wren
#

wouldnt cloning clone just create the same reference

summer steppe
#

Almost there; add a .clone() after the deref

dry wren
#

basically, im changing a few variables in groups then using that, then dropping it later

#

or at least thats the intention

#

why does clone create a reference

summer steppe
#

I’m doing this and it seems to be working as expected

dry wren
#

but no deref

summer steppe
#

Usually, the compiler does that automatically; i also just tried with g.deref().clone() and it still works—have you run cargo check? Might just be the ide not recognizing the change

dry wren
#

but

#

i think clone would just adjust g_state which would change the original global_state

summer steppe
#

Yep

dry wren
#

fs

#

but if its locked would it still be able to change

summer steppe
#

Try (&g_state).clone()?

#

Wait nvm dont try that

#

Yeah, idk what’s up with that; g_state.deref().clone() should work

dry wren
#

let me send the rest of the code

#

maybe im misisng something

dry wren
#

did you try with deref?

summer steppe
#

I don’t think you are; do you think you could try with Clone::clone(<MutexGuard<_> as Deref>::deref(&g_state)) ?

dry wren
#

just tells me to borrow

#

so same result

#

fs rust

summer steppe
#

Well, it’s fairly late over here for me and i need to head off—try asking #rust-discussions-3 ?

dry wren
#

Okay, thanks fren ❤️

summer steppe
#

Yeah np, sorry I couldn’t be more help

dry wren
#

nah, you helped fren

#

just rust being a bih

summer steppe
#

True, it do be like that sometimes

dry wren
#

someone gave the answer 😮

#

(*g_state).clone()

summer steppe
#

Just spent the last ~three hours trying to figure out some weird async stuff only to find that all i needed was to wrap a type in an option ,_,

dry wren
#

I think its bc i forgot to derive clone from something -.-

#

might jump off my balcony tonight lmao

summer steppe
#

Ohhh, rip 😔

#

Welp, now you know for next time, at least?

dry wren
#

yeah 100% lmao

#

thanks for your help

#

probs was making you crazy