Commit 34552f7
authored
Rollup merge of rust-lang#156104 - aobatact:try-as-dyn-unsized, r=oli-obk
Relax `T: Sized` bound on `try_as_dyn` / `try_as_dyn_mut`
`trait_info_of` already returns `None` for unsized types, so allowing `T: ?Sized` is sound and lets callers in generic contexts use these functions without a separate `Sized` bound. For unsized `T`, the function always returns `None`.
Tracking issue: rust-lang#144361
## Motivation
Currently, it is not possible to use `try_as_dyn` as "specialization-like" dispatch in
blanket impls with `?Sized` type.
```rust
// Cannot add ?Sized now.
impl<T: 'static /* + ?Sized */, S: Serializer + 'static> Serialize<S> for T {
fn serialize(&self, serializer: &mut S) -> Result<S::Ok, S::Error> {
if let Some(spec) = try_as_dyn::<_, dyn SpecializedSer<S>>(self) {
spec.specialized_serialize(serializer)
} else {
// fall back to compile-time reflection via TypeId
...
}
}
}
```2 files changed
Lines changed: 48 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1007 | 1007 | | |
1008 | 1008 | | |
1009 | 1009 | | |
1010 | | - | |
| 1010 | + | |
1011 | 1011 | | |
1012 | 1012 | | |
1013 | 1013 | | |
1014 | 1014 | | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
1015 | 1018 | | |
1016 | 1019 | | |
1017 | 1020 | | |
1018 | 1021 | | |
1019 | | - | |
| 1022 | + | |
1020 | 1023 | | |
1021 | 1024 | | |
| 1025 | + | |
| 1026 | + | |
1022 | 1027 | | |
1023 | 1028 | | |
1024 | 1029 | | |
| |||
1061 | 1066 | | |
1062 | 1067 | | |
1063 | 1068 | | |
1064 | | - | |
| 1069 | + | |
1065 | 1070 | | |
1066 | 1071 | | |
1067 | 1072 | | |
1068 | 1073 | | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
1069 | 1077 | | |
1070 | 1078 | | |
1071 | 1079 | | |
1072 | 1080 | | |
1073 | | - | |
| 1081 | + | |
1074 | 1082 | | |
1075 | 1083 | | |
| 1084 | + | |
| 1085 | + | |
1076 | 1086 | | |
1077 | 1087 | | |
1078 | 1088 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
0 commit comments