Skip to content

Commit e93be70

Browse files
committed
downcast-arc
1 parent 20f9f44 commit e93be70

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

crates/formality-macros/src/cast.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn upcast_impls(s: synstructure::Structure) -> Vec<TokenStream> {
1313
.filter(|v| num_variants == 1 || has_isa_attr(v.ast().attrs))
1414
.map(|v: &VariantInfo<'_>| upcast_to_variant(&s, v))
1515
.chain(Some(self_upcast(&s)))
16-
.chain(Some(arc_upcast(&s)))
16+
.chain(Some(arc_cast(&s)))
1717
.collect()
1818
}
1919

@@ -29,13 +29,14 @@ fn self_upcast(s: &synstructure::Structure) -> TokenStream {
2929
})
3030
}
3131

32-
fn arc_upcast(s: &synstructure::Structure) -> TokenStream {
32+
fn arc_cast(s: &synstructure::Structure) -> TokenStream {
3333
let type_name = &s.ast().ident;
3434
let (impl_generics, type_generics, where_clauses) = s.ast().generics.split_for_impl();
3535

3636
quote_spanned! { type_name.span() =>
3737
const _: () = {
3838
use formality_core::Upcast;
39+
use formality_core::DowncastFrom;
3940
use std::sync::Arc;
4041

4142
impl #impl_generics Upcast<Arc<Self>> for #type_name #type_generics
@@ -44,6 +45,14 @@ fn arc_upcast(s: &synstructure::Structure) -> TokenStream {
4445
Arc::new(self)
4546
}
4647
}
48+
49+
50+
impl #impl_generics DowncastFrom<Arc<Self>> for #type_name #type_generics
51+
#where_clauses {
52+
fn downcast_from(t: &Arc<Self>) -> Option<Self> {
53+
Some(Self::clone(t))
54+
}
55+
}
4756
};
4857
}
4958
}

0 commit comments

Comments
 (0)