You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #123203 - jkarneges:context-ext, r=Amanieu
Add `Context::ext`
This change enables `Context` to carry arbitrary extension data via a single `&mut dyn Any` field.
```rust
#![feature(context_ext)]
impl Context {
fn ext(&mut self) -> &mut dyn Any;
}
impl ContextBuilder {
fn ext(self, data: &'a mut dyn Any) -> Self;
fn from(cx: &'a mut Context<'_>) -> Self;
fn waker(self, waker: &'a Waker) -> Self;
}
```
Basic usage:
```rust
struct MyExtensionData {
executor_name: String,
}
let mut ext = MyExtensionData {
executor_name: "foo".to_string(),
};
let mut cx = ContextBuilder::from_waker(&waker).ext(&mut ext).build();
if let Some(ext) = cx.ext().downcast_mut::<MyExtensionData>() {
println!("{}", ext.executor_name);
}
```
Currently, `Context` only carries a `Waker`, but there is interest in having it carry other kinds of data. Examples include [LocalWaker](#118959), [a reactor interface](rust-lang/libs-team#347), and [multiple arbitrary values by type](https://docs.rs/context-rs/latest/context_rs/). There is also a general practice in the ecosystem of sharing data between executors and futures via thread-locals or globals that would arguably be better shared via `Context`, if it were possible.
The `ext` field would provide a low friction (to stabilization) solution to enable experimentation. It would enable experimenting with what kinds of data we want to carry as well as with what data structures we may want to use to carry such data.
Dedicated fields for specific kinds of data could still be added directly on `Context` when we have sufficient experience or understanding about the problem they are solving, such as with `LocalWaker`. The `ext` field would be for data for which we don't have such experience or understanding, and that could be graduated to dedicated fields once proven.
Both the provider and consumer of the extension data must be aware of the concrete type behind the `Any`. This means it is not possible for the field to carry an abstract interface. However, the field can carry a concrete type which in turn carries an interface. There are different ways one can imagine an interface-carrying concrete type to work, hence the benefit of being able to experiment with such data structures.
## Passing interfaces
Interfaces can be placed in a concrete type, such as a struct, and then that type can be casted to `Any`. However, one gotcha is `Any` cannot contain non-static references. This means one cannot simply do:
```rust
struct Extensions<'a> {
interface1: &'a mut dyn Trait1,
interface2: &'a mut dyn Trait2,
}
let mut ext = Extensions {
interface1: &mut impl1,
interface2: &mut impl2,
};
let ext: &mut dyn Any = &mut ext;
```
To work around this without boxing, unsafe code can be used to create a safe projection using accessors. For example:
```rust
pub struct Extensions {
interface1: *mut dyn Trait1,
interface2: *mut dyn Trait2,
}
impl Extensions {
pub fn new<'a>(
interface1: &'a mut (dyn Trait1 + 'static),
interface2: &'a mut (dyn Trait2 + 'static),
scratch: &'a mut MaybeUninit<Self>,
) -> &'a mut Self {
scratch.write(Self {
interface1,
interface2,
})
}
pub fn interface1(&mut self) -> &mut dyn Trait1 {
unsafe { self.interface1.as_mut().unwrap() }
}
pub fn interface2(&mut self) -> &mut dyn Trait2 {
unsafe { self.interface2.as_mut().unwrap() }
}
}
let mut scratch = MaybeUninit::uninit();
let ext: &mut Extensions = Extensions::new(&mut impl1, &mut impl2, &mut scratch);
// ext can now be casted to `&mut dyn Any` and back, and used safely
let ext: &mut dyn Any = ext;
```
## Context inheritance
Sometimes when futures poll other futures they want to provide their own `Waker` which requires creating their own `Context`. Unfortunately, polling sub-futures with a fresh `Context` means any properties on the original `Context` won't get propagated along to the sub-futures. To help with this, some additional methods are added to `ContextBuilder`.
Here's how to derive a new `Context` from another, overriding only the `Waker`:
```rust
let mut cx = ContextBuilder::from(parent_cx).waker(&new_waker).build();
```
Copy file name to clipboardExpand all lines: tests/ui/async-await/async-is-unwindsafe.stderr
+37-6
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,18 @@ LL | is_unwindsafe(async {
6
6
| |_____|
7
7
| ||
8
8
LL | ||
9
+
LL | ||
9
10
LL | || use std::ptr::null;
10
-
LL | || use std::task::{Context, RawWaker, RawWakerVTable, Waker};
11
11
... ||
12
12
LL | || drop(cx_ref);
13
13
LL | || });
14
14
| ||_____-^ `&mut Context<'_>` may not be safely transferred across an unwind boundary
15
15
| |_____|
16
-
| within this `{async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6}`
16
+
| within this `{async block@$DIR/async-is-unwindsafe.rs:12:19: 30:6}`
17
17
|
18
-
= help: within `{async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6}`, the trait `UnwindSafe` is not implemented for `&mut Context<'_>`, which is required by `{async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6}: UnwindSafe`
19
-
= note: `UnwindSafe` is implemented for `&Context<'_>`, but not for `&mut Context<'_>`
18
+
= help: within `{async block@$DIR/async-is-unwindsafe.rs:12:19: 30:6}`, the trait `UnwindSafe` is not implemented for `&mut Context<'_>`, which is required by `{async block@$DIR/async-is-unwindsafe.rs:12:19: 30:6}: UnwindSafe`
20
19
note: future does not implement `UnwindSafe` as this value is used across an await
21
-
--> $DIR/async-is-unwindsafe.rs:25:18
20
+
--> $DIR/async-is-unwindsafe.rs:26:18
22
21
|
23
22
LL | let cx_ref = &mut cx;
24
23
| ------ has type `&mut Context<'_>` which does not implement `UnwindSafe`
@@ -31,6 +30,38 @@ note: required by a bound in `is_unwindsafe`
| ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_unwindsafe`
33
32
34
-
error: aborting due to 1 previous error
33
+
error[E0277]: the type `&mut (dyn Any + 'static)` may not be safely transferred across an unwind boundary
34
+
--> $DIR/async-is-unwindsafe.rs:12:5
35
+
|
36
+
LL | is_unwindsafe(async {
37
+
| _____^_____________-
38
+
| |_____|
39
+
| ||
40
+
LL | ||
41
+
LL | ||
42
+
LL | || use std::ptr::null;
43
+
... ||
44
+
LL | || drop(cx_ref);
45
+
LL | || });
46
+
| ||_____-^ `&mut (dyn Any + 'static)` may not be safely transferred across an unwind boundary
47
+
| |_____|
48
+
| within this `{async block@$DIR/async-is-unwindsafe.rs:12:19: 30:6}`
49
+
|
50
+
= help: within `{async block@$DIR/async-is-unwindsafe.rs:12:19: 30:6}`, the trait `UnwindSafe` is not implemented for `&mut (dyn Any + 'static)`, which is required by `{async block@$DIR/async-is-unwindsafe.rs:12:19: 30:6}: UnwindSafe`
51
+
note: future does not implement `UnwindSafe` as this value is used across an await
52
+
--> $DIR/async-is-unwindsafe.rs:26:18
53
+
|
54
+
LL | let mut cx = Context::from_waker(&waker);
55
+
| ------ has type `Context<'_>` which does not implement `UnwindSafe`
56
+
...
57
+
LL | async {}.await; // this needs an inner await point
58
+
| ^^^^^ await occurs here, with `mut cx` maybe used later
0 commit comments