termwiz: use interior mutability for Line::set_appdata
This commit is contained in:
1 parent
6962d6805a
commit
66761ed014
1 file changed
+32
-13
@@ -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<ZoneRange>,
|
||||
seqno: SequenceNo,
|
||||
bits: LineBits,
|
||||
#[cfg_attr(feature = "use_serde", serde(skip))]
|
||||
appdata: Option<Weak<dyn Any + Send + Sync>>,
|
||||
appdata: Mutex<Option<Weak<dyn Any + Send + Sync>>>,
|
||||
}
|
||||
|
||||
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<dyn Any + Send + Sync>) {
|
||||
self.appdata.replace(Arc::downgrade(&appdata));
|
||||
pub fn set_appdata(&self, appdata: Arc<dyn Any + Send + Sync>) {
|
||||
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<Arc<dyn Any + Send + Sync>> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user