summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-17 19:24:00 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-17 21:28:53 +0800
commitb7bc239c47d419ea5c5b078b89ca11def6c7311b (patch)
treebaa005a248467d8347c5e3537aefae47ad6340dd
parentd3e755389ad61a262767f4a0cb48c0a2f85c80c6 (diff)
perf: more prevalent &str
-rw-r--r--src/client/connector.rs4
-rw-r--r--src/client/connector/httpconnector.rs4
-rw-r--r--src/client/executor.rs10
-rw-r--r--src/tasks.rs12
4 files changed, 15 insertions, 15 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs
index c16b4bc..50d5f65 100644
--- a/src/client/connector.rs
+++ b/src/client/connector.rs
@@ -23,7 +23,7 @@ pub trait Connection {
fn get_ip(&self) -> Option<IpAddr>;
fn force_network(&mut self) {
- _ = Executor::new(&NETWORK_T, ["on".to_string()]);
+ _ = Executor::new(&NETWORK_T, ["on"]);
}
fn init<'a>(&self, c: &crate::client::Client<'a, Self>) -> std::result::Result<(), Self::Err>
@@ -62,7 +62,7 @@ pub trait Connection {
None => return,
};
// FIXME: handle the errors... peak prod code
- self.ntfy(Executor::new(task.try_into().unwrap(), it.map(str::to_string)).unwrap().run().unwrap().clone().unwrap()).unwrap();
+ self.ntfy(Executor::new(task.try_into().unwrap(), it).unwrap().run().unwrap().clone().unwrap()).unwrap();
}
Result::Err(e) => {
#[cfg(debug_assertions)]
diff --git a/src/client/connector/httpconnector.rs b/src/client/connector/httpconnector.rs
index c6be503..9addf66 100644
--- a/src/client/connector/httpconnector.rs
+++ b/src/client/connector/httpconnector.rs
@@ -7,7 +7,7 @@ use reqwest::{
use std::{net::IpAddr, thread::sleep, time::Duration};
pub struct HTTPConnector {
- network_enforcer: Executor<'static>,
+ network_enforcer: Executor<'static, 'static>,
pull_url: Url,
ntfy_url: Url,
ip_getter_url: Url,
@@ -18,7 +18,7 @@ pub struct HTTPConnector {
impl HTTPConnector {
pub fn new(pull_url: &str, ntfy_url: &str, ip_getter_url: &str) -> std::result::Result<Self, url::ParseError> {
Ok(Self {
- network_enforcer: Executor::new(&NETWORK_T, ["on".to_string()]).unwrap(), // .unwrap() should never panic
+ network_enforcer: Executor::new(&NETWORK_T, ["on"]).unwrap(), // .unwrap() should never panic
prev: String::new(),
pull_url: Url::parse(pull_url)?,
ntfy_url: Url::parse(ntfy_url)?,
diff --git a/src/client/executor.rs b/src/client/executor.rs
index 35e3f75..b75040a 100644
--- a/src/client/executor.rs
+++ b/src/client/executor.rs
@@ -1,13 +1,13 @@
use remote_control_rs::tasks::{IntoTaskErr, Task};
-pub struct Executor<'a> {
- task: &'a Task<'a>,
- args: Box<[String]>,
+pub struct Executor<'a, 't> {
+ task: &'a Task<'t>,
+ args: Box<[&'a str]>,
output: Option<Result<String, String>>,
}
-impl<'a> Executor<'a> {
- pub fn new(task: &'a Task<'a>, args: impl IntoIterator<Item = String>) -> Result<Self, IntoTaskErr> {
+impl<'a, 't> Executor<'a, 't> {
+ pub fn new(task: &'t Task<'t>, args: impl IntoIterator<Item = &'a str>) -> Result<Self, IntoTaskErr> {
Ok(Self { task: task, args: args.into_iter().collect(), output: None })
}
diff --git a/src/tasks.rs b/src/tasks.rs
index 3c49c7a..9175214 100644
--- a/src/tasks.rs
+++ b/src/tasks.rs
@@ -1,6 +1,6 @@
pub struct Task<'a> {
pub name: &'a str,
- pub exec: fn(&[String]) -> Result<String, String>,
+ pub exec: fn(&[&str]) -> Result<String, String>,
}
impl<'a> PartialEq for &'a Task<'a> {
@@ -13,15 +13,15 @@ pub const TASKS: &[Task<'static>] = &[Task { name: "nop", exec: no_op }, Task {
pub const NETWORK_T: Task<'static> = Task { name: "network", exec: network };
-fn no_op(_: &[String]) -> Result<String, String> {
+fn no_op(_: &[&str]) -> Result<String, String> {
Ok("".to_string())
}
-fn exec(cmds: &[String]) -> Result<String, String> {
+fn exec(cmds: &[&str]) -> Result<String, String> {
use std::process::Command;
// TODO: fmt and add exit code, and add support for both stdout and stderr, and
// support for no output
- match Command::new(cmds[0].as_str()).args(&cmds[1..]).output() {
+ match Command::new(cmds[0]).args(&cmds[1..]).output() {
Ok(o) => {
if o.status.success() {
Ok(String::from_utf8_lossy(&o.stdout).to_string())
@@ -33,7 +33,7 @@ fn exec(cmds: &[String]) -> Result<String, String> {
}
}
-fn network(switch: &[String]) -> Result<String, String> {
+fn network(switch: &[&str]) -> Result<String, String> {
use std::process::{Command, Stdio};
let switch: bool = switch[0].parse::<bool>().map_err(|e| e.to_string())?;
@@ -112,7 +112,7 @@ pub enum IntoTaskErr {
NotFound(String),
}
-impl<'a> TryFrom<&str> for &'a Task<'a> {
+impl TryFrom<&str> for &'static Task<'static> {
type Error = IntoTaskErr;
fn try_from(value: &str) -> Result<Self, Self::Error> {