Hi there! I have this working code:
const CMDS: &'static [&[&str]] = &[
&[",zone=nvidia xps15manjaro.temperature=", "nvidia-smi","--query-gpu=temperature.gpu","--format=csv,noheader"],
];
#[tokio::main]
async fn main() -> io::Result<()> {
let (tx, rx) = mpsc::channel::<String>(1024);
let mut handles = vec![];
handles.push(tokio::spawn(data_sender(rx)));
for def in CMDS {
let mut cmd = Command::new(def[1]);
for (i,arg) in def.iter().enumerate() {
if i > 1 {
cmd.arg(arg);
}
}
println!("{:?}", cmd);
handles.push(tokio::spawn(metrics_sender(tx.clone(), String::from(def[0]), &[], move || {
let result = cmd.output().unwrap();
String::from(str::from_utf8(&result.stdout).unwrap().trim())
})));
}
futures::future::join_all(handles).await;
Ok(())
}
(I've tried to remove all the code irrelevant to the question to make it easier to grasp.)
Now since I consider it ugly and bad practice to assign def[0] and def[1] the special meanings they have, I'd prefer to use a struct, but my attempt to do so:
struct ExternalSource<'a> {
prefix: &'a str,
cmd: &'a str,
args: [&'a str],
}
const EXTERNAL_SOURCES: &'static [&ExternalSource<'static>] = &[&ExternalSource{
prefix: ",zone=nvidia xps15manjaro.temperature=",
cmd: "nvidia-smi",
args: ["--query-gpu=temperature.gpu","--format=csv,noheader"],
}];
fails with this compiler errors:
error[E0308]: mismatched types
--> src/main.rs:33:11
|
33 | args: ["--query-gpu=temperature.gpu","--format=csv,noheader"],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[&str]`, found array `[&str; 2]`
error[E0277]: the size for values of type `[&str]` cannot be known at compilation time
--> src/main.rs:30:66
|
30 | const EXTERNAL_SOURCES: &'static [&ExternalSource<'static>] = &[&ExternalSource{
| __________________________________________________________________^
31 | | prefix: ",zone=nvidia xps15manjaro.temperature=",
32 | | cmd: "nvidia-smi",
33 | | args: ["--query-gpu=temperature.gpu","--format=csv,noheader"],
34 | | }];
| |_^ doesn't have a size known at compile-time
|
= help: within `ExternalSource<'_>`, the trait `Sized` is not implemented for `[&str]`
note: required because it appears within the type `ExternalSource<'_>`
--> src/main.rs:24:8
|
24 | struct ExternalSource<'a> {
| ^^^^^^^^^^^^^^
= note: structs must have a statically known size to be initialized
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.