Closed hkBst closed 8 months ago
Actually this problem seems to be more fundamental and is possibly a compiler bug. I've now included below a minimal test case, which still works (for me), but which stops working when I remove comments or add blank lines or change button strings, or remove the game: Game,
member from App
.
I am using trunk serve
, and ~/.cargo/bin/trunk --version
is trunk 0.17.5
, with rust 1.74.1.
Cargo.toml:
[package]
name = "tic-tac-toe-eframe"
version = "0.1.0"
edition = "2021"
[dependencies]
tic-tac-toe = { git = "https://github.com/hkBst/tic-tac-toe" }
egui = "0.25"
eframe = "0.25"
log = "0.4"
# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger = "0.10"
# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
src/main.rs:
use tic_tac_toe_eframe::App;
// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result<()> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([400.0, 300.0])
.with_min_inner_size([300.0, 220.0]),
..Default::default()
};
eframe::run_native(
"eframe template",
native_options,
Box::new(|cc| Box::new(App::new(cc))),
)
}
// When compiling to web using trunk:
#[cfg(target_arch = "wasm32")]
fn main() {
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
let web_options = eframe::WebOptions::default();
wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new()
.start(
"the_canvas_id", // hardcode it
web_options,
Box::new(|cc| Box::new(App::new(cc))),
)
.await
.expect("failed to start eframe");
});
}
src/lib.rs:
use tic_tac_toe::*;
#[derive(Default)]
pub struct App {
game: Game,
}
impl App {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
Default::default()
}
}
impl eframe::App for App {
// /// Called by the frame work to save state before shutdown.
// fn save(&mut self, storage: &mut dyn eframe::Storage) {
// eframe::set_value(storage, eframe::APP_KEY, self);
// }
/// Called each time the UI needs repainting, which may be many times per second.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Put your widgets into a `SidePanel`, `TopBottomPanel`, `CentralPanel`, `Window` or `Area`.
// For inspiration and more examples, go to https://emilk.github.io/egui
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
// NOTE: no File->Quit on web pages!
let is_web = cfg!(target_arch = "wasm32");
if !is_web {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}
});
ui.add_space(16.0);
}
egui::widgets::global_dark_light_mode_buttons(ui);
});
});
egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.heading("tic tac toe");
[Vert::Top, Vert::Mid, Vert::Bottom]
.into_iter()
.for_each(|v| {
ui.horizontal(|ui| {
[Hor::Left, Hor::Mid, Hor::Right].into_iter().for_each(|h| {
ui.button("{h:?},{v:?}");
})
});
});
});
}
}
Cargo.lock:
While interesting, this isn't really the right place to report bugs in rustc
, if that is indeed what this is. I suggest you dig a bit deeper, and then maybe open an issue on the rustc
repository.
Note that ui.button("{h:?},{v:?}")
is very different from ui.button(format!("{h:?},{v:?}"))
- the former treats the string verbatim, and the latter uses string interpolation/substitution.
Btw, you can use <details>
to avoid huge lists to scroll.
Thanks for the tip about <details>
!
I know this is not the right place to report compiler bugs, but since I first thought this an egui bug (and reported it as such), I thought I'd clarify here first. Besides, I think there are bound to be duplicates, like maybe #2504.
On WASM,
button
compiles fine, but causes the app to not display when using a String instead of a&'static str
.To Reproduce This code works as expected:
ui.button("{h:?},{v:?}")
But this code compiles fine, but produces no output:ui.button(format!("{h:?},{v:?}"))
And this fails similarly:ui.button("{h:?},{v:?}".to_string())