ProjectionArg: remove unnecessary arc

This commit is contained in:
Michael Sippel 2021-05-13 23:46:22 +02:00
parent c6717e0e75
commit fc85af7493
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -52,9 +52,8 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
port: OuterViewPort<dyn SingletonView<Item = Item>>, port: OuterViewPort<dyn SingletonView<Item = Item>>,
notify: impl Fn(&mut P, &()) + Send + Sync + 'static notify: impl Fn(&mut P, &()) + Send + Sync + 'static
) -> Arc<RwLock<Option<Arc<dyn SingletonView<Item = Item>>>>> { ) -> Arc<RwLock<Option<Arc<dyn SingletonView<Item = Item>>>>> {
let (view, obs) = self.new_arg(notify); port.add_observer(self.new_arg(notify));
port.add_observer(obs); port.get_view_arc()
view
} }
pub fn new_sequence_arg<Item: 'static>( pub fn new_sequence_arg<Item: 'static>(
@ -62,9 +61,8 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
port: OuterViewPort<dyn SequenceView<Item = Item>>, port: OuterViewPort<dyn SequenceView<Item = Item>>,
notify: impl Fn(&mut P, &usize) + Send + Sync + 'static notify: impl Fn(&mut P, &usize) + Send + Sync + 'static
) -> Arc<RwLock<Option<Arc<dyn SequenceView<Item = Item>>>>> { ) -> Arc<RwLock<Option<Arc<dyn SequenceView<Item = Item>>>>> {
let (view, obs) = self.new_arg(notify); port.add_observer(self.new_arg(notify));
port.add_observer(obs); port.get_view_arc()
view
} }
pub fn new_index_arg<Key: Clone + Send + Sync + 'static, Item: 'static>( pub fn new_index_arg<Key: Clone + Send + Sync + 'static, Item: 'static>(
@ -72,9 +70,8 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
port: OuterViewPort<dyn IndexView<Key, Item = Item>>, port: OuterViewPort<dyn IndexView<Key, Item = Item>>,
notify: impl Fn(&mut P, &Key) + Send + Sync + 'static notify: impl Fn(&mut P, &Key) + Send + Sync + 'static
) -> Arc<RwLock<Option<Arc<dyn IndexView<Key, Item = Item>>>>> { ) -> Arc<RwLock<Option<Arc<dyn IndexView<Key, Item = Item>>>>> {
let (view, obs) = self.new_arg(notify); port.add_observer(self.new_arg(notify));
port.add_observer(obs); port.get_view_arc()
view
} }
pub fn new_arg< pub fn new_arg<
@ -82,16 +79,13 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
>( >(
&mut self, &mut self,
notify: impl Fn(&mut P, &V::Msg) + Send + Sync + 'static notify: impl Fn(&mut P, &V::Msg) + Send + Sync + 'static
) -> ( ) -> Arc<RwLock<ProjectionArg<V, Vec<V::Msg>>>>
Arc<RwLock<Option<Arc<V>>>>, where V::Msg: Send + Sync {
Arc<RwLock<ProjectionArg<V, Vec<V::Msg>>>>
) where V::Msg: Send + Sync {
let (tx, mut rx) = channel::<Vec<V::Msg>>(); let (tx, mut rx) = channel::<Vec<V::Msg>>();
let view = Arc::new(RwLock::new(None));
let arg = Arc::new(RwLock::new( let arg = Arc::new(RwLock::new(
ProjectionArg { ProjectionArg {
src: view.clone(), src: None,
sender: tx sender: tx
})); }));
@ -105,8 +99,8 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
}); });
self.keepalive.push(arg.clone()); self.keepalive.push(arg.clone());
(view, arg) arg
} }
} }
@ -117,7 +111,7 @@ where V: View + ?Sized,
D: ChannelData<Item = V::Msg>, D: ChannelData<Item = V::Msg>,
D::IntoIter: Send + Sync D::IntoIter: Send + Sync
{ {
src: Arc<RwLock<Option<Arc<V>>>>, src: Option<Arc<V>>,
sender: ChannelSender<D> sender: ChannelSender<D>
} }
@ -128,7 +122,7 @@ where D: ChannelData<Item = ()>,
D::IntoIter: Send + Sync D::IntoIter: Send + Sync
{ {
fn reset(&mut self, new_src: Option<Arc<dyn SingletonView<Item = Item>>>) { fn reset(&mut self, new_src: Option<Arc<dyn SingletonView<Item = Item>>>) {
*self.src.write().unwrap() = new_src; self.src = new_src;
self.notify(&()); self.notify(&());
} }
@ -145,7 +139,7 @@ where D: ChannelData<Item = usize>,
{ {
fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = Item>>>) { fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = Item>>>) {
let old_len = self.src.len().unwrap_or(0); let old_len = self.src.len().unwrap_or(0);
*self.src.write().unwrap() = new_src; self.src = new_src;
let new_len = self.src.len().unwrap_or(0); let new_len = self.src.len().unwrap_or(0);
self.notify_each(0 .. max(old_len, new_len)); self.notify_each(0 .. max(old_len, new_len));
@ -164,7 +158,7 @@ where D: ChannelData<Item = Key>,
{ {
fn reset(&mut self, new_src: Option<Arc<dyn IndexView<Key, Item = Item>>>) { fn reset(&mut self, new_src: Option<Arc<dyn IndexView<Key, Item = Item>>>) {
let old_area = self.src.area(); let old_area = self.src.area();
*self.src.write().unwrap() = new_src; self.src = new_src;
let new_area = self.src.area(); let new_area = self.src.area();
if let Some(area) = old_area { self.notify_each(area); } if let Some(area) = old_area { self.notify_each(area); }