Skip to content

Commit fa90cc5

Browse files
Support smol as async runtime backend
1 parent 78d4f4b commit fa90cc5

13 files changed

Lines changed: 251 additions & 75 deletions

File tree

Cargo.lock

Lines changed: 51 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ masonry_imaging = { version = "0.4.0", path = "masonry_imaging", default-feature
4949
masonry_testing = { version = "0.4.0", path = "masonry_testing", default-features = false }
5050
masonry_winit = { version = "0.4.0", path = "masonry_winit", default-features = false }
5151
xilem_core = { version = "0.4.0", path = "xilem_core" }
52-
xilem_masonry = { version = "0.4.0", path = "xilem_masonry" }
52+
xilem_masonry = { version = "0.4.0", path = "xilem_masonry", default-features = false }
5353
xilem = { version = "0.4.0", path = "xilem", default-features = false }
5454
tree_arena = { version = "0.2.0", path = "tree_arena" }
5555
include_doc_path = { version = "0.1.0", path = "include_doc_path", package = "linebender_include_doc_path" }
@@ -85,7 +85,6 @@ bitflags = "2.11.0"
8585
accesskit = "0.24.0"
8686
accesskit_winit = { version = "0.32.2", default-features = false, features = [
8787
"accesskit_unix",
88-
"tokio",
8988
"rwh_06",
9089
] }
9190
accesskit_consumer = "0.35.0"
@@ -96,6 +95,8 @@ reqwest = { version = "0.12.28", default-features = false, features = [
9695
"rustls-tls",
9796
] }
9897
divan = "0.1.21"
98+
tokio = { version = "1.53.1", features = ["rt", "rt-multi-thread", "time", "sync"] }
99+
smol = "2.0.2"
99100

100101
[workspace.lints]
101102
# unsafe code is not allowed in Xilem or Masonry

masonry_winit/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ license.workspace = true
1010
repository.workspace = true
1111

1212
[package.metadata.docs.rs]
13-
all-features = true
1413
# There are no platform specific docs.
1514
default-target = "x86_64-unknown-linux-gnu"
1615
targets = []
1716
# rustdoc-scrape-examples tracking issue https://github.com/rust-lang/rust/issues/88791
1817
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]
1918

2019
[features]
21-
default = ["imaging_vello"]
20+
default = ["imaging_vello", "accesskit-tokio"]
2221
imaging_vello = ["masonry_imaging/imaging_vello"]
2322
imaging_vello_hybrid = ["masonry_imaging/imaging_vello_hybrid"]
2423
imaging_skia = ["masonry_imaging/imaging_skia"]
24+
accesskit-tokio = ["accesskit_winit/tokio"]
25+
accesskit-async-io = ["accesskit_winit/async-io"]
2526
# Enables tracing using tracy if the default Masonry tracing is used.
2627
# https://github.com/wolfpld/tracy can be connected to when this feature is enabled.
2728
tracy = [

xilem/Cargo.toml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ exclude = [
1515
]
1616

1717
[package.metadata.docs.rs]
18-
all-features = true
1918
# There are no platform specific docs.
2019
default-target = "x86_64-unknown-linux-gnu"
2120
targets = []
@@ -32,7 +31,9 @@ debuggable = true
3231
name = "android.permission.INTERNET"
3332

3433
[features]
35-
default = ["masonry/default", "masonry_winit/default"]
34+
default = ["masonry/default", "masonry_winit/default", "tokio-rt"]
35+
tokio-rt = ["dep:tokio", "xilem_masonry/tokio-rt", "masonry_winit/accesskit-tokio"]
36+
smol-rt = ["dep:smol", "xilem_masonry/smol-rt", "masonry_winit/accesskit-async-io"]
3637

3738
[dependencies]
3839
xilem_core.workspace = true
@@ -41,7 +42,8 @@ masonry.workspace = true
4142
masonry_winit.workspace = true
4243
winit.workspace = true
4344
tracing.workspace = true
44-
tokio = { version = "1.50.0", features = ["rt", "rt-multi-thread", "time", "sync"] }
45+
tokio = { workspace = true, optional = true }
46+
smol = { workspace = true, optional = true }
4547

4648
[dev-dependencies]
4749
# Used for `variable_clock`
@@ -69,18 +71,21 @@ name = "mason"
6971
# This actually enables scraping for all examples, not just this one.
7072
# However it is possible to set doc-scrape-examples to false for other specific examples.
7173
doc-scrape-examples = true
74+
required-features = ["tokio-rt"]
7275

7376
[[example]]
7477
name = "calc"
7578

7679
[[example]]
7780
name = "stopwatch"
81+
required-features = ["tokio-rt"]
7882

7983
[[example]]
8084
name = "to_do_mvc"
8185

8286
[[example]]
8387
name = "variable_clock"
88+
required-features = ["tokio-rt"]
8489

8590
[[example]]
8691
name = "emoji_picker"
@@ -93,6 +98,11 @@ doc-scrape-examples = false
9398

9499
[[example]]
95100
name = "http_cats"
101+
required-features = ["tokio-rt"]
102+
103+
[[example]]
104+
name = "virtual_cats"
105+
required-features = ["tokio-rt"]
96106

97107
# Also add to ANDROID_TARGETS in .github/ci.yml if adding a new Android example
98108
# and update the list in xilem/examples/android/README.md.
@@ -102,6 +112,7 @@ name = "mason_android"
102112
path = "examples/android/mason_android.rs"
103113
# cdylib is required for cargo-apk
104114
crate-type = ["cdylib"]
115+
required-features = ["tokio-rt"]
105116

106117
[[example]]
107118
name = "calc_android"
@@ -114,6 +125,7 @@ name = "stopwatch_android"
114125
path = "examples/android/stopwatch_android.rs"
115126
# cdylib is required for cargo-apk
116127
crate-type = ["cdylib"]
128+
required-features = ["tokio-rt"]
117129

118130
[[example]]
119131
name = "to_do_mvc_android"
@@ -126,6 +138,7 @@ name = "variable_clock_android"
126138
path = "examples/android/variable_clock_android.rs"
127139
# cdylib is required for cargo-apk
128140
crate-type = ["cdylib"]
141+
required-features = ["tokio-rt"]
129142

130143
[[example]]
131144
name = "emoji_picker_android"
@@ -139,6 +152,7 @@ name = "http_cats_android"
139152
path = "examples/android/http_cats_android.rs"
140153
# cdylib is required for cargo-apk
141154
crate-type = ["cdylib"]
155+
required-features = ["tokio-rt"]
142156

143157
[[example]]
144158
name = "canvas"

xilem/src/app.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ use masonry::core::DefaultProperties;
88
use masonry::peniko::{Blob, Color};
99
use masonry::theme::{BACKGROUND_COLOR, default_property_set};
1010
use masonry_winit::app::{EventLoopBuilder, MasonryState, MasonryUserEvent, NewWindow, WindowId};
11-
use tokio::runtime::Runtime as TokioRuntime;
1211
use winit::error::EventLoopError;
12+
use xilem_masonry::runtime::Executor;
13+
14+
#[cfg(feature = "tokio-rt")]
15+
use tokio::runtime::Runtime as TokioRuntime;
1316

1417
use crate::core::map_state;
1518
use crate::window_options::WindowCallbacks;
@@ -22,7 +25,7 @@ use crate::{MasonryDriver, WidgetView, WindowOptions, WindowView};
2225
pub struct Xilem<State, Logic> {
2326
state: State,
2427
logic: Logic,
25-
runtime: Arc<TokioRuntime>,
28+
runtime: Arc<Executor>,
2629
default_properties: Option<DefaultProperties>,
2730
default_base_color: Color,
2831
// Font data to include in loading.
@@ -62,21 +65,34 @@ impl<State>
6265
where
6366
View: WidgetView<State>,
6467
{
65-
Self::new_simple_with_tokio(
66-
state,
67-
logic,
68-
window_options,
69-
Arc::new(TokioRuntime::new().unwrap()),
70-
)
68+
Self::new_simple_with_executor(state, logic, window_options, Executor::new().into())
7169
}
7270

7371
/// Same as [`Self::new_simple`] but allows passing an existing tokio runtime.
72+
#[deprecated(
73+
since = "0.4.0",
74+
note = "use `Xilem::new_simple_with_executor` with `xilem_masonry::runtime::Executor` instead"
75+
)]
76+
#[cfg(feature = "tokio-rt")]
7477
pub fn new_simple_with_tokio<View>(
7578
state: State,
76-
mut logic: impl FnMut(&mut State) -> View + 'static,
79+
logic: impl FnMut(&mut State) -> View + 'static,
7780
window_options: WindowOptions<State>,
7881
tokio_rt: Arc<TokioRuntime>,
7982
) -> Self
83+
where
84+
View: WidgetView<State>,
85+
{
86+
Self::new_simple_with_executor(state, logic, window_options, Arc::new(tokio_rt.into()))
87+
}
88+
89+
/// Same as [`Self::new_simple`] but allows passing an existing runtime executor.
90+
pub fn new_simple_with_executor<View>(
91+
state: State,
92+
mut logic: impl FnMut(&mut State) -> View + 'static,
93+
window_options: WindowOptions<State>,
94+
runtime: Arc<Executor>,
95+
) -> Self
8096
where
8197
View: WidgetView<State>,
8298
{
@@ -112,7 +128,7 @@ impl<State>
112128
}),
113129
)
114130
}),
115-
tokio_rt,
131+
runtime,
116132
)
117133
}
118134
}
@@ -138,10 +154,10 @@ where
138154
where
139155
State: AppState,
140156
{
141-
Self::new_inner(state, logic, Arc::new(TokioRuntime::new().unwrap()))
157+
Self::new_inner(state, logic, Arc::new(Executor::new()))
142158
}
143159

144-
fn new_inner(state: State, logic: Logic, runtime: Arc<TokioRuntime>) -> Self {
160+
fn new_inner(state: State, logic: Logic, runtime: Arc<Executor>) -> Self {
145161
Self {
146162
state,
147163
logic,

xilem/src/driver.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ use crate::core::{
1717
};
1818
use crate::window_view::{WindowView, WindowViewState};
1919
use crate::{AppState, Color, ViewCtx};
20+
use xilem_masonry::runtime::Executor;
2021

2122
/// The composition root of Xilem's Masonry backend.
2223
///
2324
/// Owns the root state, the root view (kind of), the window state, the event loop proxy,
24-
/// the tokio runtime, etc.
25+
/// the async runtime executor, etc.
2526
pub struct MasonryDriver<State: 'static, Logic> {
2627
state: State,
2728
logic: Logic,
2829
windows: HashMap<WindowId, Window<State>>,
2930
proxy: Arc<MasonryProxy>,
30-
runtime: Arc<tokio::runtime::Runtime>,
31+
runtime: Arc<Executor>,
3132
default_base_color: Color,
3233
// Fonts which will be registered on startup.
3334
fonts: Vec<Blob<u8>>,
@@ -53,7 +54,7 @@ where
5354
// TODO: narrow down MasonryUserEvent in event_sink once masonry_winit supports custom event types
5455
// (we only ever use it to send MasonryUserEvent::Action with ASYNC_MARKER_WIDGET)
5556
event_sink: impl Fn(MasonryUserEvent) -> Result<(), MasonryUserEvent> + Send + Sync + 'static,
56-
runtime: Arc<tokio::runtime::Runtime>,
57+
runtime: Arc<Executor>,
5758
default_base_color: Color,
5859
fonts: Vec<Blob<u8>>,
5960
start_callback: Option<Box<dyn FnOnce(&mut MasonryState<'_>)>>,

xilem/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ pub use masonry;
153153
pub use masonry::dpi;
154154
pub use masonry::palette;
155155
pub use masonry::{kurbo, peniko};
156+
#[cfg(feature = "smol-rt")]
157+
pub use smol;
158+
#[cfg(feature = "tokio-rt")]
156159
pub use tokio;
157160
pub use winit;
158161
pub use xilem_core as core;

0 commit comments

Comments
 (0)