mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 17:18:32 +03:00
ca222b85ff
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/rust#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 ... } } } ```