summaryrefslogtreecommitdiff
path: root/helix-core/src
diff options
context:
space:
mode:
authorBenoƮt CORTIER2021-06-15 03:26:05 +0000
committerIvan Tham2021-06-15 15:01:56 +0000
commit6bdf609caaf4eb1c137f503f147d1e4e4f3e8676 (patch)
tree6de0864a077dd6bdea6f956ece264542a3ebeee7 /helix-core/src
parent6fb2d2679dc63ca34be0d02efb543c3e0b49cb74 (diff)
Remove RwLock for registers
Registers are stored inside `Editor` and accessed without `RwLock`. To work around ownership, I added a sister method to `Editor::current`: `Editor::current_with_context`. I tried to modify `Editor::current` directly but it's used at a lot of places so I reverted into this for now at least.
Diffstat (limited to 'helix-core/src')
-rw-r--r--helix-core/src/register.rs77
1 files changed, 60 insertions, 17 deletions
diff --git a/helix-core/src/register.rs b/helix-core/src/register.rs
index 0176d23e..cc881a17 100644
--- a/helix-core/src/register.rs
+++ b/helix-core/src/register.rs
@@ -1,20 +1,63 @@
-use crate::Tendril;
-use once_cell::sync::Lazy;
-use std::{collections::HashMap, sync::RwLock};
-
-// TODO: could be an instance on Editor
-static REGISTRY: Lazy<RwLock<HashMap<char, Vec<String>>>> =
- Lazy::new(|| RwLock::new(HashMap::new()));
-
-/// Read register values.
-pub fn get(register_name: char) -> Option<Vec<String>> {
- let registry = REGISTRY.read().unwrap();
- registry.get(&register_name).cloned() // TODO: no cloning
+use std::collections::HashMap;
+
+#[derive(Debug)]
+pub struct Register {
+ name: char,
+ values: Vec<String>,
+}
+
+impl Register {
+ pub fn new(name: char) -> Self {
+ Self {
+ name,
+ values: Vec::new(),
+ }
+ }
+
+ pub fn new_with_values(name: char, values: Vec<String>) -> Self {
+ Self { name, values }
+ }
+
+ pub fn name(&self) -> char {
+ self.name
+ }
+
+ pub fn read(&self) -> &Vec<String> {
+ &self.values
+ }
+
+ pub fn write(&mut self, values: Vec<String>) {
+ self.values = values;
+ }
}
-/// Read register values.
-// restoring: bool
-pub fn set(register_name: char, values: Vec<String>) {
- let mut registry = REGISTRY.write().unwrap();
- registry.insert(register_name, values);
+/// Currently just wraps a `HashMap` of `Register`s
+#[derive(Debug, Default)]
+pub struct Registers {
+ inner: HashMap<char, Register>,
+}
+
+impl Registers {
+ pub fn get(&self, name: char) -> Option<&Register> {
+ self.inner.get(&name)
+ }
+
+ pub fn get_mut(&mut self, name: char) -> Option<&mut Register> {
+ self.inner.get_mut(&name)
+ }
+
+ pub fn get_or_insert(&mut self, name: char) -> &mut Register {
+ self.inner
+ .entry(name)
+ .or_insert_with(|| Register::new(name))
+ }
+
+ pub fn write(&mut self, name: char, values: Vec<String>) {
+ self.inner
+ .insert(name, Register::new_with_values(name, values));
+ }
+
+ pub fn read(&self, name: char) -> Option<&Vec<String>> {
+ self.get(name).map(|reg| reg.read())
+ }
}