Skip to content

Commit 5fef720

Browse files
committed
introduce Upcast impls for Arc
1 parent 4387277 commit 5fef720

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

book/src/SUMMARY.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
- [Intro](./intro.md)
44
- [`formality_core`: the Formality system](./formality_core.md)
5-
- [Defining your lang](./formality_core/lang.md)
6-
- [Defining terms with the `term` macro](./formality_core/terms.md)
7-
- [Parsing](./formality_core/parse.md)
8-
- [Customizing debug](./formality_core/debug.md)
9-
- [Variables](./formality_core/variables.md)
10-
- [Collections](./formality_core/collections.md)
11-
- [Judgment functions and inference rules](./formality_core/judgment_fn.md)
12-
- [FAQ and troubleshooting](./formality_core/faq.md)
5+
- [Defining your lang](./formality_core/lang.md)
6+
- [Defining terms with the `term` macro](./formality_core/terms.md)
7+
- [Parsing](./formality_core/parse.md)
8+
- [Customizing debug](./formality_core/debug.md)
9+
- [Constructors](./formality_core/constructors.md)
10+
- [Variables](./formality_core/variables.md)
11+
- [Collections](./formality_core/collections.md)
12+
- [Judgment functions and inference rules](./formality_core/judgment_fn.md)
13+
- [FAQ and troubleshooting](./formality_core/faq.md)

crates/formality-macros/src/cast.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ pub(crate) fn upcast_impls(s: synstructure::Structure) -> Vec<TokenStream> {
1111
s.variants()
1212
.iter()
1313
.filter(|v| num_variants == 1 || has_isa_attr(v.ast().attrs))
14-
.map(|v| upcast_to_variant(&s, v))
14+
.map(|v: &VariantInfo<'_>| upcast_to_variant(&s, v))
1515
.chain(Some(self_upcast(&s)))
16+
.chain(Some(arc_upcast(&s)))
1617
.collect()
1718
}
1819

@@ -28,6 +29,25 @@ fn self_upcast(s: &synstructure::Structure) -> TokenStream {
2829
})
2930
}
3031

32+
fn arc_upcast(s: &synstructure::Structure) -> TokenStream {
33+
let type_name = &s.ast().ident;
34+
let (impl_generics, type_generics, where_clauses) = s.ast().generics.split_for_impl();
35+
36+
quote_spanned! { type_name.span() =>
37+
const _: () = {
38+
use formality_core::Upcast;
39+
use std::sync::Arc;
40+
41+
impl #impl_generics Upcast<Arc<Self>> for #type_name #type_generics
42+
#where_clauses {
43+
fn upcast(self) -> Arc<Self> {
44+
Arc::new(self)
45+
}
46+
}
47+
};
48+
}
49+
}
50+
3151
fn upcast_to_variant(s: &synstructure::Structure, v: &VariantInfo) -> TokenStream {
3252
let binding_tys: Vec<&Type> = v.bindings().iter().map(|b| &b.ast().ty).collect();
3353
let binding_names: Vec<&syn::Ident> = v.bindings().iter().map(|b| &b.binding).collect();

0 commit comments

Comments
 (0)