Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port from syn -> venial #1

Merged
merged 6 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,24 @@ mod tests {
pub fn with_doc_comments() -> ControlFlow<usize> {
ControlFlow::Break(11)?;
}

#[tryvial]
unsafe fn generic_fn<T, U: Clone>(x: T, y: &U) -> ControlFlow<U>
where
T: PartialEq<U>,
{
if x == *y {
ControlFlow::Break(y.clone())?;
}
}

struct MyStruct(u32);

impl core::convert::TryFrom<&str> for MyStruct {
type Error = core::num::ParseIntError;
#[tryvial]
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self(value.parse()?)
}
}
}
2 changes: 1 addition & 1 deletion tryvial-proc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["full"] }
venial = "0.5.0"
52 changes: 37 additions & 15 deletions tryvial-proc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::ItemFn;
use venial::{Declaration, Error, Function};

/// An attribute macro that performs "Ok-wrapping" on the return value of a `fn` item.
/// This is compatible with [`Result`], [`Option`], [`ControlFlow`], and any type that
Expand Down Expand Up @@ -33,22 +33,44 @@ use syn::ItemFn;
/// [`ControlFlow`]: core::ops::ControlFlow
#[proc_macro_attribute]
pub fn tryvial(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item = syn::parse_macro_input!(item as ItemFn);
impl_tryvial(item).into()
impl_tryvial(item.into())
.unwrap_or_else(|e| e.to_compile_error())
.into()
}

fn impl_tryvial(item: ItemFn) -> TokenStream2 {
let ItemFn {
attrs,
vis,
sig,
block,
} = item;
fn impl_tryvial(input: TokenStream2) -> Result<TokenStream2, Error> {
let decl = venial::parse_declaration(input)?;
let Function {
attributes,
vis_marker,
qualifiers,
tk_fn_keyword,
name,
generic_params,
tk_params_parens: _,
params,
where_clause,
tk_return_arrow: _,
return_ty,
tk_semicolon: _,
body,
} = match decl {
Declaration::Function(item) => item,
_ => Err(Error::new("`#[tryvial]` is supported only on `fn` items"))?,
};

quote! {
#(#attrs)*
#vis #sig {
::core::iter::empty().try_fold(#block, |_, __x: ::core::convert::Infallible| match __x {})
let body = body.ok_or(Error::new(
"`#[tryvial]` can only be used on functions with a body",
))?;

let return_ty = return_ty.map_or_else(|| quote! { () }, |ty| quote! { #ty });

Ok(quote! {
#(#attributes)*
#vis_marker #qualifiers #tk_fn_keyword #name #generic_params ( #params ) -> #return_ty
#where_clause
{
::core::iter::empty().try_fold(#body, |_, __x: ::core::convert::Infallible| match __x {})
}
}
})
}