Skip to content

Commit 3469c61

Browse files
ids1024jackpot51
authored andcommitted
improv: Use RefCell internally in implementation of Daemon
`Rc<dyn Daemon>` is a nicer type to be passing around than `Rc<RefCell<dyn Daemon>>`. Dealing with mutability this way isn't really idiomatic Rust, but is idiomatic gtk/gtk-rs.
1 parent 6582295 commit 3469c61

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

src/application/keyboard.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::picker::Picker;
2323
use super::rect::Rect;
2424

2525
pub struct Keyboard {
26-
daemon_opt: Option<Rc<RefCell<dyn Daemon>>>,
26+
daemon_opt: Option<Rc<dyn Daemon>>,
2727
daemon_board: usize,
2828
keymap: HashMap<String, u16>,
2929
keys: RefCell<Vec<Key>>,
@@ -33,7 +33,7 @@ pub struct Keyboard {
3333
}
3434

3535
impl Keyboard {
36-
pub fn new<P: AsRef<Path>>(dir: P, daemon_opt: Option<Rc<RefCell<dyn Daemon>>>, daemon_board: usize) -> Rc<Self> {
36+
pub fn new<P: AsRef<Path>>(dir: P, daemon_opt: Option<Rc<dyn Daemon>>, daemon_board: usize) -> Rc<Self> {
3737
let dir = dir.as_ref();
3838

3939
let keymap_csv = fs::read_to_string(dir.join("keymap.csv"))
@@ -45,7 +45,7 @@ impl Keyboard {
4545
Self::new_data(&keymap_csv, &layout_csv, &physical_json, daemon_opt, daemon_board)
4646
}
4747

48-
pub fn new_board(board: &str, daemon_opt: Option<Rc<RefCell<dyn Daemon>>>, daemon_board: usize) -> Option<Rc<Self>> {
48+
pub fn new_board(board: &str, daemon_opt: Option<Rc<dyn Daemon>>, daemon_board: usize) -> Option<Rc<Self>> {
4949
macro_rules! keyboard {
5050
($board:expr) => (if board == $board {
5151
let keymap_csv = include_str!(concat!("../../layouts/", $board, "/keymap.csv"));
@@ -69,7 +69,7 @@ impl Keyboard {
6969
None
7070
}
7171

72-
fn new_data(keymap_csv: &str, layout_csv: &str, physical_json: &str, daemon_opt: Option<Rc<RefCell<dyn Daemon>>>, daemon_board: usize) -> Rc<Self> {
72+
fn new_data(keymap_csv: &str, layout_csv: &str, physical_json: &str, daemon_opt: Option<Rc<dyn Daemon>>, daemon_board: usize) -> Rc<Self> {
7373
let mut keymap = HashMap::new();
7474
let mut scancode_names = HashMap::new();
7575
scancode_names.insert(0, "NONE");
@@ -181,7 +181,6 @@ impl Keyboard {
181181
for layer in 0..2 {
182182
println!(" Layer {}", layer);
183183
let scancode = if let Some(ref daemon) = daemon_opt {
184-
let mut daemon = daemon.borrow_mut();
185184
match daemon.keymap_get(daemon_board, layer, electrical.0, electrical.1) {
186185
Ok(value) => value,
187186
Err(err) => {
@@ -353,7 +352,6 @@ button {
353352
}
354353
println!(" set {}, {}, {} to {:04X}", layer, k.electrical.0, k.electrical.1, k.scancodes[layer].0);
355354
if let Some(ref daemon) = kb.daemon_opt {
356-
let mut daemon = daemon.borrow_mut();
357355
if let Err(err) = daemon.keymap_set(kb.daemon_board, layer as u8, k.electrical.0, k.electrical.1, k.scancodes[layer].0) {
358356
eprintln!("Failed to set keymap: {:?}", err);
359357
}
@@ -410,7 +408,6 @@ button {
410408
};
411409

412410
let max_brightness = if let Some(ref daemon) = self.daemon_opt {
413-
let mut daemon = daemon.borrow_mut();
414411
match daemon.max_brightness(self.daemon_board) {
415412
Ok(value) => value as f64,
416413
Err(err) => {
@@ -431,7 +428,6 @@ button {
431428
brightness_scale.connect_value_changed(move |this| {
432429
let value = this.get_value() as i32;
433430
if let Some(ref daemon) = kb.daemon_opt {
434-
let mut daemon = daemon.borrow_mut();
435431
if let Err(err) = daemon.set_brightness(kb.daemon_board, value) {
436432
eprintln!("{}", err);
437433
}
@@ -446,7 +442,6 @@ button {
446442
};
447443

448444
let color_rgba = if let Some(ref daemon) = self.daemon_opt {
449-
let mut daemon = daemon.borrow_mut();
450445
match daemon.color(self.daemon_board) {
451446
Ok(value) => {
452447
let (red, green, blue) = value.to_floats();
@@ -468,7 +463,6 @@ button {
468463
let rgba = this.get_rgba();
469464
let color = Rgb::from_floats(rgba.red, rgba.green, rgba.blue);
470465
if let Some(ref daemon) = kb.daemon_opt {
471-
let mut daemon = daemon.borrow_mut();
472466
if let Err(err) = daemon.set_color(kb.daemon_board, color) {
473467
eprintln!("{}", err);
474468
}

src/application/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use cascade::cascade;
22
use gio::prelude::*;
33
use gtk::prelude::*;
4-
use std::cell::RefCell;
54
use std::rc::Rc;
65

76
use crate::daemon::{Daemon, DaemonClient, daemon_server};
@@ -44,8 +43,8 @@ fn main_keyboard(app: &gtk::Application, keyboard: Rc<Keyboard>) {
4443
});
4544
}
4645

47-
fn main_app(app: &gtk::Application, daemon: Rc<RefCell<dyn Daemon>>) {
48-
let boards = daemon.borrow_mut().boards().expect("Failed to load boards");
46+
fn main_app(app: &gtk::Application, daemon: Rc<dyn Daemon>) {
47+
let boards = daemon.boards().expect("Failed to load boards");
4948
let i = 0;
5049
if let Some(board) = boards.get(i) {
5150
if let Some(keyboard) = Keyboard::new_board(board, Some(daemon), i) {
@@ -63,7 +62,7 @@ fn main_app(app: &gtk::Application, daemon: Rc<RefCell<dyn Daemon>>) {
6362
}
6463

6564
#[cfg(target_os = "linux")]
66-
fn with_daemon<F: Fn(Rc<RefCell<dyn Daemon>>)>(f: F) {
65+
fn with_daemon<F: Fn(Rc<dyn Daemon>)>(f: F) {
6766
use std::{
6867
process::{
6968
Command,
@@ -74,7 +73,7 @@ fn with_daemon<F: Fn(Rc<RefCell<dyn Daemon>>)>(f: F) {
7473
if unsafe { libc::geteuid() == 0 } {
7574
eprintln!("Already running as root");
7675
let server = daemon_server().expect("Failed to create server");
77-
f(Rc::new(RefCell::new(server)));
76+
f(Rc::new(server));
7877
return;
7978
}
8079

@@ -96,7 +95,7 @@ fn with_daemon<F: Fn(Rc<RefCell<dyn Daemon>>)>(f: F) {
9695
let stdin = child.stdin.take().expect("Failed to get stdin of daemon");
9796
let stdout = child.stdout.take().expect("Failed to get stdout of daemon");
9897

99-
f(Rc::new(RefCell::new(DaemonClient::new(stdout, stdin))));
98+
f(Rc::new(DaemonClient::new(stdout, stdin)));
10099

101100
let status = child.wait().expect("Failed to wait for daemon");
102101
if ! status.success() {

src/daemon/client.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
cell::RefCell,
23
io::{
34
BufRead,
45
BufReader,
@@ -15,27 +16,27 @@ use super::{
1516
};
1617

1718
pub struct DaemonClient<R: Read, W: Write> {
18-
read: BufReader<R>,
19-
write: W,
19+
read: RefCell<BufReader<R>>,
20+
write: RefCell<W>,
2021
}
2122

2223
impl<R: Read, W: Write> DaemonClient<R, W> {
2324
pub fn new(read: R, write: W) -> Self {
2425
Self {
25-
read: BufReader::new(read),
26-
write,
26+
read: RefCell::new(BufReader::new(read)),
27+
write: RefCell::new(write),
2728
}
2829
}
2930
}
3031

3132
impl<R: std::io::Read, W: std::io::Write> DaemonClientTrait for DaemonClient<R, W> {
32-
fn send_command(&mut self, command: DaemonCommand) -> Result<DaemonResponse, String> {
33+
fn send_command(&self, command: DaemonCommand) -> Result<DaemonResponse, String> {
3334
let mut command_json = serde_json::to_string(&command).map_err(err_str)?;
3435
command_json.push('\n');
35-
self.write.write_all(command_json.as_bytes()).map_err(err_str)?;
36+
self.write.borrow_mut().write_all(command_json.as_bytes()).map_err(err_str)?;
3637

3738
let mut response_json = String::new();
38-
self.read.read_line(&mut response_json).map_err(err_str)?;
39+
self.read.borrow_mut().read_line(&mut response_json).map_err(err_str)?;
3940
serde_json::from_str(&response_json).map_err(err_str)?
4041
}
4142
}

src/daemon/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ pub use self::server::DaemonServer;
1010
mod server;
1111

1212
pub trait DaemonClientTrait {
13-
fn send_command(&mut self, command: DaemonCommand) -> Result<DaemonResponse, String>;
13+
fn send_command(&self, command: DaemonCommand) -> Result<DaemonResponse, String>;
1414
}
1515

1616
// Define Daemon trait, DaemonCommand enum, and DaemonResponse enum
1717
macro_rules! commands {
18-
( $( fn $func:ident(&mut self $(,)? $( $arg:ident: $type:ty ),*) -> Result<$ret:ty, String>; )* ) => {
18+
( $( fn $func:ident(&self $(,)? $( $arg:ident: $type:ty ),*) -> Result<$ret:ty, String>; )* ) => {
1919
pub trait Daemon {
2020
$(
21-
fn $func(&mut self, $( $arg: $type ),*) -> Result<$ret, String>;
21+
fn $func(&self, $( $arg: $type ),*) -> Result<$ret, String>;
2222
)*
2323

24-
fn dispatch_command_to_method(&mut self, command: DaemonCommand) -> Result<DaemonResponse, String> {
24+
fn dispatch_command_to_method(&self, command: DaemonCommand) -> Result<DaemonResponse, String> {
2525
match command {
2626
$(
2727
DaemonCommand::$func{$( $arg ),*} => {
@@ -52,7 +52,7 @@ macro_rules! commands {
5252

5353
impl<T: DaemonClientTrait> Daemon for T {
5454
$(
55-
fn $func(&mut self, $( $arg: $type ),*) -> Result<$ret, String> {
55+
fn $func(&self, $( $arg: $type ),*) -> Result<$ret, String> {
5656
let res = self.send_command(DaemonCommand::$func{$( $arg ),*});
5757
match res {
5858
Ok(DaemonResponse::$func(ret)) => Ok(ret),
@@ -66,15 +66,15 @@ macro_rules! commands {
6666
}
6767

6868
commands! {
69-
fn boards(&mut self) -> Result<Vec<String>, String>;
70-
fn keymap_get(&mut self, board: usize, layer: u8, output: u8, input: u8) -> Result<u16, String>;
71-
fn keymap_set(&mut self, board: usize, layer: u8, output: u8, input: u8, value: u16) -> Result<(), String>;
72-
fn color(&mut self, board: usize) -> Result<Rgb, String>;
73-
fn set_color(&mut self, board: usize, color: Rgb) -> Result<(), String>;
74-
fn max_brightness(&mut self, board: usize) -> Result<i32, String>;
75-
fn brightness(&mut self, board: usize) -> Result<i32, String>;
76-
fn set_brightness(&mut self, board: usize, brightness: i32) -> Result<(), String>;
77-
fn exit(&mut self) -> Result<(), String>;
69+
fn boards(&self) -> Result<Vec<String>, String>;
70+
fn keymap_get(&self, board: usize, layer: u8, output: u8, input: u8) -> Result<u16, String>;
71+
fn keymap_set(&self, board: usize, layer: u8, output: u8, input: u8, value: u16) -> Result<(), String>;
72+
fn color(&self, board: usize) -> Result<Rgb, String>;
73+
fn set_color(&self, board: usize, color: Rgb) -> Result<(), String>;
74+
fn max_brightness(&self, board: usize) -> Result<i32, String>;
75+
fn brightness(&self, board: usize) -> Result<i32, String>;
76+
fn set_brightness(&self, board: usize, brightness: i32) -> Result<(), String>;
77+
fn exit(&self) -> Result<(), String>;
7878
}
7979

8080
fn err_str<E: std::fmt::Debug>(err: E) -> String {

0 commit comments

Comments
 (0)