From 66761ed014af925beb757a00db07f3090bc7ba6c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 25 Aug 2022 06:19:56 -0700 Subject: [PATCH] termwiz: use interior mutability for Line::set_appdata --- termwiz/src/surface/line/line.rs | 45 +++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/termwiz/src/surface/line/line.rs b/termwiz/src/surface/line/line.rs index d321054b5..9ea31d49d 100644 --- a/termwiz/src/surface/line/line.rs +++ b/termwiz/src/surface/line/line.rs @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize}; use std::any::Any; use std::borrow::Cow; use std::ops::Range; -use std::sync::{Arc, Weak}; +use std::sync::{Arc, Mutex, Weak}; use unicode_segmentation::UnicodeSegmentation; use wezterm_bidi::{Direction, ParagraphDirectionHint}; @@ -30,14 +30,26 @@ pub enum DoubleClickRange { } #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct Line { pub(crate) cells: CellStorage, zones: Vec, seqno: SequenceNo, bits: LineBits, #[cfg_attr(feature = "use_serde", serde(skip))] - appdata: Option>, + appdata: Mutex>>, +} + +impl Clone for Line { + fn clone(&self) -> Self { + Self { + cells: self.cells.clone(), + zones: self.zones.clone(), + seqno: self.seqno, + bits: self.bits, + appdata: Mutex::new(self.appdata.lock().unwrap().clone()), + } + } } impl PartialEq for Line { @@ -56,7 +68,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], - appdata: None, + appdata: Mutex::new(None), } } @@ -67,7 +79,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], - appdata: None, + appdata: Mutex::new(None), } } @@ -81,7 +93,7 @@ impl Line { cells: CellStorage::C(ClusteredLine::new()), seqno, zones: vec![], - appdata: None, + appdata: Mutex::new(None), } } @@ -94,7 +106,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], - appdata: None, + appdata: Mutex::new(None), } } @@ -120,7 +132,7 @@ impl Line { bits: LineBits::NONE, seqno, zones: vec![], - appdata: None, + appdata: Mutex::new(None), } } @@ -204,15 +216,22 @@ impl Line { /// and not for use by "middleware" crates. /// A Weak reference is stored. /// `get_appdata` is used to retrieve a previously stored reference. - pub fn set_appdata(&mut self, appdata: Arc) { - self.appdata.replace(Arc::downgrade(&appdata)); + pub fn set_appdata(&self, appdata: Arc) { + self.appdata + .lock() + .unwrap() + .replace(Arc::downgrade(&appdata)); } /// Retrieve the appdata for the line, if any. /// This may return None in the case where the underlying data has /// been released: Line only stores a Weak reference to it. pub fn get_appdata(&self) -> Option> { - self.appdata.as_ref().and_then(|data| data.upgrade()) + self.appdata + .lock() + .unwrap() + .as_ref() + .and_then(|data| data.upgrade()) } /// Returns true if the line's last changed seqno is more recent @@ -526,7 +545,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], - appdata: None, + appdata: Mutex::new(None), } } @@ -610,7 +629,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno: self.current_seqno(), zones: vec![], - appdata: None, + appdata: Mutex::new(None), } }