IssueRaiser

Tracking Issue for try_as_dyn

<!-- homu-ignore:start --> *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/issues/144361)* <!-- homu-ignore:end --> Feature gate: `#![feature(try_as_dyn)]` This is a tracking issue for the `try_as_dyn` and `try_as_dyn_mut` functions. This feature will let us try to turn any value into a `dyn MyTrait`. If the value does not implement `MyTrait` then we simply return `None` so that the user can have some default functionality happen instead. We expect to be able to remove many cases of specialization from the compiler and instead perform this kind of downcasting in the bodies of functions that were relying on specialization before. A simple example could be ```rust // Look ma, no `T: Debug` fn downcast_debug_format<T: 'static>(t: &T) -> String { match std::any::try_as_dyn::<_, dyn Debug>(t) { Some(d) => format!("{d:?}"), None => "default".to_string() } } ``` ### Public API <!-- For most library features, it'd be useful to include a summarized version of the public API. (E.g. just the public function signatures without their doc comments or implementation.) --> ```rust pub const fn try_as_dyn< T: Any + 'static, U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static, >( t: &T, ) -> Option<&U> pub const fn try_as_dyn_mut< T: Any + 'static, U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static, >( t: &mut T, ) -> Option<&mut U> ``` ### Steps / History <!-- For la

Posted by ivarflakstad on Jul 23, 2025, 2:08 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,000 issues0 comments

github (Sysadmin)

Tracking Issue for try_as_dyn

ivarflakstadJul 23, 2025, 2:08 PM

<!-- homu-ignore:start --> *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/issues/144361)* <!-- homu-ignore:end --> Feature gate: `#![feature(try_as_dyn)]` This is a tracking issue for the `try_as_dyn` and `try_as_dyn_mut` functions. This feature will let us try to turn any value into a `dyn MyTrait`. If the value does not implement `MyTrait` then we simply return `None` so that the user can have some default functionality happen instead. We expect to be able to remove many cases of specialization from the compiler and instead perform this kind of downcasting in the bodies of functions that were relying on specialization before. A simple example could be ```rust // Look ma, no `T: Debug` fn downcast_debug_format<T: 'static>(t: &T) -> String { match std::any::try_as_dyn::<_, dyn Debug>(t) { Some(d) => format!("{d:?}"), None => "default".to_string() } } ``` ### Public API <!-- For most library features, it'd be useful to include a summarized version of the public API. (E.g. just the public function signatures without their doc comments or implementation.) --> ```rust pub const fn try_as_dyn< T: Any + 'static, U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static, >( t: &T, ) -> Option<&U> pub const fn try_as_dyn_mut< T: Any + 'static, U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static, >( t: &mut T, ) -> Option<&mut U> ``` ### Steps / History <!-- For la

Comments (0)

No comments yet. Start the discussion.

Comment on this issue