I cant await for a function if i use a Tauri function.
I am really new to RUST and dont know much about futures and stuff, My program worked well in the terminal but now i just want to make a good UI so i am using tauri, it does not allow me to call an async function
Main.rs
#[tauri::command(rename_all = "snake_case")]
async fn url_entered(url: &str, img_box_checked:bool, pdf_box_checked:bool, subfolder_box_checked:bool) -> Result<String, ()> {
println!("IMAGE:{} | PDF:{} | Folder:{} | URL: {}", img_box_checked, pdf_box_checked, subfolder_box_checked, url);
let _ = get_table(url, CURRENT_DIRECTORY, pdf_box_checked, img_box_checked, subfolder_box_checked).await; // Await here causes error
Ok(format!("Sending request to: {}", url))
}
Main.js
const { invoke } = window.__TAURI__.tauri;
let urlInputEl;
let urlMsgEl;
let downloadPdfs;
let downloadImages;
let scanSubfolders;
async function url_submitted() {
urlMsgEl.textContent = "Trying to download...";
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
const { url, img_box_checked, pdf_box_checked, subfolder_box_checked } = {
url: urlInputEl.value,
img_box_checked: downloadImages.checked,
pdf_box_checked: downloadPdfs.checked,
subfolder_box_checked: scanSubfolders.checked
};
urlMsgEl.textContent = await invoke("url_entered", { url, img_box_checked, pdf_box_checked, subfolder_box_checked }).then(() =>
console.log('Completed!'));
}
window.addEventListener("DOMContentLoaded", () => {
urlInputEl = document.querySelector("#url-input");
urlMsgEl = document.querySelector("#url-msg");
downloadPdfs = document.querySelector("#DownloadPdfs");
downloadImages = document.querySelector("#DownloadImages");
scanSubfolders = document.querySelector("#ScanSubfolders");
document.querySelector("#url-form").addEventListener("submit", (e) => {
e.preventDefault();
url_submitted();
});
});
There is a button in html:
<button type="submit">Enter</button>
error: future cannot be sent between threads safely
--> srcmain.rs:214:1
|
214 | #[tauri::command(rename_all = "snake_case")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `url_entered` is not `Send`
...
231 | .invoke_handler(tauri::generate_handler![url_entered])
| ------------------------------------- in this macro invocation
|
= help: within `ego_tree::Node<scraper::Node>`, the trait `Sync` is not implemented for `std::cell::Cell<NonZero<usize>>`, which is required by `impl futures::Future<Output = Result<String, ()>>: std::marker::Send`
= note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock`
note: future is not `Send` as this value is used across an await
--> srcmain.rs:89:90
|
35 | for table in html.select(&table_selector){ // get all tables from html
| ----- has type `ElementRef<'_>` which is not `Send`
...
89 | download_pdfs, download_imgs, scan_subfolders)).await?; // Call get_table function with new url
| ^^^^^ await occurs here, with `table` maybe used later
note: required by a bound in `ResultFutureTag::future`
--> C:UsersDELL.cargoregistrysrcindex.crates.io-6f17d22bba15001ftauri-1.6.8srccommand.rs:293:42
|
289 | pub fn future<T, E, F>(self, value: F) -> impl Future<Output = Result<Value, InvokeError>>
| ------ required by a bound in this associated function
...
293 | F: Future<Output = Result<T, E>> + Send,
| ^^^^ required by this bound in `ResultFutureTag::future`
= note: this error originates in the macro `__cmd__url_entered` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
I tried searching on google and use codeium but nothing worked.