@@ -816,7 +816,7 @@ impl rwh_06::HasWindowHandle for dyn Surface + '_ {
816816///
817817/// **Web:** The [`Window`], which is represented by a `HTMLElementCanvas`, can
818818/// not be closed by dropping the [`Window`].
819- pub trait Window : Surface {
819+ pub trait Window : AsSurface {
820820 /// Reset the dead key state of the keyboard.
821821 ///
822822 /// This is useful when a dead key is bound to trigger an action. Then
@@ -1601,18 +1601,65 @@ impl ActivationToken {
16011601 }
16021602}
16031603
1604+ // Implementation trait to allow upcasting to [`Surface`].
1605+ // This trait can be safely removed once MSRV hits Rust 1.86, similar to `AsAny`.
1606+ #[ doc( hidden) ]
1607+ pub trait AsSurface : Surface {
1608+ #[ doc( hidden) ]
1609+ fn __as_surface ( & self ) -> & dyn Surface ;
1610+ #[ doc( hidden) ]
1611+ fn __as_surface_mut ( & mut self ) -> & mut dyn Surface ;
1612+ #[ doc( hidden) ]
1613+ fn __into_surface ( self : Box < Self > ) -> Box < dyn Surface > ;
1614+ }
1615+
1616+ impl < T : Surface > AsSurface for T {
1617+ fn __as_surface ( & self ) -> & dyn Surface {
1618+ self
1619+ }
1620+
1621+ fn __as_surface_mut ( & mut self ) -> & mut dyn Surface {
1622+ self
1623+ }
1624+
1625+ fn __into_surface ( self : Box < Self > ) -> Box < dyn Surface > {
1626+ self
1627+ }
1628+ }
1629+
16041630/// Dynamic reference to one of the common surface traits.
1631+ #[ non_exhaustive]
16051632pub enum SurfaceDowncastRef < ' a > {
16061633 /// A toplevel window, see [`Window`] for details.
16071634 Window ( & ' a dyn Window ) ,
16081635}
16091636
1637+ impl SurfaceDowncastRef < ' _ > {
1638+ /// Tries to cast this reference into a [`Window`].
1639+ pub fn as_window ( & self ) -> Option < & ' _ dyn Window > {
1640+ match self {
1641+ Self :: Window ( window) => Some ( * window) ,
1642+ _ => None ,
1643+ }
1644+ }
1645+ }
1646+
16101647/// Dynamic mutable reference to one of the common surface traits.
16111648pub enum SurfaceDowncastMut < ' a > {
16121649 /// A toplevel window, see [`Window`] for details.
16131650 Window ( & ' a mut dyn Window ) ,
16141651}
16151652
1653+ impl SurfaceDowncastMut < ' _ > {
1654+ /// Tries to cast this reference into a [`Window`].
1655+ pub fn as_window ( & mut self ) -> Option < & ' _ mut dyn Window > {
1656+ match self {
1657+ Self :: Window ( window) => Some ( * window) ,
1658+ _ => None ,
1659+ }
1660+ }
1661+ }
1662+
16161663/// Helper macro for implementing [`Surface::try_downcast`] and [`Surface::try_downcast_mut`].
16171664/// ## Syntax
16181665/// Use the names of variants of [`SurfaceDowncastRef`] or [`SurfaceDowncastMut`] to return that type:
0 commit comments