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