mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 18:58:36 +00:00
Improve documented workaround for Tokio integration (#5736)
Document the two constraints of using Tokio futures in Slint, and how to work around them. Fixes #5733 Co-authored-by: Olivier Goffart <olivier.goffart@slint.dev>
This commit is contained in:
parent
2050414cbd
commit
fbe0b23684
5 changed files with 126 additions and 21 deletions
|
@ -200,6 +200,8 @@ slint-build = { path = "../build" }
|
|||
i-slint-backend-testing = { path = "../../../internal/backends/testing", features = ["internal"] }
|
||||
serde_json = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "net", "io-util"]}
|
||||
async-compat = { version = "0.2.4" }
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
# this line is there to add the "enable" feature by default, but only on linux
|
||||
|
|
34
api/rs/slint/tests/tokio.rs
Normal file
34
api/rs/slint/tests/tokio.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
|
||||
|
||||
#[test]
|
||||
fn tokio_poll_with_compat() {
|
||||
i_slint_backend_testing::init_integration_test_with_mock_time();
|
||||
use std::io::Write;
|
||||
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let local_addr = listener.local_addr().unwrap();
|
||||
let server = std::thread::spawn(move || {
|
||||
let mut stream = listener.incoming().next().unwrap().unwrap();
|
||||
stream.write("Hello World".as_bytes()).unwrap();
|
||||
});
|
||||
|
||||
let slint_future = async move {
|
||||
for _ in 0..1000 {
|
||||
tokio::task::consume_budget().await;
|
||||
}
|
||||
|
||||
use tokio::io::AsyncReadExt;
|
||||
let mut stream = tokio::net::TcpStream::connect(local_addr).await.unwrap();
|
||||
let mut data = Vec::new();
|
||||
stream.read_to_end(&mut data).await.unwrap();
|
||||
assert_eq!(data, "Hello World".as_bytes());
|
||||
slint::quit_event_loop().unwrap();
|
||||
};
|
||||
|
||||
slint::spawn_local(async_compat::Compat::new(slint_future)).unwrap();
|
||||
|
||||
slint::run_event_loop_until_quit().unwrap();
|
||||
|
||||
server.join().unwrap();
|
||||
}
|
34
api/rs/slint/tests/tokio_block_in_place.rs
Normal file
34
api/rs/slint/tests/tokio_block_in_place.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
async fn tokio_poll_with_block_in_place() {
|
||||
i_slint_backend_testing::init_integration_test_with_mock_time();
|
||||
use std::io::Write;
|
||||
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let local_addr = listener.local_addr().unwrap();
|
||||
let server = std::thread::spawn(move || {
|
||||
let mut stream = listener.incoming().next().unwrap().unwrap();
|
||||
stream.write("Hello World".as_bytes()).unwrap();
|
||||
});
|
||||
|
||||
let slint_future = async move {
|
||||
for _ in 0..1000 {
|
||||
tokio::task::consume_budget().await;
|
||||
}
|
||||
|
||||
use tokio::io::AsyncReadExt;
|
||||
let mut stream = tokio::net::TcpStream::connect(local_addr).await.unwrap();
|
||||
let mut data = Vec::new();
|
||||
stream.read_to_end(&mut data).await.unwrap();
|
||||
assert_eq!(data, "Hello World".as_bytes());
|
||||
slint::quit_event_loop().unwrap();
|
||||
};
|
||||
|
||||
slint::spawn_local(slint_future).unwrap();
|
||||
|
||||
tokio::task::block_in_place(slint::run_event_loop_until_quit).unwrap();
|
||||
|
||||
server.join().unwrap();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue