Skip to content

Commit 29afacc

Browse files
committed
chore: Use Cell instead of RefCell
1 parent d311970 commit 29afacc

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

backend/src/board.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ impl DaemonBoard {
153153
pub fn export_keymap(&self) -> KeyMap {
154154
let mut map = HashMap::new();
155155
for key in self.keys().iter() {
156-
let scancodes = key.scancodes.borrow();
157-
let scancodes = scancodes.iter().map(|s| s.1.clone()).collect();
156+
let scancodes = (0..self.layout().meta.num_layers as usize)
157+
.map(|layer| key.get_scancode(layer).unwrap().1)
158+
.collect();
158159
map.insert(key.logical_name.clone(), scancodes);
159160
}
160161
KeyMap {

backend/src/key.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
cell::{Cell, RefCell},
3-
char,
4-
};
1+
use std::{cell::Cell, char};
52

63
use crate::{DaemonBoard, DaemonBoardWeak, Hs, PhysicalLayoutKey, Rect, Rgb};
74

@@ -24,11 +21,11 @@ pub struct Key {
2421
pub leds: Vec<u8>,
2522
/// LED name
2623
pub led_name: String,
27-
pub(crate) led_color: Cell<Option<Hs>>,
24+
led_color: Cell<Option<Hs>>,
2825
// Key is currently pressed
2926
pub(crate) pressed: Cell<bool>,
3027
// Currently loaded scancodes and their names
31-
pub(crate) scancodes: RefCell<Vec<(u16, String)>>,
28+
scancodes: Vec<Cell<u16>>,
3229
// Background color
3330
pub background_color: Rgb,
3431
}
@@ -90,14 +87,12 @@ impl Key {
9087
}
9188
};
9289
debug!(" Scancode: {:04X}", scancode);
90+
debug!(
91+
" Scancode Name: {:?}",
92+
board.layout().scancode_names.get(&scancode)
93+
);
9394

94-
let scancode_name = match board.layout().scancode_names.get(&scancode) {
95-
Some(some) => some.to_string(),
96-
None => String::new(),
97-
};
98-
debug!(" Scancode Name: {}", scancode_name);
99-
100-
scancodes.push((scancode, scancode_name));
95+
scancodes.push(Cell::new(scancode));
10196
}
10297

10398
let mut led_color = None;
@@ -120,7 +115,7 @@ impl Key {
120115
led_name,
121116
led_color: Cell::new(led_color),
122117
pressed: Cell::new(false),
123-
scancodes: RefCell::new(scancodes),
118+
scancodes,
124119
background_color,
125120
}
126121
}
@@ -148,7 +143,13 @@ impl Key {
148143
}
149144

150145
pub fn get_scancode(&self, layer: usize) -> Option<(u16, String)> {
151-
self.scancodes.borrow().get(layer).cloned()
146+
let board = self.board();
147+
let scancode = self.scancodes.get(layer)?.get();
148+
let scancode_name = match board.layout().scancode_names.get(&scancode) {
149+
Some(some) => some.to_string(),
150+
None => String::new(),
151+
};
152+
Some((scancode, scancode_name))
152153
}
153154

154155
pub fn set_scancode(&self, layer: usize, scancode_name: &str) -> Result<(), String> {
@@ -165,7 +166,7 @@ impl Key {
165166
self.electrical.1,
166167
scancode,
167168
)?;
168-
self.scancodes.borrow_mut()[layer] = (scancode, scancode_name.to_string());
169+
self.scancodes[layer].set(scancode);
169170
Ok(())
170171
}
171172
}

0 commit comments

Comments
 (0)