8000 rune: Clean up macros and add more instrumentation · rune-rs/rune@798e31f · GitHub
[go: up one dir, main page]

Skip to content

Commit 798e31f

Browse files
committed
rune: Clean up macros and add more instrumentation
1 parent 38c5f2d commit 798e31f

File tree

13 files changed

+236
-321
lines changed

13 files changed

+236
-321
lines changed

crates/rune-macros/src/any.rs

Lines changed: 54 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -521,24 +521,17 @@ where
521521
any_t,
522522
context_error,
523523
fmt,
524-
from_value,
525524
hash,
526525
install_with,
527526
item,
528527
maybe_type_of,
529528
meta,
530529
module,
531-
mut_,
532530
named,
533531
non_null,
534-
raw_any_guard,
535532
raw_value_guard,
536-
ref_,
537533
result,
538-
runtime_error,
539534
static_type_info,
540-
static_type_mod,
541-
to_value,
542535
type_hash_t,
543536
type_of,
544537
unsafe_to_mut,
@@ -617,180 +610,92 @@ where
617610
}
618611
};
619612

620-
let impl_type_of = if let Some(ty) = attr.static_type {
621-
let ty_hash = syn::Ident::new(&format!("{ty}_HASH"), ty.span());
613+
let type_hash = type_hash.into_inner();
622614

623-
Some(quote! {
624-
#[automatically_derived]
625-
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
626-
const HASH: #hash = #static_type_mod::#ty_hash;
627-
}
628-
629-
#[automatically_derived]
630-
impl #impl_generics #type_of for #ident #type_generics #where_clause {
631-
const STATIC_TYPE_INFO: #static_type_info = #static_type_info::static_type(#static_type_mod::#ty);
632-
}
633-
634-
#[automatically_derived]
635-
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
636-
#[inline]
637-
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
638-
#meta::DocType::with_generics(
639-
<Self as #type_hash_t>::HASH,
640-
[#(<#generic_names as #maybe_type_of>::maybe_type_of()),*]
641-
)
642-
}
643-
}
644-
})
645-
} else if attr.builtin.is_none() {
646-
let type_hash = type_hash.into_inner();
647-
648-
let make_hash = if !generic_names.is_empty() {
649-
quote!(#hash::new_with_type_parameters(#type_hash, #hash::parameters([#(<#generic_names as #type_hash_t>::HASH),*])))
650-
} else {
651-
quote!(#hash::new(#type_hash))
652-
};
653-
654-
let type_parameters =
655-
quote!(#hash::parameters([#(<#generic_names as #type_hash_t>::HASH),*]));
656-
657-
Some(quote! {
658-
#[automatically_derived]
659-
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
660-
const HASH: #hash = #make_hash;
661-
}
662-
663-
#[automatically_derived]
664-
impl #impl_generics #type_of for #ident #type_generics #where_clause {
665-
const PARAMETERS: #hash = #type_parameters;
666-
const STATIC_TYPE_INFO: #static_type_info = #static_type_info::any_type_info(<Self as #any_t>::ANY_TYPE_INFO);
667-
}
668-
669-
#[automatically_derived]
670-
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
671-
#[inline]
672-
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
673-
#meta::DocType::with_generics(
674-
<Self as #type_hash_t>::HASH,
675-
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
676-
)
677-
}
678-
}
679-
})
615+
let make_hash = if !generic_names.is_empty() {
616+
quote!(#hash::new_with_type_parameters(#type_hash, #hash::parameters([#(<#generic_names as #type_hash_t>::HASH),*])))
680617
} else {
681-
None
618+
quote!(#hash::new(#type_hash))
682619
};
683620

684-
let non_builtin = attr.builtin.is_none().then(|| {
685-
quote! {
686-
#[automatically_derived]
687-
impl #impl_generics #any_t for #ident #type_generics #where_clause {
688-
}
621+
let type_parameters =
622+
quote!(#hash::parameters([#(<#generic_names as #type_hash_t>::HASH),*]));
689623

690-
#[automatically_derived]
691-
impl #impl_generics #unsafe_to_ref for #ident #type_generics #where_clause {
692-
type Guard = #raw_value_guard;
693-
694-
unsafe fn unsafe_to_ref<'a>(value: #value) -> #vm_result<(&'a Self, Self::Guard)> {
695-
let (value, guard) = #vm_try!(#value::into_any_ref_ptr(value));
696-
#vm_result::Ok((#non_null::as_ref(&value), guard))
697-
}
698-
}
699-
700-
#[automatically_derived]
701-
impl #impl_generics #unsafe_to_mut for #ident #type_generics #where_clause {
702-
type Guard = #raw_value_guard;
703-
704-
unsafe fn unsafe_to_mut<'a>(value: #value) -> #vm_result<(&'a mut Self, Self::Guard)> {
705-
let (mut value, guard) = #vm_try!(#value::into_any_mut_ptr(value));
706-
#vm_result::Ok((#non_null::as_mut(&mut value), guard))
707-
}
708-
}
709-
710-
#[automatically_derived]
711-
impl #impl_generics #unsafe_to_value for &#ident #type_generics #where_clause {
712-
type Guard = #value_ref_guard;
713-
714-
unsafe fn unsafe_to_value(self) -> #vm_result<(#value, Self::Guard)> {
715-
let (shared, guard) = #vm_try!(#value::from_ref(self));
716-
#vm_result::Ok((shared, guard))
717-
}
718-
719-
fn try_into_to_value(self) -> Option<impl #to_value> {
720-
Option::<&str>::None
721-
}
722-
}
723-
724-
#[automatically_derived]
725-
impl #impl_generics #unsafe_to_value for &mut #ident #type_generics #where_clause {
726-
type Guard = #value_mut_guard;
624+
let impl_type_of = Some(quote! {
625+
#[automatically_derived]
626+
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
627+
const HASH: #hash = #make_hash;
628+
}
727629

728-
unsafe fn unsafe_to_value(self) -> #vm_result<(#value, Self::Guard)> {
729-
let (shared, guard) = #vm_try!(#value::from_mut(self));
730-
#vm_result::Ok((shared, guard))
731-
}
630+
#[automatically_derived]
631+
impl #impl_generics #type_of for #ident #type_generics #where_clause {
632+
const PARAMETERS: #hash = #type_parameters;
633+
const STATIC_TYPE_INFO: #static_type_info = #static_type_info::any_type_info(<Self as #any_t>::ANY_TYPE_INFO);
634+
}
732635

733-
fn try_into_to_value(self) -> Option<impl #to_value> {
734-
Option::<&str>::None
735-
}
636+
#[automatically_derived]
637+
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
638+
#[inline]
639+
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
640+
#meta::DocType::with_generics(
641+
<Self as #type_hash_t>::HASH,
642+
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
643+
)
736644
}
737645
}
738646
});
739647

740-
let impl_from_value = attr.from_value.as_ref().map(|path| {
741-
quote! {
742-
impl #impl_generics #from_value for #ident #type_generics {
743-
fn from_value(value: #value) -> #result<Self, #runtime_error> {
744-
#path(value)
745-
}
746-
}
648+
let impl_any = quote! {
649+
#[automatically_derived]
650+
impl #impl_generics #any_t for #ident #type_generics #where_clause {
747651
}
748-
});
749652

750-
let impl_from_value_ref = attr.from_value_ref.as_ref().map(|path| quote! {
751-
impl #impl_generics #unsafe_to_ref for #ident #type_generics {
752-
type Guard = #raw_any_guard;
653+
#[automatically_derived]
654+
impl #impl_generics #unsafe_to_ref for #ident #type_generics #where_clause {
655+
type Guard = #raw_value_guard;
753656

754657
unsafe fn unsafe_to_ref<'a>(value: #value) -> #vm_result<(&'a Self, Self::Guard)> {
755-
let value = #vm_try!(#path(value));
756-
let (value, guard) = #ref_::into_raw(value);
757-
#vm_result::Ok((value.as_ref(), guard))
658+
let (value, guard) = #vm_try!(#value::into_any_ref_ptr(value));
659+
#vm_result::Ok((#non_null::as_ref(&value), guard))
758660
}
759661
}
760662

761-
impl #impl_generics #from_value for #ref_<#ident #type_generics> {
762-
fn from_value(value: #value) -> #result<Self, #runtime_error> {
763-
#path(value)
663+
#[automatically_derived]
664+
impl #impl_generics #unsafe_to_mut for #ident #type_generics #where_clause {
665+
type Guard = #raw_value_guard;
666+
667+
unsafe fn unsafe_to_mut<'a>(value: #value) -> #vm_result<(&'a mut Self, Self::Guard)> {
668+
let (mut value, guard) = #vm_try!(#value::into_any_mut_ptr(value));
669+
#vm_result::Ok((#non_null::as_mut(&mut value), guard))
764670
}
765671
}
766-
});
767672

768-
let impl_from_value_mut = attr.from_value_mut.as_ref().map(|path| quote! {
769-
impl #impl_generics #unsafe_to_mut for #ident #type_generics {
770-
type Guard = #raw_any_guard;
673+
#[automatically_derived]
674+
impl #impl_generics #unsafe_to_value for &#ident #type_generics #where_clause {
675+
type Guard = #value_ref_guard;
771676

772-
unsafe fn unsafe_to_mut<'a>(value: #value) -> #vm_result<(&'a mut Self, Self::Guard)> {
773-
let value = #vm_try!(#path(value));
774-
let (mut value, guard) = #mut_::into_raw(value);
775-
#vm_result::Ok((value.as_mut(), guard))
677+
unsafe fn unsafe_to_value(self) -> #vm_result<(#value, Self::Guard)> {
678+
let (shared, guard) = #vm_try!(#value::from_ref(self));
679+
#vm_result::Ok((shared, guard))
776680
}
777681
}
778682

779-
impl #impl_generics #from_value for #mut_<#ident #type_generics> {
780-
fn from_value(value: #value) -> #result<Self, #runtime_error> {
781-
#path(value)
683+
#[automatically_derived]
684+
impl #impl_generics #unsafe_to_value for &mut #ident #type_generics #where_clause {
685+
type Guard = #value_mut_guard;
686+
687+
unsafe fn unsafe_to_value(self) -> #vm_result<(#value, Self::Guard)> {
688+
let (shared, guard) = #vm_try!(#value::from_mut(self));
689+
#vm_result::Ok((shared, guard))
782690
}
783691
}
784-
});
692+
};
785693

786694
quote! {
787695
#install_with
788696
#impl_named
789697
#impl_type_of
790-
#impl_from_value
791-
#impl_from_value_ref
792-
#impl_from_value_mut
793-
#non_builtin
698+
#impl_any
794699
}
795700
}
796701
}

0 commit comments

Comments
 (0)
0