-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
book: Add example that reqwest fails without tokio
- Loading branch information
1 parent
1c454f3
commit 4dc533d
Showing
4 changed files
with
88 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}} | ||
``` | ||
|
||
|
||
|