Reputation: 4116
I'm using this struct:
pub struct Store {
pub player: HashMap<i64, Player>,
pub team: HashMap<i64, Team>,
}
impl Store {
pub fn new() -> Arc<Self> {
Arc::new(Self {
player: HashMap::new(),
team: HashMap::new(),
})
}
}
sharing it in my resolvers with:
async fn player_by_id(&self, store: Arc<Store>, id: i64) -> Result<()> {
let team = get_team();
store.team.insert(id, team.into()); // here I get the error
}
I get the error:
error[E0596]: cannot borrow data in an `Arc` as mutable
|
84 | store.team.insert(id, team.into());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Arc<Store>`
Why?
Upvotes: 0
Views: 761
Reputation: 70850
As explained in Rc
's documentation,
Shared references in Rust disallow mutation by default, and
Rc
is no exception: you cannot generally obtain a mutable reference to something inside anRc
.
Arc
is the same as Rc
in this regard. You need to use a Mutex
or RwLock
(or atomics) to change its contents.
Upvotes: 3