From 06549c35abec283b38e2333772e72e80170f8ac5 Mon Sep 17 00:00:00 2001 From: z_lenovo Date: Sat, 28 Jun 2025 19:54:11 +0800 Subject: [PATCH] correct the update method to get the return type[Task] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 昨天用rustup更新了一下,不知道为什么连info都会提示我调了unsafe方法,要求我把它放进unsafe块中,日了狗,先记录一下再说吧 --- Cargo.toml | 3 +++ README.md | 1 - src/ui/db_browser.rs | 7 ++++--- src/ui/home_page.rs | 7 ++++--- src/ui/jlc_downloader.rs | 17 ++++++++++++++--- src/ui/main_window.rs | 36 +++++++++++++++++------------------- src/ui/part_viewer.rs | 7 ++++--- 7 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e0ae43..05cede9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ iced = { git = "https://github.com/iced-rs/iced.git", features = [ "advanced", "image", "sipper", + "tokio", ] } tokio = { version = "1.45.1", features = ["full"] } reqwest = "0.12.20" @@ -47,6 +48,8 @@ iced_futures = { version = "0.13.2", features = [ "thread-pool", ] } num_enum = "0.7.4" +trace = "0.1.7" +tracing = "0.1.41" [build-dependencies] diff --git a/README.md b/README.md index c33ef72..20158da 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,3 @@ ## 忐忑 * 本软件的3D封装下载调用了jlc的api,不知道哪天就收到了某函,所以暂时只在本站开源了,在未想办法解决掉该可能引起纠纷的事项之前不想广泛传播,所以也请各位道友手下留情,不要随意传播本软件 -中 东奔西跑黑暗 \ No newline at end of file diff --git a/src/ui/db_browser.rs b/src/ui/db_browser.rs index 7fad1a9..c668f4b 100644 --- a/src/ui/db_browser.rs +++ b/src/ui/db_browser.rs @@ -1,5 +1,5 @@ -use iced::{Length, alignment::Horizontal, widget::Column}; -use log::info; +use iced::{Length, Task, alignment::Horizontal, widget::Column}; +use tracing::info; use crate::ui::main_window::{MainWindowMsg, TabContent}; @@ -14,12 +14,13 @@ pub enum DbBrowserMsg { impl TabContent for DbBrowser { type TabMessage = DbBrowserMsg; - fn update(&mut self, msg: Self::TabMessage) { + fn update(&mut self, msg: Self::TabMessage) -> Task { match msg { DbBrowserMsg::Nothing => { info!("This function not allowed"); } } + Task::none() } fn content(&self) -> iced::Element<'_, MainWindowMsg> { diff --git a/src/ui/home_page.rs b/src/ui/home_page.rs index 966135a..ffe560b 100644 --- a/src/ui/home_page.rs +++ b/src/ui/home_page.rs @@ -1,5 +1,5 @@ -use iced::{Length, alignment::Horizontal, widget::Column}; -use log::info; +use iced::{Length, Task, alignment::Horizontal, widget::Column}; +use tracing::info; #[allow(unused_imports)] use crate::ui::main_window::{MainWindowMsg, TabContent}; @@ -66,7 +66,7 @@ impl TabContent for HomePage { type TabMessage = HomePageMsg; - fn update(&mut self, msg: Self::TabMessage) { + fn update(&mut self, msg: Self::TabMessage) -> Task { match msg { HomePageMsg::Nothing => { info!("This way ok."); @@ -96,5 +96,6 @@ impl TabContent for HomePage { info!("Is the message you should process ? =====>> {msg:?}"); } } + Task::none() } } diff --git a/src/ui/jlc_downloader.rs b/src/ui/jlc_downloader.rs index 89ca84a..90071fe 100644 --- a/src/ui/jlc_downloader.rs +++ b/src/ui/jlc_downloader.rs @@ -1,7 +1,7 @@ use crate::ui::main_window::{MainWindowMsg, TabContent}; use anyhow::Result; -use iced::{Length, alignment::Horizontal, widget::Column}; -use log::info; +use iced::{Length, Task, alignment::Horizontal, widget::Column}; +use tracing::info; #[derive(Default)] pub struct JlcDownloader { @@ -19,7 +19,7 @@ pub enum JlcDownloaderMsg { impl TabContent for JlcDownloader { type TabMessage = JlcDownloaderMsg; - fn update(&mut self, msg: Self::TabMessage) { + fn update(&mut self, msg: Self::TabMessage) -> Task { match msg { JlcDownloaderMsg::Nothing => { info!("JlcDownloaderMsg::Nothing"); @@ -34,6 +34,7 @@ impl TabContent for JlcDownloader { info!("Whach out the msg: {msg:?}"); } } + Task::none() } fn content(&self) -> iced::Element<'_, MainWindowMsg> { @@ -61,6 +62,13 @@ pub struct KeywordSearchItem { } impl JlcDownloader { pub async fn search_keyword(keyword: &str) -> Result> { + if keyword.is_empty() { + tokio::time::sleep(tokio::time::Duration::from_secs(1)); + info!("The keyword is empty."); + return Err(anyhow::Error::msg( + "Please insert the keyword before search", + )); + } tokio::time::sleep(tokio::time::Duration::from_secs(2)).await; for i in 0..10 { tokio::time::sleep(tokio::time::Duration::from_secs(2)).await; @@ -68,6 +76,9 @@ impl JlcDownloader { } Ok(Vec::new()) } + pub async fn keyword_search_task() -> Result> { + Ok(Vec::new()) + } pub async fn fetch_item_images( item: &KeywordSearchItem, ) -> Result> { diff --git a/src/ui/main_window.rs b/src/ui/main_window.rs index 7d3a150..fc27702 100644 --- a/src/ui/main_window.rs +++ b/src/ui/main_window.rs @@ -7,8 +7,8 @@ use crate::ui::part_viewer::PartViewerMsg; use crate::utils::step_downloader; use iced::Subscription; use iced::Task; -use log::info; -use log::warn; +use tracing::info; +use tracing::warn; #[allow(unused_imports)] use super::db_browser; @@ -65,7 +65,7 @@ impl MainWindow { ( Self::default(), Task::batch([ - Task::perform(JlcDownloader::search_keyword("test"), |x| match x { + Task::perform(JlcDownloader::search_keyword(""), |x| match x { Ok(v) => MainWindowMsg::JlcDownloader(JlcDownloaderMsg::KeywordSearchResult(v)), Err(e) => MainWindowMsg::JlcDownloader(JlcDownloaderMsg::KeywordSearchError( format!("{e:?}"), @@ -192,7 +192,7 @@ impl MainWindow { let btn = button(txt).style(bstyle); btn.on_press(MainWindowMsg::TabSelected(tab)).into() } - fn update(&mut self, msg: MainWindowMsg) { + fn update(&mut self, msg: MainWindowMsg) -> Task { info!("Process the msg: {msg:?}"); match msg { MainWindowMsg::ThemeChanged(theme) => { @@ -200,37 +200,35 @@ impl MainWindow { if let Some(idx) = Theme::ALL.iter().position(|x| x == &theme) { crate::utils::app_settings::set_curr_theme(idx as u32).unwrap(); } + Task::none() } MainWindowMsg::TitleChanged(title) => { self.title = title; + Task::none() } MainWindowMsg::Nothing => { info!("Nothing"); iced::debug::time("Test".to_owned()); + Task::none() } MainWindowMsg::TabSelected(i) => { self.curr_tab = i; self.curr_tab.save(); + Task::none() } MainWindowMsg::HomePage(msg) => { info!("update HomePage"); - self.home_page.update(msg); - } - MainWindowMsg::JlcDownloader(msg) => { - self.jlc_downloader.update(msg); - } - MainWindowMsg::DbBrowser(msg) => { - self.db_browser.update(msg); - } - MainWindowMsg::PartViewer(part_viewer_msg) => { - self.part_viewer.update(part_viewer_msg); - } - MainWindowMsg::SearchKeywordChanged(k) => { - self.jlc_downloader - .update(JlcDownloaderMsg::KeywordChanged(k)); + self.home_page.update(msg) } + MainWindowMsg::JlcDownloader(msg) => self.jlc_downloader.update(msg), + MainWindowMsg::DbBrowser(msg) => self.db_browser.update(msg), + MainWindowMsg::PartViewer(part_viewer_msg) => self.part_viewer.update(part_viewer_msg), + MainWindowMsg::SearchKeywordChanged(k) => self + .jlc_downloader + .update(JlcDownloaderMsg::KeywordChanged(k)), MainWindowMsg::StepDownloader(msg) => { info!("StepDownloaderEvent: {msg:?}"); + Task::none() } } } @@ -254,7 +252,7 @@ impl MainWindow { pub trait TabContent { type TabMessage; - fn update(&mut self, msg: Self::TabMessage); + fn update(&mut self, msg: Self::TabMessage) -> Task; fn view(&self) -> Element<'_, MainWindowMsg> { iced::widget::Container::new(self.content()) .width(Length::Fill) diff --git a/src/ui/part_viewer.rs b/src/ui/part_viewer.rs index 49660c2..3ad2a86 100644 --- a/src/ui/part_viewer.rs +++ b/src/ui/part_viewer.rs @@ -1,5 +1,5 @@ -use iced::{Length, alignment::Horizontal, widget::Column}; -use log::info; +use iced::{Length, Task, alignment::Horizontal, widget::Column}; +use tracing::info; use crate::ui::main_window::{MainWindowMsg, TabContent}; @@ -14,12 +14,13 @@ pub enum PartViewerMsg { impl TabContent for PartViewer { type TabMessage = PartViewerMsg; - fn update(&mut self, msg: Self::TabMessage) { + fn update(&mut self, msg: Self::TabMessage) -> Task { match msg { PartViewerMsg::Nothing => { info!("This function not allowed."); } } + Task::none() } fn content(&self) -> iced::Element<'_, MainWindowMsg> {