summaryrefslogtreecommitdiff
path: root/src/config
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-10 19:06:46 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-10 19:06:46 +0200
commit63cfcbe7a7745b276de58ec92e0141b958c44feb (patch)
tree990e33a83756e27187033579ee2f85d5c79169d5 /src/config
parentb747ca8af52129876b577a4f20f7105a05c6b002 (diff)
use unsafe blocks instead of mutexes
Diffstat (limited to 'src/config')
-rw-r--r--src/config/constants.rs23
-rw-r--r--src/config/env.rs22
-rw-r--r--src/config/keymap/template.rs8
-rw-r--r--src/config/mod.rs18
-rw-r--r--src/config/theme/bar.rs8
-rw-r--r--src/config/theme/editor/bar.rs3
-rw-r--r--src/config/theme/editor/mod.rs18
-rw-r--r--src/config/theme/view/bar.rs3
-rw-r--r--src/config/theme/view/mod.rs16
9 files changed, 48 insertions, 71 deletions
diff --git a/src/config/constants.rs b/src/config/constants.rs
deleted file mode 100644
index 5a8283a..0000000
--- a/src/config/constants.rs
+++ /dev/null
@@ -1,23 +0,0 @@
-use lazy_static::lazy_static;
-
-lazy_static! {
- pub static ref USER_CONFIG_DIR: String = {
- match dirs::config_local_dir() {
- Some(mut d) => {
- d.push("neosheet");
- d.as_path().to_str().unwrap_or("").to_string()
- }
- None => String::new(),
- }
- };
- pub static ref USER_RC_PATH: String = {
- match dirs::config_local_dir() {
- Some(mut d) => {
- d.push("neosheet");
- d.push("init.lua");
- d.as_path().to_str().unwrap_or("").to_string()
- }
- None => String::new(),
- }
- };
-}
diff --git a/src/config/env.rs b/src/config/env.rs
new file mode 100644
index 0000000..5d53cc5
--- /dev/null
+++ b/src/config/env.rs
@@ -0,0 +1,22 @@
+use once_cell::sync::Lazy;
+
+pub static USER_CONFIG_DIR: Lazy<String> = Lazy::new(|| {
+ match dirs::config_local_dir() {
+ Some(mut d) => {
+ d.push("neosheet");
+ d.as_path().to_str().unwrap_or("").to_string()
+ }
+ None => String::new(),
+ }
+});
+
+pub static USER_RC_PATH: Lazy<String> = Lazy::new(|| {
+ match dirs::config_local_dir() {
+ Some(mut d) => {
+ d.push("neosheet");
+ d.push("init.lua");
+ d.as_path().to_str().unwrap_or("").to_string()
+ }
+ None => String::new(),
+ }
+});
diff --git a/src/config/keymap/template.rs b/src/config/keymap/template.rs
index cf68f80..37ebedf 100644
--- a/src/config/keymap/template.rs
+++ b/src/config/keymap/template.rs
@@ -17,7 +17,7 @@ macro_rules! KeyMapSections {
pub fn handle(event: KeyEvent) -> bool {
let (def, func) = {
- let mut config = GlobalConfig::instance_mut();
+ let config = GlobalConfig::get();
(config.keymap.$key.default_return, config.keymap.$key.store.get(event))
};
@@ -33,7 +33,7 @@ macro_rules! KeyMapSections {
}
pub fn map(event: KeyEvent, func: impl Runnable<(), bool> + 'static) {
- GlobalConfig::instance_mut()
+ GlobalConfig::get()
.keymap
.$key
.store
@@ -44,11 +44,11 @@ macro_rules! KeyMapSections {
impl UserData for $name {
fn add_fields<'lua, M: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut M) {
fields.add_field_function_get("default", |_, _| {
- Ok(GlobalConfig::instance().keymap.$key.default_return)
+ Ok(GlobalConfig::get().keymap.$key.default_return)
});
fields.add_field_function_set("default", |_, _, def: bool| {
- GlobalConfig::instance_mut().keymap.$key.default_return = def;
+ GlobalConfig::get().keymap.$key.default_return = def;
Ok(())
})
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index cc4154c..4aac0cf 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -1,11 +1,10 @@
-use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
+use std::ptr::addr_of_mut;
-use lazy_static::lazy_static;
use mlua::{UserData, UserDataFields};
use self::{keymap::KeyMap, theme::Theme};
-pub mod constants;
+pub mod env;
pub mod theme;
pub mod keymap;
@@ -15,10 +14,6 @@ pub struct GlobalConfig {
pub keymap: KeyMap,
}
-lazy_static! {
- static ref GLOBAL_CONFIG: RwLock<GlobalConfig> = RwLock::new(GlobalConfig::new());
-}
-
const DUMMY_CONFIG: GlobalConfig = GlobalConfig::new();
impl GlobalConfig {
@@ -29,12 +24,9 @@ impl GlobalConfig {
}
}
- pub fn instance() -> RwLockReadGuard<'static, Self> {
- GLOBAL_CONFIG.read().unwrap()
- }
-
- pub fn instance_mut() -> RwLockWriteGuard<'static, Self> {
- GLOBAL_CONFIG.write().unwrap()
+ pub fn get() -> &'static mut Self {
+ static mut GLOBAL_CONFIG: GlobalConfig = GlobalConfig::new();
+ unsafe { &mut *addr_of_mut!(GLOBAL_CONFIG) }
}
}
diff --git a/src/config/theme/bar.rs b/src/config/theme/bar.rs
index a28bede..59b6c00 100644
--- a/src/config/theme/bar.rs
+++ b/src/config/theme/bar.rs
@@ -1,5 +1,5 @@
macro_rules! BarTheme {
- ($name:ident, $type:ty, $cfg:expr, $cfg_mut:expr) => {
+ ($name:ident, $type:ty, $cfg:expr) => {
use crate::config::theme::style::Style;
use crate::lua::evalsto::EvalTo;
use crate::widgets::statusbar::StatusBar;
@@ -38,19 +38,19 @@ macro_rules! BarTheme {
fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_function_get("left", |_, _| Ok($cfg.left.clone()));
fields.add_field_function_set("left", |_, _, style: EvalTo<Style, $type>| {
- $cfg_mut.left = style;
+ $cfg.left = style;
Ok(())
});
fields.add_field_function_get("middle", |_, _| Ok($cfg.middle.clone()));
fields.add_field_function_set("middle", |_, _, style: EvalTo<Style, $type>| {
- $cfg_mut.middle = style;
+ $cfg.middle = style;
Ok(())
});
fields.add_field_function_get("right", |_, _| Ok($cfg.right.clone()));
fields.add_field_function_set("right", |_, _, style: EvalTo<Style, $type>| {
- $cfg_mut.right = style;
+ $cfg.right = style;
Ok(())
});
}
diff --git a/src/config/theme/editor/bar.rs b/src/config/theme/editor/bar.rs
index 62be7b0..e0ff0a7 100644
--- a/src/config/theme/editor/bar.rs
+++ b/src/config/theme/editor/bar.rs
@@ -3,6 +3,5 @@ use crate::config::{theme::bar::BarTheme, GlobalConfig};
BarTheme!(
EditorBarTheme,
(),
- GlobalConfig::instance().theme.editor.bar,
- GlobalConfig::instance_mut().theme.editor.bar
+ GlobalConfig::get().theme.editor.bar
);
diff --git a/src/config/theme/editor/mod.rs b/src/config/theme/editor/mod.rs
index 662dacc..33ab600 100644
--- a/src/config/theme/editor/mod.rs
+++ b/src/config/theme/editor/mod.rs
@@ -36,13 +36,7 @@ impl EditorTheme {
macro_rules! cfg {
() => {
- GlobalConfig::instance().theme.editor
- };
-}
-
-macro_rules! cfg_mut {
- () => {
- GlobalConfig::instance_mut().theme.editor
+ GlobalConfig::get().theme.editor
};
}
@@ -50,25 +44,25 @@ impl UserData for EditorTheme {
fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_function_get("background", |_, _| Ok(cfg!().background.clone()));
fields.add_field_function_set("background", |_, _, background: EvalTo<Style, ()>| {
- cfg_mut!().background = background;
+ cfg!().background = background;
Ok(())
});
fields.add_field_function_get("highlight", |_, _| Ok(cfg!().highlight.clone()));
fields.add_field_function_set("highlight", |_, _, highlight: HighlightTheme| {
- cfg_mut!().highlight = highlight;
+ cfg!().highlight = highlight;
Ok(())
});
fields.add_field_function_get("cursor_line", |_, _| Ok(cfg!().cursor_line.clone()));
fields.add_field_function_set("cursor_line", |_, _, cursor_line: EvalTo<Style, ()>| {
- cfg_mut!().cursor_line = cursor_line;
+ cfg!().cursor_line = cursor_line;
Ok(())
});
fields.add_field_function_get("line_number", |_, _| Ok(cfg!().line_number.clone()));
fields.add_field_function_set("line_number", |_, _, line_number: EvalTo<Style, ()>| {
- cfg_mut!().line_number = line_number;
+ cfg!().line_number = line_number;
Ok(())
});
@@ -78,7 +72,7 @@ impl UserData for EditorTheme {
fields.add_field_function_set(
"active_line_number",
|_, _, active_line_number: EvalTo<Style, ()>| {
- cfg_mut!().active_line_number = active_line_number;
+ cfg!().active_line_number = active_line_number;
Ok(())
},
);
diff --git a/src/config/theme/view/bar.rs b/src/config/theme/view/bar.rs
index 7fab31a..c2d759b 100644
--- a/src/config/theme/view/bar.rs
+++ b/src/config/theme/view/bar.rs
@@ -4,6 +4,5 @@ use crate::{config::GlobalConfig, state::view::mode::Mode};
BarTheme!(
SheetViewBarTheme,
Mode,
- GlobalConfig::instance().theme.view.bar,
- GlobalConfig::instance_mut().theme.view.bar
+ GlobalConfig::get().theme.view.bar
);
diff --git a/src/config/theme/view/mod.rs b/src/config/theme/view/mod.rs
index e88e9ad..931e7dd 100644
--- a/src/config/theme/view/mod.rs
+++ b/src/config/theme/view/mod.rs
@@ -32,13 +32,7 @@ impl SheetViewTheme {
macro_rules! cfg {
() => {
- GlobalConfig::instance().theme.view
- };
-}
-
-macro_rules! cfg_mut {
- () => {
- GlobalConfig::instance_mut().theme.view
+ GlobalConfig::get().theme.view
};
}
@@ -47,25 +41,25 @@ impl UserData for SheetViewTheme {
fields.add_field_function_get("cursor", |_, _| Ok(cfg!().cursor.clone()));
fields.add_field_function_set("cursor", |_, _, pair: EvalTo<Style, CellRef>| {
- cfg_mut!().cursor = pair;
+ cfg!().cursor = pair;
Ok(())
});
fields.add_field_function_get("selection", |_, _| Ok(cfg!().selection.clone()));
fields.add_field_function_set("selection", |_, _, pair: EvalTo<Style, CellRef>| {
- cfg_mut!().selection = pair;
+ cfg!().selection = pair;
Ok(())
});
fields.add_field_function_get("cell", |_, _| Ok(cfg!().cell.clone()));
fields.add_field_function_set("cell", |_, _, cell: EvalTo<Style, CellRef>| {
- cfg_mut!().cell = cell;
+ cfg!().cell = cell;
Ok(())
});
fields.add_field_function_get("background", |_, _| Ok(cfg!().background.clone()));
fields.add_field_function_set("background", |_, _, background: EvalTo<Style, ()>| {
- cfg_mut!().background = background;
+ cfg!().background = background;
Ok(())
});