Skip to content

Commit

Permalink
book: Add example that reqwest fails without tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Oct 22, 2023
1 parent 1c454f3 commit 4dc533d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
3 changes: 3 additions & 0 deletions book/listings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ path = "main_event_loop/7/main.rs"
name = "main_event_loop_8"
path = "main_event_loop/8/main.rs"

[[bin]]
name = "main_event_loop_9"
path = "main_event_loop/9/main.rs"

# saving_window_state
[[bin]]
Expand Down
12 changes: 4 additions & 8 deletions book/listings/main_event_loop/8/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ use glib::{clone, MainContext, Priority};
use gtk::glib;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;

// ANCHOR: tokio_runtime
const APP_ID: &str = "org.gtk_rs.MainEventLoop8";
static RUNTIME: Lazy<Runtime> =
Lazy::new(|| Runtime::new().expect("Setting up tokio runtime needs to succeed."));

fn main() -> glib::ExitCode {
// Create a new application
Expand All @@ -20,7 +15,6 @@ fn main() -> glib::ExitCode {
// Run the application
app.run()
}
// ANCHOR_END: tokio_runtime

fn build_ui(app: &Application) {
// Create a button
Expand All @@ -36,7 +30,9 @@ fn build_ui(app: &Application) {
let (sender, receiver) = MainContext::channel(Priority::default());
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
RUNTIME.spawn(clone!(@strong sender => async move {
let main_context = MainContext::default();
// The main loop executes the asynchronous block
main_context.spawn_local(clone!(@strong sender => async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender
.send(response)
Expand All @@ -49,7 +45,7 @@ fn build_ui(app: &Application) {
if let Ok(response) = response {
println!("Status {}", response.status());
} else {
println!("Couldn't make a `GET` request.");
println!("Could not make a `GET` request.");
}
glib::ControlFlow::Continue
});
Expand Down
67 changes: 67 additions & 0 deletions book/listings/main_event_loop/9/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use glib::{clone, MainContext, Priority};
use gtk::glib;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;

// ANCHOR: tokio_runtime
const APP_ID: &str = "org.gtk_rs.MainEventLoop8";
static RUNTIME: Lazy<Runtime> =
Lazy::new(|| Runtime::new().expect("Setting up tokio runtime needs to succeed."));

fn main() -> glib::ExitCode {
// Create a new application
let app = Application::builder().application_id(APP_ID).build();

// Connect to "activate" signal of `app`
app.connect_activate(build_ui);

// Run the application
app.run()
}
// ANCHOR_END: tokio_runtime

fn build_ui(app: &Application) {
// Create a button
let button = Button::builder()
.label("Press me!")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();

// ANCHOR: callback
let (sender, receiver) = MainContext::channel(Priority::default());
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
RUNTIME.spawn(clone!(@strong sender => async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender
.send(response)
.expect("Could not send through channel");
}));
});

// The main loop executes the closure as soon as it receives the message
receiver.attach(None, move |response| {
if let Ok(response) = response {
println!("Status {}", response.status());
} else {
println!("Could not make a `GET` request.");
}
glib::ControlFlow::Continue
});
// ANCHOR_END: callback

// Create a window
let window = ApplicationWindow::builder()
.application(app)
.title("My GTK App")
.child(&button)
.build();

// Present window
window.present();
}
18 changes: 14 additions & 4 deletions book/src/main_event_loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,30 @@ If you decide to share it, you user name will be printed on the console.


```
cargo add tokio@1 --features rt-multi-thread
cargo add [email protected] --features rustls-tls --no-default-features
```

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

```rust
{{#rustdoc_include ../listings/main_event_loop/8/main.rs:tokio_runtime}}
{{#rustdoc_include ../listings/main_event_loop/8/main.rs:callback}}
```

```
cargo add tokio@1 --features rt-multi-thread
```

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

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

```rust
{{#rustdoc_include ../listings/main_event_loop/8/main.rs:callback}}
{{#rustdoc_include ../listings/main_event_loop/9/main.rs:tokio_runtime}}
```

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

```rust
{{#rustdoc_include ../listings/main_event_loop/9/main.rs:callback}}
```


Expand Down

0 comments on commit 4dc533d

Please sign in to comment.