Use Arc::make_mut in ChannelStore
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
bbe4a9b388
commit
2605ae1ef5
2 changed files with 6 additions and 24 deletions
|
@ -7,7 +7,6 @@ use futures::Future;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use gpui::{AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
|
use gpui::{AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
|
||||||
use rpc::{proto, TypedEnvelope};
|
use rpc::{proto, TypedEnvelope};
|
||||||
use std::mem;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub type ChannelId = u64;
|
pub type ChannelId = u64;
|
||||||
|
@ -319,10 +318,9 @@ impl ChannelStore {
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.find(|c| c.id == channel.id)
|
.find(|c| c.id == channel.id)
|
||||||
{
|
{
|
||||||
util::make_arc_mut(existing_channel, |new_existing_channel| {
|
let existing_channel = Arc::make_mut(existing_channel);
|
||||||
new_existing_channel.name = channel.name;
|
existing_channel.name = channel.name;
|
||||||
new_existing_channel.user_is_admin = channel.user_is_admin;
|
existing_channel.user_is_admin = channel.user_is_admin;
|
||||||
});
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,10 +338,9 @@ impl ChannelStore {
|
||||||
|
|
||||||
for channel in payload.channels {
|
for channel in payload.channels {
|
||||||
if let Some(existing_channel) = self.channels.iter_mut().find(|c| c.id == channel.id) {
|
if let Some(existing_channel) = self.channels.iter_mut().find(|c| c.id == channel.id) {
|
||||||
util::make_arc_mut(existing_channel, |new_existing_channel| {
|
let existing_channel = Arc::make_mut(existing_channel);
|
||||||
new_existing_channel.name = channel.name;
|
existing_channel.name = channel.name;
|
||||||
new_existing_channel.user_is_admin = channel.user_is_admin;
|
existing_channel.user_is_admin = channel.user_is_admin;
|
||||||
});
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,11 +9,9 @@ pub mod test;
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
cmp::{self, Ordering},
|
cmp::{self, Ordering},
|
||||||
mem,
|
|
||||||
ops::{AddAssign, Range, RangeInclusive},
|
ops::{AddAssign, Range, RangeInclusive},
|
||||||
panic::Location,
|
panic::Location,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
sync::Arc,
|
|
||||||
task::{Context, Poll},
|
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> {
|
pub trait ResultExt<E> {
|
||||||
type Ok;
|
type Ok;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue