8000 rune: Store Generator and Stream in AnyObj instead of Mutable (relate… · rune-rs/rune@e4c252d · GitHub
[go: up one dir, main page]

Skip to content

Commit e4c252d

Browse files
committed
rune: Store Generator and Stream in AnyObj instead of Mutable (relates #844)
1 parent eca0e8a commit e4c252d

File tree

12 files changed

+400
-204
lines changed

12 files changed

+400
-204
lines changed

crates/rune-macros/src/any.rs

Lines changed: 57 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,29 @@ where
553553
..
554554
} = &tokens;
555555

556-
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
556+
let empty;
557+
let mut current;
558+
let generic_names;
557559

558-
let generic_names = if attr.static_type.is_some() {
559-
vec![]
560-
} else {
561-
generics.type_params().map(|v| &v.ident).collect::<Vec<_>>()
560+
let (impl_generics, type_generics, where_clause) = match &attr.impl_params {
561+
Some(params) => {
562+
empty = syn::Generics::default();
563+
current = syn::Generics::default();
564+
565+
for p in params {
566+
current.params.push(syn::GenericParam::Type(p.clone()));
567+
}
568+
569+
let (impl_generics, _, where_clause) = empty.split_for_impl();
570+
let (_, type_generics, _) = current.split_for_impl();
571+
generic_names = Vec::new();
572+
(impl_generics, type_generics, where_clause)
573+
}
574+
None => {
575+
current = generics;
576+
generic_names = current.type_params().map(|v| &v.ident).collect::<Vec<_>>();
577+
current.split_for_impl()
578+
}
562579
};
563580

564581
let impl_named = if let [first_name, remainder @ ..] = &generic_names[..] {
@@ -720,85 +737,51 @@ where
720737
}
721738
});
722739

723-
let impl_from_value = 'out: {
724-
if let Some(path) = attr.from_value {
725-
let ty = match &attr.from_value_params {
726-
Some(params) => quote!(#ident<#params>),
727-
None if generics.params.is_empty() => quote!(#ident),
728-
_ => break 'out None,
729-
};
730-
731-
Some(quote! {
732-
impl #from_value for #ty {
733-
fn from_value(value: Value) -> #result<Self, #runtime_error> {
734-
#path(value)
735-
}
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)
736745
}
737-
})
738-
} else {
739-
None
746+
}
740747
}
741-
};
742-
743-
let impl_from_value_ref = 'out: {
744-
if let Some(path) = attr.from_value_ref {
745-
let ty = match &attr.from_value_params {
746-
Some(params) => quote!(#ident<#params>),
747-
None if generics.params.is_empty() => quote!(#ident),
748-
_ => break 'out None,
749-
};
750-
751-
Some(quote! {
752-
impl #unsafe_to_ref for #ty {
753-
type Guard = #raw_any_guard;
748+
});
754749

755-
unsafe fn unsafe_to_ref<'a>(value: #value) -> #vm_result<(&'a Self, Self::Guard)> {
756-
let value = #vm_try!(#path(value));
757-
let (value, guard) = #ref_::into_raw(value);
758-
#vm_result::Ok((value.as_ref(), guard))
759-
}
760-
}
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;
761753

762-
impl #from_value for #ref_<#ty> {
763-
fn from_value(value: #value) -> #result<Self, #runtime_error> {
764-
#path(value)
765-
}
766-
}
767-
})
768-
} else {
769-
None
754+
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))
758+
}
770759
}
771-
};
772760

773-
let impl_from_value_mut = 'out: {
774-
if let Some(path) = attr.from_value_mut {
775-
let ty = match &attr.from_value_params {
776-
Some(params) => quote!(#ident<#params>),
777-
None if generics.params.is_empty() => quote!(#ident),
778-
_ => break 'out None,
779-
};
761+
impl #impl_generics #from_value for #ref_<#ident #type_generics> {
762+
fn from_value(value: #value) -> #result<Self, #runtime_error> {
763+
#path(value)
764+
}
765+
}
766+
});
780767

781-
Some(quote! {
782-
impl #unsafe_to_mut for #ty {
783-
type Guard = #raw_any_guard;
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;
784771

785-
unsafe fn unsafe_to_mut<'a>(value: #value) -> #vm_result<(&'a mut Self, Self::Guard)> {
786-
let value = #vm_try!(#path(value));
787-
let (mut value, guard) = #mut_::into_raw(value);
788-
#vm_result::Ok((value.as_mut(), guard))
789-
}
790-
}
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))
776+
}
777+
}
791778

792-
impl #from_value for #mut_<#ty> {
793-
fn from_value(value: #value) -> #result<Self, #runtime_error> {
794-
#path(value)
795-
}
796-
}
797-
})
798-
} else {
799-
None
779+
impl #impl_generics #from_value for #mut_<#ident #type_generics> {
780+
fn from_value(value: #value) -> #result<Self, #runtime_error> {
781+
#path(value)
782+
}
800783
}
801-
};
784+
});
802785

803786
quote! {
804787
#install_with

crates/rune-macros/src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) struct TypeAttr {
9898
/// Method to use to convert from value mut.
9999
pub(crate) from_value_mut: Option<syn::Path>,
100100
/// Method to use to convert from value.
101-
pub(crate) from_value_params: Option<syn::punctuated::Punctuated<syn::Type, Token![,]>>,
101+
pub(crate) impl_params: Option<syn::punctuated::Punctuated<syn::TypeParam, Token![,]>>,
102102
}
103103

104104
/// Parsed #[const_value(..)] field attributes.
@@ -610,11 +610,11 @@ impl Context {
610610
} else if meta.path == FROM_VALUE_MUT {
611611
meta.input.parse::<Token![=]>()?;
612612
attr.from_value_mut = Some(meta.input.parse()?);
613-
} else if meta.path == FROM_VALUE_PARAMS {
613+
} else if meta.path.is_ident("impl_params") {
614614
meta.input.parse::<Token![=]>()?;
615615
let content;
616616
syn::bracketed!(content in meta.input);
617-
attr.from_value_params =
617+
attr.impl_params =
618618
Some(syn::punctuated::Punctuated::parse_terminated(&content)?);
619619
} else {
620620
return Err(syn::Error::new_spanned(

crates/rune-macros/src/internals.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub const STATIC_TYPE: Symbol = Symbol("static_type");
2828
pub const FROM_VALUE: Symbol = Symbol("from_value");
2929
pub const FROM_VALUE_REF: Symbol = Symbol("from_value_ref");
3030
pub const FROM_VALUE_MUT: Symbol = Symbol("from_value_mut");
31-
pub const FROM_VALUE_PARAMS: Symbol = Symbol("from_value_params");
3231
pub const GET: Symbol = Symbol("get");
3332
pub const SET: Symbol = Symbol("set");
3433
pub const COPY: Symbol = Symbol("copy");

crates/rune/src/modules/generator.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

crates/rune/src/modules/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub mod disable_io;
2828
pub mod f64;
2929
pub mod fmt;
3030
pub mod future;
31-
pub mod generator;
3231
pub mod hash;
3332
pub mod i64;
3433
pub mod io;

0 commit comments

Comments
 (0)
0