Ok, so to begin with I'm relatively new to rust / learning it currently. But with a lot of experience with other languages. So whilst in other languages I would probably use a global variable for this, in rust this would require an unsafe marker and I want to try to avoid this. So I wanted to ask what would be the solution the rust way.
I am working on a little Tauri-App, that plays audio (main objective is to learn more rust and tauri) via a rodio::Sink. To do that, a button opens a file selector dialog and triggers a function that opens a Sink in a separate Thread and plays the audio from the file (separate thread so that Tauri GUI doesn't freeze up)
#[tauri::command]
fn select_file(path: &str) {
let s_path = path.to_string();
thread::spawn(move || {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
utils::play(&s_path.clone(),&sink);
sink.sleep_until_end();
});
}
To note here: the utils::play() is my function that plays the audio
btw. up to that point everything works.
Now to my main Question:
In the utils::play function I will put a Receiver that, if sent a certain value, pauses or resumes the audio playback. The Receiver could be put in that thread at the spawn point, but how can I store the Sender to access it from a separate tauri command function.
In other languages I would've probably used a global variable, but I'm not sure if this is really the right way to do this in rust, and if declaring a public channel (or just public sender is such a good option).
So is it acceptable in your eyes to use a public Sender / Channel or is there some sort of way to do this smarter, that I'm just completely missing.
Thanks in advance