Skip to content

Commit 11ad6e0

Browse files
fix doc: disappear # from hidden line (#2059)
1 parent 80c1a99 commit 11ad6e0

File tree

2 files changed

+51
-56
lines changed

2 files changed

+51
-56
lines changed

book/src/g_object_memory_management.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ With our first example, we have window with a single button.
77
Every button click should increment an integer `number` by one.
88

99
```rust ,no_run,compile_fail
10-
#use gtk::prelude::*;
11-
#use gtk::{self, glib, Application, ApplicationWindow, Button};
10+
# use gtk::prelude::*;
11+
# use gtk::{self, glib, Application, ApplicationWindow, Button};
1212
#
13-
#const APP_ID: &str = "org.gtk_rs.GObjectMemoryManagement0";
13+
# const APP_ID: &str = "org.gtk_rs.GObjectMemoryManagement0";
1414
#
1515
// DOES NOT COMPILE!
1616
fn main() -> glib::ExitCode {
@@ -72,7 +72,7 @@ note: function requires argument type to outlive `'static`
7272
help: to force the closure to take ownership of `number` (and any other referenced variables), use the `move` keyword
7373
|
7474
32 | button_increase.connect_clicked(move |_| number += 1);
75-
|
75+
|
7676
```
7777

7878
Our closure only borrows `number`.
@@ -81,31 +81,31 @@ The compiler also suggests how to fix this.
8181
By adding the `move` keyword in front of the closure, `number` will be moved into the closure.
8282

8383
```rust ,no_run,compile_fail
84-
#use gtk::prelude::*;
85-
#use gtk::{self, glib, Application, ApplicationWindow, Button};
84+
# use gtk::prelude::*;
85+
# use gtk::{self, glib, Application, ApplicationWindow, Button};
8686
#
87-
#const APP_ID: &str = "org.gtk_rs.GObjectMemoryManagement0";
87+
# const APP_ID: &str = "org.gtk_rs.GObjectMemoryManagement0";
8888
#
89-
#fn main() -> glib::ExitCode {
90-
# // Create a new application
91-
# let app = Application::builder().application_id(APP_ID).build();
89+
# fn main() -> glib::ExitCode {
90+
# // Create a new application
91+
# let app = Application::builder().application_id(APP_ID).build();
9292
#
93-
# // Connect to "activate" signal of `app`
94-
# app.connect_activate(build_ui);
93+
# // Connect to "activate" signal of `app`
94+
# app.connect_activate(build_ui);
9595
#
96-
# // Run the application
97-
# app.run()
98-
#}
96+
# // Run the application
97+
# app.run()
98+
# }
9999
#
100-
#fn build_ui(application: &Application) {
101-
# // Create two buttons
102-
# let button_increase = Button::builder()
103-
# .label("Increase")
104-
# .margin_top(12)
105-
# .margin_bottom(12)
106-
# .margin_start(12)
107-
# .margin_end(12)
108-
# .build();
100+
# fn build_ui(application: &Application) {
101+
# // Create two buttons
102+
# let button_increase = Button::builder()
103+
# .label("Increase")
104+
# .margin_top(12)
105+
# .margin_bottom(12)
106+
# .margin_start(12)
107+
# .margin_end(12)
108+
# .build();
109109
#
110110
// DOES NOT COMPILE!
111111
// A mutable integer
@@ -124,7 +124,7 @@ By adding the `move` keyword in front of the closure, `number` will be moved int
124124
#
125125
# // Present the window
126126
# window.present();
127-
#}
127+
# }
128128
```
129129

130130
This still leaves the following error message:
@@ -169,7 +169,6 @@ That is exactly what the [`std::rc::Rc`](https://doc.rust-lang.org/std/rc/struct
169169
If we want to modify the content of our [`Rc`](https://doc.rust-lang.org/std/rc/struct.Rc.html),
170170
we can again use the [`Cell`](https://doc.rust-lang.org/std/cell/struct.Cell.html) type.
171171

172-
173172
Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/main/book/listings/g_object_memory_management/2/main.rs">listings/g_object_memory_management/2/main.rs</a>
174173

175174
```rust

book/src/main_event_loop.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ We can't even move the window.
2626
The `sleep` call is an artificial example,
2727
but frequently, we want to run a slightly longer operation in one go.
2828

29-
3029
<div style="text-align:center">
3130
<video autoplay muted loop>
3231
<source src="vid/main_event_loop_1.webm" type="video/webm">
3332
<p>A video which shows that after pressing the button, the window can still be moved</p>
3433
</video>
3534
</div>
3635

37-
3836
## How to Avoid Blocking the Main Loop
3937

4038
In order to avoid blocking the main loop, we can spawn a new task with [`gio::spawn_blocking`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/fn.spawn_blocking.html) and let the operation run on the thread pool.
@@ -56,7 +54,6 @@ This is not necessarily what we want.
5654
</video>
5755
</div>
5856

59-
6057
> If you come from another language than Rust, you might be uncomfortable with the thought of running tasks in separate threads before even looking at other options.
6158
> Luckily, Rust's safety guarantees allow you to stop worrying about the nasty bugs that concurrency tends to bring.
6259
@@ -123,12 +120,12 @@ But why did we not do the same thing with our multithreaded example?
123120

124121
```rust ,no_run,compile_fail
125122
# use std::{thread, time::Duration};
126-
#
123+
#
127124
# use glib::{clone, MainContext, PRIORITY_DEFAULT};
128125
# use gtk::{glib, gio};
129126
# use gtk::prelude::*;
130127
# use gtk::{Application, ApplicationWindow, Button};
131-
#
128+
#
132129
# fn main() {
133130
# // Create a new application
134131
# let app = Application::builder()
@@ -137,21 +134,21 @@ But why did we not do the same thing with our multithreaded example?
137134
#
138135
# // Connect to "activate" signal
139136
# app.connect_activate(build_ui);
140-
#
137+
#
141138
# // Get command-line arguments
142139
# let args: Vec<String> = args().collect();
143140
# // Run the application
144141
# app.run(&args);
145142
# }
146-
#
143+
#
147144
# // When the application is launched…
148145
# fn build_ui(application: &Application) {
149146
# // Create a window
150147
# let window = ApplicationWindow::builder()
151148
# .application(application)
152149
# .title("My GTK App")
153150
# .build();
154-
#
151+
#
155152
# // Create a button
156153
# let button = Button::builder()
157154
# .label("Press me!")
@@ -160,7 +157,7 @@ But why did we not do the same thing with our multithreaded example?
160157
# .margin_start(12)
161158
# .margin_end(12)
162159
# .build();
163-
#
160+
#
164161
// DOES NOT COMPILE!
165162
// Connect to "clicked" signal of `button`
166163
button.connect_clicked(move |button| {
@@ -175,7 +172,7 @@ But why did we not do the same thing with our multithreaded example?
175172
button.set_sensitive(true);
176173
});
177174
});
178-
#
175+
#
179176
# // Add button
180177
# window.set_child(Some(&button));
181178
# window.present();
@@ -281,32 +278,32 @@ Doing this will block one of the runtime's threads with the GLib main loop, whic
281278
Instead, we bind [`tokio::runtime::Runtime`](https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html) to a static variable.
282279

283280
```rust
284-
#use std::sync::OnceLock;
281+
# use std::sync::OnceLock;
285282
#
286-
#use glib::clone;
287-
#use gtk::glib;
288-
#use gtk::prelude::*;
289-
#use gtk::{Application, ApplicationWindow, Button};
290-
#use tokio::runtime::Runtime;
283+
# use glib::clone;
284+
# use gtk::glib;
285+
# use gtk::prelude::*;
286+
# use gtk::{Application, ApplicationWindow, Button};
287+
# use tokio::runtime::Runtime;
291288
#
292-
#const APP_ID: &str = "org.gtk_rs.MainEventLoop0";
289+
# const APP_ID: &str = "org.gtk_rs.MainEventLoop0";
293290
#
294291
// DOES NOT COMPILE!
295292
static RUNTIME: Runtime =
296293
Runtime::new().expect("Setting up tokio runtime needs to succeed.");
297294
#
298-
#fn main() -> glib::ExitCode {
299-
# // Create a new application
300-
# let app = Application::builder().application_id(APP_ID).build();
295+
# fn main() -> glib::ExitCode {
296+
# // Create a new application
297+
# let app = Application::builder().application_id(APP_ID).build();
301298
#
302-
# // Connect to "activate" signal of `app`
303-
# app.connect_activate(build_ui);
299+
# // Connect to "activate" signal of `app`
300+
# app.connect_activate(build_ui);
304301
#
305-
# // Run the application
306-
# app.run()
307-
#}
302+
# // Run the application
303+
# app.run()
304+
# }
308305
#
309-
#fn build_ui(app: &Application) {
306+
# fn build_ui(app: &Application) {
310307
# // Create a button
311308
# let button = Button::builder()
312309
# .label("Press me!")
@@ -347,7 +344,7 @@ static RUNTIME: Runtime =
347344
#
348345
# // Present window
349346
# window.present();
350-
#}
347+
# }
351348
```
352349

353350
Unfortunately, this doesn't compile.
@@ -362,7 +359,6 @@ consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` cr
362359
We could follow the advice directly, but the standard library also provides solutions for that.
363360
With [`std::sync::OnceLock`](https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html) we can initialize the static with the const function `OnceLock::new()` and initialize it the first time our function `runtime` is called.
364361

365-
366362
Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/main/book/listings/main_event_loop/9/main.rs">listings/main_event_loop/9/main.rs</a>
367363

368364
```rust
@@ -391,12 +387,12 @@ cargo remove tokio reqwest ashpd
391387

392388
How to find out whether you can spawn an `async` task on the `glib` main loop?
393389
`glib` should be able to spawn the task when the called functions come from libraries that either:
390+
394391
- come from the `glib` ecosystem,
395392
- don't depend on a runtime but only on the `futures` family of crates (`futures-io`, `futures-core` etc),
396393
- depend on the `async-std` or `smol` runtimes, or
397394
- have cargo features that let them depend on `async-std`/`smol` instead of `tokio`.
398395

399-
400396
## Conclusion
401397

402398
You don't want to block the main thread long enough that it is noticeable by the user.
@@ -408,7 +404,7 @@ That means you have to run the task in a separate thread and let it send results
408404

409405
If your task is [IO bound](https://en.wikipedia.org/wiki/I/O_bound), the answer depends on the crates at your disposal and the type of work to be done.
410406

411-
- Light I/O work with functions from crates using `glib`, `smol`, `async-std` or the `futures` trait family can be spawned on the main loop. This way, you can often avoid synchronization via channels.
407+
- Light I/O work with functions from crates using `glib`, `smol`, `async-std` or the `futures` trait family can be spawned on the main loop. This way, you can often avoid synchronization via channels.
412408
- Heavy I/O work might still benefit from running in a separate thread / an async executor to avoid saturating the main loop. If you are unsure, benchmarking is advised.
413409

414410
If the best crate for the job relies on `tokio`, you will have to spawn it with the tokio runtime and communicate via channels.

0 commit comments

Comments
 (0)