Skip to content

Commit

Permalink
rune: Store Generator and Stream in AnyObj instead of Mutable (relates
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 1, 2024
1 parent eca0e8a commit e4c252d
Show file tree
Hide file tree
Showing 12 changed files with 400 additions and 204 deletions.
131 changes: 57 additions & 74 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,29 @@ where
..
} = &tokens;

let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
let empty;
let mut current;
let generic_names;

let generic_names = if attr.static_type.is_some() {
vec![]
} else {
generics.type_params().map(|v| &v.ident).collect::<Vec<_>>()
let (impl_generics, type_generics, where_clause) = match &attr.impl_params {
Some(params) => {
empty = syn::Generics::default();
current = syn::Generics::default();

for p in params {
current.params.push(syn::GenericParam::Type(p.clone()));
}

let (impl_generics, _, where_clause) = empty.split_for_impl();
let (_, type_generics, _) = current.split_for_impl();
generic_names = Vec::new();
(impl_generics, type_generics, where_clause)
}
None => {
current = generics;
generic_names = current.type_params().map(|v| &v.ident).collect::<Vec<_>>();
current.split_for_impl()
}
};

let impl_named = if let [first_name, remainder @ ..] = &generic_names[..] {
Expand Down Expand Up @@ -720,85 +737,51 @@ where
}
});

let impl_from_value = 'out: {
if let Some(path) = attr.from_value {
let ty = match &attr.from_value_params {
Some(params) => quote!(#ident<#params>),
None if generics.params.is_empty() => quote!(#ident),
_ => break 'out None,
};

Some(quote! {
impl #from_value for #ty {
fn from_value(value: Value) -> #result<Self, #runtime_error> {
#path(value)
}
let impl_from_value = attr.from_value.as_ref().map(|path| {
quote! {
impl #impl_generics #from_value for #ident #type_generics {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
})
} else {
None
}
}
};

let impl_from_value_ref = 'out: {
if let Some(path) = attr.from_value_ref {
let ty = match &attr.from_value_params {
Some(params) => quote!(#ident<#params>),
None if generics.params.is_empty() => quote!(#ident),
_ => break 'out None,
};

Some(quote! {
impl #unsafe_to_ref for #ty {
type Guard = #raw_any_guard;
});

unsafe fn unsafe_to_ref<'a>(value: #value) -> #vm_result<(&'a Self, Self::Guard)> {
let value = #vm_try!(#path(value));
let (value, guard) = #ref_::into_raw(value);
#vm_result::Ok((value.as_ref(), guard))
}
}
let impl_from_value_ref = attr.from_value_ref.as_ref().map(|path| quote! {
impl #impl_generics #unsafe_to_ref for #ident #type_generics {
type Guard = #raw_any_guard;

impl #from_value for #ref_<#ty> {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
}
})
} else {
None
unsafe fn unsafe_to_ref<'a>(value: #value) -> #vm_result<(&'a Self, Self::Guard)> {
let value = #vm_try!(#path(value));
let (value, guard) = #ref_::into_raw(value);
#vm_result::Ok((value.as_ref(), guard))
}
}
};

let impl_from_value_mut = 'out: {
if let Some(path) = attr.from_value_mut {
let ty = match &attr.from_value_params {
Some(params) => quote!(#ident<#params>),
None if generics.params.is_empty() => quote!(#ident),
_ => break 'out None,
};
impl #impl_generics #from_value for #ref_<#ident #type_generics> {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
}
});

Some(quote! {
impl #unsafe_to_mut for #ty {
type Guard = #raw_any_guard;
let impl_from_value_mut = attr.from_value_mut.as_ref().map(|path| quote! {
impl #impl_generics #unsafe_to_mut for #ident #type_generics {
type Guard = #raw_any_guard;

unsafe fn unsafe_to_mut<'a>(value: #value) -> #vm_result<(&'a mut Self, Self::Guard)> {
let value = #vm_try!(#path(value));
let (mut value, guard) = #mut_::into_raw(value);
#vm_result::Ok((value.as_mut(), guard))
}
}
unsafe fn unsafe_to_mut<'a>(value: #value) -> #vm_result<(&'a mut Self, Self::Guard)> {
let value = #vm_try!(#path(value));
let (mut value, guard) = #mut_::into_raw(value);
#vm_result::Ok((value.as_mut(), guard))
}
}

impl #from_value for #mut_<#ty> {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
}
})
} else {
None
impl #impl_generics #from_value for #mut_<#ident #type_generics> {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
}
};
});

quote! {
#install_with
Expand Down
6 changes: 3 additions & 3 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) struct TypeAttr {
/// Method to use to convert from value mut.
pub(crate) from_value_mut: Option<syn::Path>,
/// Method to use to convert from value.
pub(crate) from_value_params: Option<syn::punctuated::Punctuated<syn::Type, Token![,]>>,
pub(crate) impl_params: Option<syn::punctuated::Punctuated<syn::TypeParam, Token![,]>>,
}

/// Parsed #[const_value(..)] field attributes.
Expand Down Expand Up @@ -610,11 +610,11 @@ impl Context {
} else if meta.path == FROM_VALUE_MUT {
meta.input.parse::<Token![=]>()?;
attr.from_value_mut = Some(meta.input.parse()?);
} else if meta.path == FROM_VALUE_PARAMS {
} else if meta.path.is_ident("impl_params") {
meta.input.parse::<Token![=]>()?;
let content;
syn::bracketed!(content in meta.input);
attr.from_value_params =
attr.impl_params =
Some(syn::punctuated::Punctuated::parse_terminated(&content)?);
} else {
return Err(syn::Error::new_spanned(
Expand Down
1 change: 0 additions & 1 deletion crates/rune-macros/src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub const STATIC_TYPE: Symbol = Symbol("static_type");
pub const FROM_VALUE: Symbol = Symbol("from_value");
pub const FROM_VALUE_REF: Symbol = Symbol("from_value_ref");
pub const FROM_VALUE_MUT: Symbol = Symbol("from_value_mut");
pub const FROM_VALUE_PARAMS: Symbol = Symbol("from_value_params");
pub const GET: Symbol = Symbol("get");
pub const SET: Symbol = Symbol("set");
pub const COPY: Symbol = Symbol("copy");
Expand Down
13 changes: 0 additions & 13 deletions crates/rune/src/modules/generator.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/rune/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub mod disable_io;
pub mod f64;
pub mod fmt;
pub mod future;
pub mod generator;
pub mod hash;
pub mod i64;
pub mod io;
Expand Down
Loading

0 comments on commit e4c252d

Please sign in to comment.