I have a struct:
pub struct CommunityContents {
pub friends: RefCell<HashMap<FriendID, FriendData>>,
pub index: RefCell<HashMap<u64, BTreeMap<FriendID, FriendData>>>,
pub authenticated: bool,
pub age: u64,
pub height: u64,
}
Which is protected with a RwLock with a parent struct:
pub struct Community {
pub community_contents: RwLock<CommunityContents>,
}
pub struct FriendData {
pointer: Rc<Data>,
}
pub struct Data {
pub key: Key,
pub friend_ids: Vec<FriendID>,
}
I want to be able to modify the data inside index
. I have no problems inserting data into index doing write()
to CommunityContents
and a borrow_mut().insert(…)
for the BtreeMap
inside index
.
My problem is deleting elements from BtreeMap
, given FriendID
. My rough attempt is:
pub fn delete_family(community: &Self, friend_id: FriendID) {
//get community contents
let mut g = community.community_contents.write().expect("Lock is poisoned");
//get friend from inside friends name of community contents
let mut friend = g.friends.borrow_mut().get(&friend_id).unwrap().pointer.clone();
// get id attri
let mut friend_key = friend.key;
let mut a = g.index.borrow_mut().get(&friend_key);
let mut c = a.unwrap();
c.remove(&friend_id);
}
And I get the error cannot borrow as mutable. I've tried various things which has made my code above a bit messy.
Edit: sorry I missed out the FriendData
and Data
structs in my question.