Skip to content

Commit

Permalink
macros: Cleaner macros (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog authored Nov 4, 2024
1 parent 84d76ae commit d2dd714
Show file tree
Hide file tree
Showing 18 changed files with 686 additions and 436 deletions.
477 changes: 386 additions & 91 deletions crates/rune-macros/src/any.rs

Large diffs are not rendered by default.

219 changes: 142 additions & 77 deletions crates/rune-macros/src/context.rs

Large diffs are not rendered by default.

89 changes: 0 additions & 89 deletions crates/rune-macros/src/internals.rs

This file was deleted.

11 changes: 5 additions & 6 deletions crates/rune-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@
#![allow(clippy::manual_map)]

use ::quote::format_ident;
use syn::{Generics, Path};

extern crate proc_macro;

mod any;
mod const_value;
mod context;
mod from_value;
mod function;
mod hash;
mod inst_display;
mod internals;
mod item;
mod macro_;
mod module;
Expand All @@ -49,7 +43,12 @@ mod to_tokens;
mod to_value;

use self::context::{Context, Tokens};

use ::quote::format_ident;
use proc_macro2::TokenStream;
use syn::{Generics, Path};

const RUNE: &str = "rune";

#[proc_macro]
pub fn quote(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand Down
5 changes: 1 addition & 4 deletions crates/rune-macros/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,8 @@ impl Expander<'_> {
if i - skipped != meta_fields.len() {
self.cx.error(syn::Error::new_spanned(
field,
format!(
"The first sequence of fields may have `#[rune({})]`, \
"The first sequence of fields may have `#[rune(meta)]`, \
but field is outside of that sequence.",
crate::internals::META,
),
));
return Err(());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune-macros/src/quote/inner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use proc_macro2 as p;

pub(crate) const S: Punct = Punct::new("::");
pub(crate) const RUNE: &str = "rune";
pub(crate) const MACROS: RuneModule = RuneModule("macros");
pub(crate) const AST: RuneModule = RuneModule("ast");

use crate::RUNE;
pub(crate) trait ToTokens {
fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span);
}
Expand Down
26 changes: 17 additions & 9 deletions crates/rune/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::alloc;
use crate::compile::meta;
use crate::hash::Hash;
use crate::runtime::{
self, AnyTypeInfo, FromValue, InstAddress, MaybeTypeOf, Memory, Output, ToReturn, TypeHash,
TypeOf, UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult,
self, AnyTypeInfo, FromValue, InstAddress, MaybeTypeOf, Memory, Output, RuntimeError, ToReturn,
TypeHash, TypeOf, UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult,
};

// Expand to function variable bindings.
Expand All @@ -29,9 +29,14 @@ macro_rules! access_memory {
$(let $var = replace($var, Value::empty());)*

$(
let $var = vm_try!($from_fn($var).with_error(|| VmErrorKind::BadArgument {
arg: $num,
}));
let $var = match $from_fn($var) {
Ok($var) => $var,
Err(error) => {
return VmResult::err(error).with_error(|| VmErrorKind::BadArgument {
arg: $num,
});
}
};
)*
};
}
Expand Down Expand Up @@ -195,14 +200,16 @@ where
// Fake guard for owned values.
struct Guard;

fn from_value<T>(value: Value) -> VmResult<(T, Guard)>
#[inline(always)]
fn from_value<T>(value: Value) -> Result<(T, Guard), RuntimeError>
where
T: FromValue,
{
VmResult::Ok((vm_try!(T::from_value(value)), Guard))
Ok((T::from_value(value)?, Guard))
}

fn unsafe_to_ref<'a, T>(value: Value) -> VmResult<(&'a T, T::Guard)>
#[inline(always)]
fn unsafe_to_ref<'a, T>(value: Value) -> Result<(&'a T, T::Guard), RuntimeError>
where
T: ?Sized + UnsafeToRef,
{
Expand All @@ -211,7 +218,8 @@ where
unsafe { T::unsafe_to_ref(value) }
}

fn unsafe_to_mut<'a, T>(value: Value) -> VmResult<(&'a mut T, T::Guard)>
#[inline(always)]
fn unsafe_to_mut<'a, T>(value: Value) -> Result<(&'a mut T, T::Guard), RuntimeError>
where
T: ?Sized + UnsafeToMut,
{
Expand Down
48 changes: 0 additions & 48 deletions crates/rune/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,6 @@ macro_rules! resolve_context {
};
}

macro_rules! impl_one_builtin_type_of {
(impl $(<$($p:ident),*>)? $path:path, $ty:ty) => {
impl $(<$($p,)*>)* $crate::TypeHash for $ty {
const HASH: $crate::Hash = ::rune_macros::hash_in!(crate, $path);
}

impl $(<$($p,)*>)* $crate::runtime::TypeOf for $ty
where
$($($p: $crate::runtime::MaybeTypeOf,)*)*
{
const STATIC_TYPE_INFO: $crate::runtime::AnyTypeInfo = $crate::runtime::AnyTypeInfo::new(
{
fn full_name(f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "{}", ::rune_macros::item_in!(crate, $path))
}

full_name
},
<Self as $crate::TypeHash>::HASH,
);
}

impl $(<$($p,)*>)* $crate::runtime::MaybeTypeOf for $ty
where
$($($p: $crate::runtime::MaybeTypeOf,)*)*
{
#[inline]
fn maybe_type_of() -> $crate::alloc::Result<$crate::compile::meta::DocType> {
Ok($crate::compile::meta::DocType::new(<$ty as $crate::TypeHash>::HASH))
}
}
}
}

/// Call the given macro with repeated type arguments and counts.
macro_rules! repeat_macro {
($macro:ident) => {
Expand Down Expand Up @@ -86,17 +52,3 @@ macro_rules! cfg_std {
)*
}
}

macro_rules! impl_builtin_type_of {
(
$(
$(#[$($impl_meta:meta)*])*
impl $(<$($p:ident),*>)? $path:path, $ty:ty;
)*
) => {
$(
$(#[$($impl_meta)*])*
impl_one_builtin_type_of!(impl $(<$($p),*>)* $path, $ty);
)*
}
}
Loading

0 comments on commit d2dd714

Please sign in to comment.