diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-10-01 11:24:23 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-10-01 13:55:16 +0200 |
commit | 27f06ba9557491ab72182bf359be1d5d1237f4c5 (patch) | |
tree | 644215fe06d609a8620bdae606c443cb159116cf /src/impl_traits.rs | |
parent | ebd9d4e90c252ff8f20c3f1733b4884e08a66c78 (diff) |
Add macro to generate failable filter implementation
Diffstat (limited to 'src/impl_traits.rs')
-rw-r--r-- | src/impl_traits.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/impl_traits.rs b/src/impl_traits.rs index a5a8c57..03e616e 100644 --- a/src/impl_traits.rs +++ b/src/impl_traits.rs @@ -67,3 +67,40 @@ macro_rules! impl_operators { impl_operators!($struct_ident, $self_var $arg_var $filter_impl, ); }; } + +/// Variant of impl_operators!() macro for FailableFilter types +#[macro_export] +macro_rules! impl_failable_operators { + ($struct_ident:ident, $self_var: ident $arg_var: ident $filter_impl:block, $( $generic:ident ),*) => { + #[cfg(feature = "unstable-filter-as-fn")] + impl<'a, I, $( $generic: Filter<I>, )*> FnOnce<(&'a I,)> for $struct_ident<$( $generic, )*> { + type Output = Result<bool, Error>; + extern "rust-call" fn call_once<'b>(self, (arg,): (&'a I,)) -> Self::Output { + self.filter(arg) + } + } + + #[cfg(feature = "unstable-filter-as-fn")] + impl<'a, I, $( $generic: Filter<I>, )*> FnMut<(&'a I,)> for $struct_ident<$( $generic, )*> { + extern "rust-call" fn call_mut<'b>(&mut self, (arg,): (&'a I,)) -> Self::Output { + self.filter(arg) + } + } + + #[cfg(feature = "unstable-filter-as-fn")] + impl<'a, I, $( $generic: Filter<I>, )*> Fn<(&'a I,)> for $struct_ident<$( $generic, )*> { + #[allow(unused_variables)] + extern "rust-call" fn call<'b>(&$self_var, ($arg_var,): (&'a I,)) -> Self::Output $filter_impl + } + + #[cfg(not(feature = "unstable-filter-as-fn"))] + impl<I, $( $generic: FailableFilter<I>, )*> FailableFilter<I> for $struct_ident<$( $generic, )*> { + #[allow(unused_variables)] + fn filter(&$self_var, $arg_var: &I) -> Result<bool, Error> $filter_impl + } + }; + ($struct_ident:ident, $self_var: ident $arg_var: ident $filter_impl: block) => { + impl_operators!($struct_ident, $self_var $arg_var $filter_impl, ); + }; +} + |