tony / oxwm

Fixed box::leak issue. Browse files
tonybtw committed 2025-10-15 parent ed311f0 commit f188c7767dfd3ae1c3079f5073cbe01ff6c865c6
Showing 4 changed files with 63 additions and 84 deletions. UnifiedSplit
+11 −11 src/bar/blocks/mod.rs
@@ -19,7 +19,7 @@ pub trait Block {
19 19
20 #[derive(Clone)] 20 #[derive(Clone)]
21 pub struct BlockConfig { 21 pub struct BlockConfig {
22 - pub format: &'static str, 22 + pub format: String,
23 pub command: BlockCommand, 23 pub command: BlockCommand,
24 pub interval_secs: u64, 24 pub interval_secs: u64,
25 pub color: u32, 25 pub color: u32,
@@ -28,28 +28,28 @@ pub struct BlockConfig {
28 28
29 #[derive(Clone)] 29 #[derive(Clone)]
30 pub enum BlockCommand { 30 pub enum BlockCommand {
31 - Shell(&'static str), 31 + Shell(String),
32 - DateTime(&'static str), 32 + DateTime(String),
33 Battery { 33 Battery {
34 - format_charging: &'static str, 34 + format_charging: String,
35 - format_discharging: &'static str, 35 + format_discharging: String,
36 - format_full: &'static str, 36 + format_full: String,
37 }, 37 },
38 Ram, 38 Ram,
39 - Static(&'static str), 39 + Static(String),
40 } 40 }
41 41
42 impl BlockConfig { 42 impl BlockConfig {
43 pub fn to_block(&self) -> Box<dyn Block> { 43 pub fn to_block(&self) -> Box<dyn Block> {
44 - match self.command { 44 + match &self.command {
45 BlockCommand::Shell(cmd) => Box::new(ShellBlock::new( 45 BlockCommand::Shell(cmd) => Box::new(ShellBlock::new(
46 - self.format, 46 + &self.format,
47 cmd, 47 cmd,
48 self.interval_secs, 48 self.interval_secs,
49 self.color, 49 self.color,
50 )), 50 )),
51 BlockCommand::DateTime(fmt) => Box::new(DateTime::new( 51 BlockCommand::DateTime(fmt) => Box::new(DateTime::new(
52 - self.format, 52 + &self.format,
53 fmt, 53 fmt,
54 self.interval_secs, 54 self.interval_secs,
55 self.color, 55 self.color,
@@ -65,7 +65,7 @@ impl BlockConfig {
65 self.interval_secs, 65 self.interval_secs,
66 self.color, 66 self.color,
67 )), 67 )),
68 - BlockCommand::Ram => Box::new(Ram::new(self.format, self.interval_secs, self.color)), 68 + BlockCommand::Ram => Box::new(Ram::new(&self.format, self.interval_secs, self.color)),
69 BlockCommand::Static(text) => Box::new(StaticBlock::new( 69 BlockCommand::Static(text) => Box::new(StaticBlock::new(
70 &format!("{}{}", self.format, text), 70 &format!("{}{}", self.format, text),
71 self.color, 71 self.color,
+11 −28 src/config/mod.rs
@@ -98,19 +98,15 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError>
98 .map(|s| parse_modkey(s)) 98 .map(|s| parse_modkey(s))
99 .collect::<Result<Vec<_>, _>>()?; 99 .collect::<Result<Vec<_>, _>>()?;
100 100
101 - let modifiers_static: &'static [KeyButMask] = Box::leak(modifiers.into_boxed_slice());
102 -
103 let key = string_to_keycode(&kb_data.key)?; 101 let key = string_to_keycode(&kb_data.key)?;
104 let action = parse_key_action(&kb_data.action)?; 102 let action = parse_key_action(&kb_data.action)?;
105 let arg = arg_data_to_arg(kb_data.arg)?; 103 let arg = arg_data_to_arg(kb_data.arg)?;
106 104
107 - keybindings.push(Key::new(modifiers_static, key, action, arg)); 105 + keybindings.push(Key::new(modifiers, key, action, arg));
108 } 106 }
109 107
110 let mut status_blocks = Vec::new(); 108 let mut status_blocks = Vec::new();
111 for block_data in data.status_blocks { 109 for block_data in data.status_blocks {
112 - let format_static: &'static str = Box::leak(block_data.format.into_boxed_str());
113 -
114 let command = match block_data.command.as_str() { 110 let command = match block_data.command.as_str() {
115 "DateTime" => { 111 "DateTime" => {
116 let fmt = block_data 112 let fmt = block_data
@@ -119,8 +115,7 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError>
119 command: "DateTime".to_string(), 115 command: "DateTime".to_string(),
120 field: "command_arg".to_string(), 116 field: "command_arg".to_string(),
121 })?; 117 })?;
122 - let fmt_static: &'static str = Box::leak(fmt.into_boxed_str()); 118 + BlockCommand::DateTime(fmt)
123 - BlockCommand::DateTime(fmt_static)
124 } 119 }
125 "Shell" => { 120 "Shell" => {
126 let cmd = block_data 121 let cmd = block_data
@@ -129,14 +124,12 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError>
129 command: "Shell".to_string(), 124 command: "Shell".to_string(),
130 field: "command_arg".to_string(), 125 field: "command_arg".to_string(),
131 })?; 126 })?;
132 - let cmd_static: &'static str = Box::leak(cmd.into_boxed_str()); 127 + BlockCommand::Shell(cmd)
133 - BlockCommand::Shell(cmd_static)
134 } 128 }
135 "Ram" => BlockCommand::Ram, 129 "Ram" => BlockCommand::Ram,
136 "Static" => { 130 "Static" => {
137 let text = block_data.command_arg.unwrap_or_default(); 131 let text = block_data.command_arg.unwrap_or_default();
138 - let text_static: &'static str = Box::leak(text.into_boxed_str()); 132 + BlockCommand::Static(text)
139 - BlockCommand::Static(text_static)
140 } 133 }
141 "Battery" => { 134 "Battery" => {
142 let formats = 135 let formats =
@@ -147,16 +140,16 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError>
147 field: "battery_formats".to_string(), 140 field: "battery_formats".to_string(),
148 })?; 141 })?;
149 BlockCommand::Battery { 142 BlockCommand::Battery {
150 - format_charging: Box::leak(formats.charging.into_boxed_str()), 143 + format_charging: formats.charging,
151 - format_discharging: Box::leak(formats.discharging.into_boxed_str()), 144 + format_discharging: formats.discharging,
152 - format_full: Box::leak(formats.full.into_boxed_str()), 145 + format_full: formats.full,
153 } 146 }
154 } 147 }
155 _ => return Err(ConfigError::UnknownBlockCommand(block_data.command)), 148 _ => return Err(ConfigError::UnknownBlockCommand(block_data.command)),
156 }; 149 };
157 150
158 status_blocks.push(BlockConfig { 151 status_blocks.push(BlockConfig {
159 - format: format_static, 152 + format: block_data.format,
160 command, 153 command,
161 interval_secs: block_data.interval_secs, 154 interval_secs: block_data.interval_secs,
162 color: block_data.color, 155 color: block_data.color,
@@ -312,21 +305,11 @@ fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction, ConfigError>
312 } 305 }
313 } 306 }
314 307
315 -fn arg_data_to_arg(data: ArgData) -> Result<crate::keyboard::Arg, ConfigError> { 308 +fn arg_data_to_arg(data: ArgData) -> Result<Arg, ConfigError> {
316 match data { 309 match data {
317 ArgData::None => Ok(Arg::None), 310 ArgData::None => Ok(Arg::None),
318 - ArgData::String(s) => { 311 + ArgData::String(s) => Ok(Arg::Str(s)),
319 - let static_str: &'static str = Box::leak(s.into_boxed_str());
320 - Ok(Arg::Str(static_str))
321 - }
322 ArgData::Int(n) => Ok(Arg::Int(n)), 312 ArgData::Int(n) => Ok(Arg::Int(n)),
323 - ArgData::Array(arr) => { 313 + ArgData::Array(arr) => Ok(Arg::Array(arr)),
324 - let static_strs: Vec<&'static str> = arr
325 - .into_iter()
326 - .map(|s| Box::leak(s.into_boxed_str()) as &'static str)
327 - .collect();
328 - let static_slice: &'static [&'static str] = Box::leak(static_strs.into_boxed_slice());
329 - Ok(Arg::Array(static_slice))
330 - }
331 } 314 }
332 } 315 }
+9 −13 src/keyboard/handlers.rs
@@ -26,8 +26,8 @@ pub enum KeyAction {
26 pub enum Arg { 26 pub enum Arg {
27 None, 27 None,
28 Int(i32), 28 Int(i32),
29 - Str(&'static str), 29 + Str(String),
30 - Array(&'static [&'static str]), 30 + Array(Vec<String>),
31 } 31 }
32 32
33 impl Arg { 33 impl Arg {
@@ -38,19 +38,14 @@ impl Arg {
38 38
39 #[derive(Clone)] 39 #[derive(Clone)]
40 pub struct Key { 40 pub struct Key {
41 - pub(crate) modifiers: &'static [KeyButMask], 41 + pub(crate) modifiers: Vec<KeyButMask>,
42 pub(crate) key: Keycode, 42 pub(crate) key: Keycode,
43 pub(crate) func: KeyAction, 43 pub(crate) func: KeyAction,
44 pub(crate) arg: Arg, 44 pub(crate) arg: Arg,
45 } 45 }
46 46
47 impl Key { 47 impl Key {
48 - pub const fn new( 48 + pub fn new(modifiers: Vec<KeyButMask>, key: Keycode, func: KeyAction, arg: Arg) -> Self {
49 - modifiers: &'static [KeyButMask],
50 - key: Keycode,
51 - func: KeyAction,
52 - arg: Arg,
53 - ) -> Self {
54 Self { 49 Self {
55 modifiers, 50 modifiers,
56 key, 51 key,
@@ -72,7 +67,7 @@ pub fn setup_keybinds(
72 keybindings: &[Key], 67 keybindings: &[Key],
73 ) -> Result<(), X11Error> { 68 ) -> Result<(), X11Error> {
74 for keybinding in keybindings { 69 for keybinding in keybindings {
75 - let modifier_mask = modifiers_to_mask(keybinding.modifiers); 70 + let modifier_mask = modifiers_to_mask(&keybinding.modifiers);
76 71
77 connection.grab_key( 72 connection.grab_key(
78 false, 73 false,
@@ -88,7 +83,7 @@ pub fn setup_keybinds(
88 83
89 pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> (KeyAction, Arg) { 84 pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> (KeyAction, Arg) {
90 for keybinding in keybindings { 85 for keybinding in keybindings {
91 - let modifier_mask = modifiers_to_mask(keybinding.modifiers); 86 + let modifier_mask = modifiers_to_mask(&keybinding.modifiers);
92 87
93 if event.detail == keybinding.key && event.state == modifier_mask.into() { 88 if event.detail == keybinding.key && event.state == modifier_mask.into() {
94 return (keybinding.func, keybinding.arg.clone()); 89 return (keybinding.func, keybinding.arg.clone());
@@ -102,7 +97,7 @@ pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> io::Result<()> {
102 use io::ErrorKind; 97 use io::ErrorKind;
103 if let KeyAction::Spawn = action { 98 if let KeyAction::Spawn = action {
104 match arg { 99 match arg {
105 - Arg::Str(command) => match Command::new(command).spawn() { 100 + Arg::Str(command) => match Command::new(command.as_str()).spawn() {
106 Err(err) if err.kind() == ErrorKind::NotFound => { 101 Err(err) if err.kind() == ErrorKind::NotFound => {
107 eprintln!( 102 eprintln!(
108 "KeyAction::Spawn failed: could not spawn \"{}\", command not found", 103 "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
@@ -117,7 +112,8 @@ pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> io::Result<()> {
117 return Ok(()); 112 return Ok(());
118 }; 113 };
119 114
120 - match Command::new(cmd).args(args).spawn() { 115 + let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
116 + match Command::new(cmd.as_str()).args(&args_str).spawn() {
121 Err(err) if err.kind() == ErrorKind::NotFound => { 117 Err(err) if err.kind() == ErrorKind::NotFound => {
122 eprintln!( 118 eprintln!(
123 "KeyAction::Spawn failed: could not spawn \"{}\", command not found", 119 "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
+32 −32 src/lib.rs
@@ -82,97 +82,97 @@ impl Default for Config {
82 .collect(), 82 .collect(),
83 keybindings: vec![ 83 keybindings: vec![
84 Key::new( 84 Key::new(
85 - &[MODKEY], 85 + vec![MODKEY],
86 keycodes::RETURN, 86 keycodes::RETURN,
87 KeyAction::Spawn, 87 KeyAction::Spawn,
88 - Arg::Str(TERMINAL), 88 + Arg::Str(TERMINAL.to_string()),
89 ), 89 ),
90 Key::new( 90 Key::new(
91 - &[MODKEY], 91 + vec![MODKEY],
92 keycodes::D, 92 keycodes::D,
93 KeyAction::Spawn, 93 KeyAction::Spawn,
94 - Arg::Array(&["sh", "-c", "dmenu_run -l 10"]), 94 + Arg::Array(vec!["sh".to_string(), "-c".to_string(), "dmenu_run -l 10".to_string()]),
95 ), 95 ),
96 - Key::new(&[MODKEY], keycodes::Q, KeyAction::KillClient, Arg::None), 96 + Key::new(vec![MODKEY], keycodes::Q, KeyAction::KillClient, Arg::None),
97 Key::new( 97 Key::new(
98 - &[MODKEY, SHIFT], 98 + vec![MODKEY, SHIFT],
99 keycodes::F, 99 keycodes::F,
100 KeyAction::ToggleFullScreen, 100 KeyAction::ToggleFullScreen,
101 Arg::None, 101 Arg::None,
102 ), 102 ),
103 - Key::new(&[MODKEY], keycodes::A, KeyAction::ToggleGaps, Arg::None), 103 + Key::new(vec![MODKEY], keycodes::A, KeyAction::ToggleGaps, Arg::None),
104 - Key::new(&[MODKEY, SHIFT], keycodes::Q, KeyAction::Quit, Arg::None), 104 + Key::new(vec![MODKEY, SHIFT], keycodes::Q, KeyAction::Quit, Arg::None),
105 - Key::new(&[MODKEY, SHIFT], keycodes::R, KeyAction::Restart, Arg::None), 105 + Key::new(vec![MODKEY, SHIFT], keycodes::R, KeyAction::Restart, Arg::None),
106 - Key::new(&[MODKEY], keycodes::F, KeyAction::ToggleFloating, Arg::None), 106 + Key::new(vec![MODKEY], keycodes::F, KeyAction::ToggleFloating, Arg::None),
107 - Key::new(&[MODKEY], keycodes::J, KeyAction::FocusStack, Arg::Int(-1)), 107 + Key::new(vec![MODKEY], keycodes::J, KeyAction::FocusStack, Arg::Int(-1)),
108 - Key::new(&[MODKEY], keycodes::K, KeyAction::FocusStack, Arg::Int(1)), 108 + Key::new(vec![MODKEY], keycodes::K, KeyAction::FocusStack, Arg::Int(1)),
109 - Key::new(&[MODKEY], keycodes::KEY_1, KeyAction::ViewTag, Arg::Int(0)), 109 + Key::new(vec![MODKEY], keycodes::KEY_1, KeyAction::ViewTag, Arg::Int(0)),
110 - Key::new(&[MODKEY], keycodes::KEY_2, KeyAction::ViewTag, Arg::Int(1)), 110 + Key::new(vec![MODKEY], keycodes::KEY_2, KeyAction::ViewTag, Arg::Int(1)),
111 - Key::new(&[MODKEY], keycodes::KEY_3, KeyAction::ViewTag, Arg::Int(2)), 111 + Key::new(vec![MODKEY], keycodes::KEY_3, KeyAction::ViewTag, Arg::Int(2)),
112 - Key::new(&[MODKEY], keycodes::KEY_4, KeyAction::ViewTag, Arg::Int(3)), 112 + Key::new(vec![MODKEY], keycodes::KEY_4, KeyAction::ViewTag, Arg::Int(3)),
113 - Key::new(&[MODKEY], keycodes::KEY_5, KeyAction::ViewTag, Arg::Int(4)), 113 + Key::new(vec![MODKEY], keycodes::KEY_5, KeyAction::ViewTag, Arg::Int(4)),
114 - Key::new(&[MODKEY], keycodes::KEY_6, KeyAction::ViewTag, Arg::Int(5)), 114 + Key::new(vec![MODKEY], keycodes::KEY_6, KeyAction::ViewTag, Arg::Int(5)),
115 - Key::new(&[MODKEY], keycodes::KEY_7, KeyAction::ViewTag, Arg::Int(6)), 115 + Key::new(vec![MODKEY], keycodes::KEY_7, KeyAction::ViewTag, Arg::Int(6)),
116 - Key::new(&[MODKEY], keycodes::KEY_8, KeyAction::ViewTag, Arg::Int(7)), 116 + Key::new(vec![MODKEY], keycodes::KEY_8, KeyAction::ViewTag, Arg::Int(7)),
117 - Key::new(&[MODKEY], keycodes::KEY_9, KeyAction::ViewTag, Arg::Int(8)), 117 + Key::new(vec![MODKEY], keycodes::KEY_9, KeyAction::ViewTag, Arg::Int(8)),
118 Key::new( 118 Key::new(
119 - &[MODKEY, SHIFT], 119 + vec![MODKEY, SHIFT],
120 keycodes::KEY_1, 120 keycodes::KEY_1,
121 KeyAction::MoveToTag, 121 KeyAction::MoveToTag,
122 Arg::Int(0), 122 Arg::Int(0),
123 ), 123 ),
124 Key::new( 124 Key::new(
125 - &[MODKEY, SHIFT], 125 + vec![MODKEY, SHIFT],
126 keycodes::KEY_2, 126 keycodes::KEY_2,
127 KeyAction::MoveToTag, 127 KeyAction::MoveToTag,
128 Arg::Int(1), 128 Arg::Int(1),
129 ), 129 ),
130 Key::new( 130 Key::new(
131 - &[MODKEY, SHIFT], 131 + vec![MODKEY, SHIFT],
132 keycodes::KEY_3, 132 keycodes::KEY_3,
133 KeyAction::MoveToTag, 133 KeyAction::MoveToTag,
134 Arg::Int(2), 134 Arg::Int(2),
135 ), 135 ),
136 Key::new( 136 Key::new(
137 - &[MODKEY, SHIFT], 137 + vec![MODKEY, SHIFT],
138 keycodes::KEY_4, 138 keycodes::KEY_4,
139 KeyAction::MoveToTag, 139 KeyAction::MoveToTag,
140 Arg::Int(3), 140 Arg::Int(3),
141 ), 141 ),
142 Key::new( 142 Key::new(
143 - &[MODKEY, SHIFT], 143 + vec![MODKEY, SHIFT],
144 keycodes::KEY_5, 144 keycodes::KEY_5,
145 KeyAction::MoveToTag, 145 KeyAction::MoveToTag,
146 Arg::Int(4), 146 Arg::Int(4),
147 ), 147 ),
148 Key::new( 148 Key::new(
149 - &[MODKEY, SHIFT], 149 + vec![MODKEY, SHIFT],
150 keycodes::KEY_6, 150 keycodes::KEY_6,
151 KeyAction::MoveToTag, 151 KeyAction::MoveToTag,
152 Arg::Int(5), 152 Arg::Int(5),
153 ), 153 ),
154 Key::new( 154 Key::new(
155 - &[MODKEY, SHIFT], 155 + vec![MODKEY, SHIFT],
156 keycodes::KEY_7, 156 keycodes::KEY_7,
157 KeyAction::MoveToTag, 157 KeyAction::MoveToTag,
158 Arg::Int(6), 158 Arg::Int(6),
159 ), 159 ),
160 Key::new( 160 Key::new(
161 - &[MODKEY, SHIFT], 161 + vec![MODKEY, SHIFT],
162 keycodes::KEY_8, 162 keycodes::KEY_8,
163 KeyAction::MoveToTag, 163 KeyAction::MoveToTag,
164 Arg::Int(7), 164 Arg::Int(7),
165 ), 165 ),
166 Key::new( 166 Key::new(
167 - &[MODKEY, SHIFT], 167 + vec![MODKEY, SHIFT],
168 keycodes::KEY_9, 168 keycodes::KEY_9,
169 KeyAction::MoveToTag, 169 KeyAction::MoveToTag,
170 Arg::Int(8), 170 Arg::Int(8),
171 ), 171 ),
172 ], 172 ],
173 status_blocks: vec![crate::bar::BlockConfig { 173 status_blocks: vec![crate::bar::BlockConfig {
174 - format: "{}", 174 + format: "{}".to_string(),
175 - command: crate::bar::BlockCommand::DateTime("%a, %b %d - %-I:%M %P"), 175 + command: crate::bar::BlockCommand::DateTime("%a, %b %d - %-I:%M %P".to_string()),
176 interval_secs: 1, 176 interval_secs: 1,
177 color: 0x0db9d7, 177 color: 0x0db9d7,
178 underline: true, 178 underline: true,