summaryrefslogtreecommitdiffstats
path: root/src/ui
diff options
context:
space:
mode:
authorStephan Dilly <dilly.stephan@gmail.com>2021-05-24 02:15:52 +0200
committerStephan Dilly <dilly.stephan@gmail.com>2021-05-24 02:15:52 +0200
commita8bf31b0abdc1461189c42ae2c6e35e4bdc1ea98 (patch)
tree5468141d57f756d4cdfb15826f92615a32df429b /src/ui
parentf73cfa01f1157929ef048e921df2458bec9f23fa (diff)
optimize all async job related clones away (#725)
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/syntax_text.rs41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/ui/syntax_text.rs b/src/ui/syntax_text.rs
index 44a0efbf..dfdb3b93 100644
--- a/src/ui/syntax_text.rs
+++ b/src/ui/syntax_text.rs
@@ -5,7 +5,7 @@ use std::{
ffi::OsStr,
ops::Range,
path::{Path, PathBuf},
- sync::Arc,
+ sync::{Arc, Mutex},
};
use syntect::{
highlighting::{
@@ -145,27 +145,50 @@ fn syntact_style_to_tui(style: &Style) -> tui::style::Style {
res
}
+enum JobState {
+ Request((String, String)),
+ Response(SyntaxText),
+}
+
#[derive(Clone, Default)]
pub struct AsyncSyntaxJob {
- //TODO: can we merge input and text into a single enum to represent the state transition?
- pub input: Option<(String, String)>,
- pub text: Arc<Option<SyntaxText>>,
+ state: Arc<Mutex<Option<JobState>>>,
}
impl AsyncSyntaxJob {
pub fn new(content: String, path: String) -> Self {
Self {
- input: Some((content, path)),
- text: Arc::new(None),
+ state: Arc::new(Mutex::new(Some(JobState::Request((
+ content, path,
+ ))))),
+ }
+ }
+
+ pub fn result(&self) -> Option<SyntaxText> {
+ if let Ok(mut state) = self.state.lock() {
+ if let Some(state) = state.take() {
+ return match state {
+ JobState::Request(_) => None,
+ JobState::Response(text) => Some(text),
+ };
+ }
}
+
+ None
}
}
impl AsyncJob for AsyncSyntaxJob {
fn run(&mut self) {
- if let Some((text, path)) = self.input.take() {
- let syntax = SyntaxText::new(text, Path::new(&path));
- self.text = Arc::new(Some(syntax));
+ if let Ok(mut state) = self.state.lock() {
+ *state = state.take().map(|state| match state {
+ JobState::Request((content, path)) => {
+ let syntax =
+ SyntaxText::new(content, Path::new(&path));
+ JobState::Response(syntax)
+ }
+ JobState::Response(res) => JobState::Response(res),
+ });
}
}
}