Use Arc::make_mut in ChannelStore

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-08-08 17:49:29 -07:00
parent bbe4a9b388
commit 2605ae1ef5
2 changed files with 6 additions and 24 deletions

View File

@ -7,7 +7,6 @@ use futures::Future;
use futures::StreamExt;
use gpui::{AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
use rpc::{proto, TypedEnvelope};
use std::mem;
use std::sync::Arc;
pub type ChannelId = u64;
@ -319,10 +318,9 @@ impl ChannelStore {
.iter_mut()
.find(|c| c.id == channel.id)
{
util::make_arc_mut(existing_channel, |new_existing_channel| {
new_existing_channel.name = channel.name;
new_existing_channel.user_is_admin = channel.user_is_admin;
});
let existing_channel = Arc::make_mut(existing_channel);
existing_channel.name = channel.name;
existing_channel.user_is_admin = channel.user_is_admin;
continue;
}
@ -340,10 +338,9 @@ impl ChannelStore {
for channel in payload.channels {
if let Some(existing_channel) = self.channels.iter_mut().find(|c| c.id == channel.id) {
util::make_arc_mut(existing_channel, |new_existing_channel| {
new_existing_channel.name = channel.name;
new_existing_channel.user_is_admin = channel.user_is_admin;
});
let existing_channel = Arc::make_mut(existing_channel);
existing_channel.name = channel.name;
existing_channel.user_is_admin = channel.user_is_admin;
continue;
}

View File

@ -9,11 +9,9 @@ pub mod test;
use std::{
borrow::Cow,
cmp::{self, Ordering},
mem,
ops::{AddAssign, Range, RangeInclusive},
panic::Location,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
@ -120,19 +118,6 @@ pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut se
}
}
/// Mutates through the arc if no other references exist,
/// otherwise clones the value and swaps out the reference with a new Arc
/// Useful for mutating the elements of a list while using iter_mut()
pub fn make_arc_mut<T: Clone>(arc: &mut Arc<T>, mutate: impl FnOnce(&mut T)) {
if let Some(t) = Arc::get_mut(arc) {
mutate(t);
return;
}
let mut new_t = (**arc).clone();
mutate(&mut new_t);
mem::swap(&mut Arc::new(new_t), arc);
}
pub trait ResultExt<E> {
type Ok;