Skip to content

Commit

Permalink
Work around trait_upcasting being unstable
Browse files Browse the repository at this point in the history
Credits for Masonry/Xilem for introducing me to this trick.
  • Loading branch information
kmicklas committed Jun 6, 2024
1 parent d988869 commit e881af7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 0 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export RUST_BACKTRACE=1
export RUSTC_BOOTSTRAP=1
1 change: 0 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:

env:
CARGO_TERM_COLOR: always
RUSTC_BOOTSTRAP: 1

jobs:
build:
Expand Down
15 changes: 14 additions & 1 deletion ravel-web/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ use crate::{
BuildCx, Builder, RebuildCx, State, View, Web,
};

/// Trait for upcasting to [`Any`], implemented automatically.
///
/// This is a workaround until `trait_upcasting` is stabilized.
pub trait AsAny: Any {
fn as_mut_dyn_any(&mut self) -> &mut dyn Any;
}

impl<T: Any> AsAny for T {
fn as_mut_dyn_any(&mut self) -> &mut dyn Any {
self
}
}

/// A wrapper around a [`View`], erasing its [`State`] type.
pub struct AnyView<V: View, Output> {
inner: V,
Expand Down Expand Up @@ -35,7 +48,7 @@ where
}

fn rebuild(self, cx: RebuildCx, state: &mut Self::State) {
match (state.state.deref_mut() as &mut dyn Any)
match (state.state.as_mut_dyn_any().deref_mut() as &mut dyn Any)
.downcast_mut::<V::State>()
{
Some(state) => self.inner.rebuild(cx, state),
Expand Down
5 changes: 2 additions & 3 deletions ravel-web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! A web/DOM/HTML backend for [`ravel`].

#![feature(trait_upcasting)]
use std::{any::Any, sync::Arc};
use std::sync::Arc;

use atomic_waker::AtomicWaker;
use dom::Position;
Expand Down Expand Up @@ -43,7 +42,7 @@ pub struct RebuildCx<'cx> {
}

/// Trait for the state of a [`Web`] component.
pub trait State<Output>: Any {
pub trait State<Output>: AsAny {
/// Processes a "frame".
///
/// This method can respond to externally triggered events by changing the
Expand Down

0 comments on commit e881af7

Please sign in to comment.