macros: Add test cases for remaining expansion contexts

This commit is contained in:
Arthur Cohen 2022-03-15 09:12:37 +01:00
parent 1a2ef9cae9
commit 935b561e7f
4 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,16 @@
macro_rules! define_trait {
($assoc:ident, $i:item) => {
type $assoc;
$i
};
}
trait DefinedThroughMacros {
define_trait!(
Inner,
fn takes_inner(i: Self::Inner) -> Self::Inner {
i
}
);
}

View File

@ -0,0 +1,9 @@
macro_rules! c_fn {
{$name:ident ($($arg_name:ident $arg_ty:ty),*) -> $ret_ty:ty} => {
fn $name($($arg_name: $arg_ty)*) -> $ret_ty;
};
}
extern "C" {
c_fn! {puts (s *const i8) -> i64}
}

View File

@ -0,0 +1,10 @@
macro_rules! print {
() => {
fn puts(s: *const i8);
fn printf(fmt: *const i8, ...);
};
}
extern "C" {
print! {}
}

View File

@ -0,0 +1,25 @@
macro_rules! maybe_impl {
($left:ident, $right:ident, $l_fn:ident, $r_fn:ident) => {
fn $l_fn(value: T) -> Maybe<T> {
Maybe::$left(value)
}
fn $r_fn() -> Maybe<T> {
Maybe::$right
}
};
}
enum Maybe<T> {
Just(T),
Nothing,
}
impl<T> Maybe<T> {
maybe_impl!(Just, Nothing, just, nothing);
}
fn main() {
let _ = Maybe::just(14);
let _: Maybe<i32> = Maybe::nothing();
}