macro_rules! impl_arc_conversions {
(
@method_into_box
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
#[inline]
fn into_box(self) -> $box_type<$($generics),*>
where
Self: 'static,
{
$box_type::new_with_optional_name(
impl_arc_conversions!(@make_closure $call_mode, self.function,
$($arg),*),
self.name
)
}
};
(
@method_into_rc
$arc_type:ident < $($generics:ident),* >,
$rc_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
#[inline]
fn into_rc(self) -> $rc_type<$($generics),*>
where
Self: 'static,
{
$rc_type::new_with_optional_name(
impl_arc_conversions!(@make_closure $call_mode, self.function,
$($arg),*),
self.name
)
}
};
(
@method_into_once
$arc_type:ident < $($generics:ident),* >,
$once_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
#[inline]
fn into_once(self) -> $once_type<$($generics),*>
where
Self: 'static,
{
$once_type::new_with_optional_name(
impl_arc_conversions!(@make_closure $call_mode, self.function,
$($arg),*),
self.name
)
}
};
(
@method_to_box
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
#[inline]
fn to_box(&self) -> $box_type<$($generics),*>
where
Self: 'static,
{
let self_fn = self.function.clone();
let self_name = self.name.clone();
$box_type::new_with_optional_name(
impl_arc_conversions!(@make_closure $call_mode, self_fn,
$($arg),*),
self_name
)
}
};
(
@method_to_rc
$arc_type:ident < $($generics:ident),* >,
$rc_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
#[inline]
fn to_rc(&self) -> $rc_type<$($generics),*>
where
Self: 'static,
{
let self_fn = self.function.clone();
let self_name = self.name.clone();
$rc_type::new_with_optional_name(
impl_arc_conversions!(@make_closure $call_mode, self_fn,
$($arg),*),
self_name
)
}
};
(
@method_to_once
$arc_type:ident < $($generics:ident),* >,
$once_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
#[inline]
fn to_once(&self) -> $once_type<$($generics),*>
where
Self: 'static,
{
let self_fn = self.function.clone();
let self_name = self.name.clone();
$once_type::new_with_optional_name(
impl_arc_conversions!(@make_closure $call_mode, self_fn,
$($arg),*),
self_name
)
}
};
(
@fn_method_into
direct,
($($arg:ident : $arg_ty:ty),*)
) => {
#[inline]
fn into_fn(self) -> impl Fn($($arg_ty),*)
{
move |$($arg),*| (self.function)($($arg),*)
}
};
(
@fn_method_into
direct,
($($arg:ident : $arg_ty:ty),*) -> $ret:ty
) => {
#[inline]
fn into_fn(self) -> impl Fn($($arg_ty),*) -> $ret
{
move |$($arg),*| (self.function)($($arg),*)
}
};
(
@fn_method_into
lock_unwrap,
($($arg:ident : $arg_ty:ty),*)
) => {
#[inline]
fn into_fn(self) -> impl FnMut($($arg_ty),*)
{
move |$($arg),*| (self.function.lock())($($arg),*)
}
};
(
@fn_method_into
lock_unwrap,
($($arg:ident : $arg_ty:ty),*) -> $ret:ty
) => {
#[inline]
fn into_fn(self) -> impl FnMut($($arg_ty),*) -> $ret
{
move |$($arg),*| (self.function.lock())($($arg),*)
}
};
(
@fn_method_to
direct,
($($arg:ident : $arg_ty:ty),*)
) => {
#[inline]
fn to_fn(&self) -> impl Fn($($arg_ty),*)
{
let self_fn = self.function.clone();
move |$($arg),*| (self_fn)($($arg),*)
}
};
(
@fn_method_to
direct,
($($arg:ident : $arg_ty:ty),*) -> $ret:ty
) => {
#[inline]
fn to_fn(&self) -> impl Fn($($arg_ty),*) -> $ret
{
let self_fn = self.function.clone();
move |$($arg),*| (self_fn)($($arg),*)
}
};
(
@fn_method_to
lock_unwrap,
($($arg:ident : $arg_ty:ty),*)
) => {
#[inline]
fn to_fn(&self) -> impl FnMut($($arg_ty),*)
{
let self_fn = self.function.clone();
move |$($arg),*| (self_fn.lock())($($arg),*)
}
};
(
@fn_method_to
lock_unwrap,
($($arg:ident : $arg_ty:ty),*) -> $ret:ty
) => {
#[inline]
fn to_fn(&self) -> impl FnMut($($arg_ty),*) -> $ret
{
let self_fn = self.function.clone();
move |$($arg),*| (self_fn.lock())($($arg),*)
}
};
(@make_closure direct, $fn_call:expr, $($arg:ident),*) => {
move |$($arg),*| ($fn_call)($($arg),*)
};
(@make_closure lock_unwrap, $fn_call:expr, $($arg:ident),*) => {
move |$($arg),*| ($fn_call.lock())($($arg),*)
};
(
@impl
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$rc_type:ident,
$once_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
impl_arc_conversions!(
@method_into_box
$arc_type<$($generics),*>, $box_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_into_rc
$arc_type<$($generics),*>, $rc_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
#[inline]
fn into_arc(self) -> $arc_type<$($generics),*>
{
self
}
impl_arc_conversions!(
@fn_method_into
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_into_once
$arc_type<$($generics),*>, $once_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_to_box
$arc_type<$($generics),*>, $box_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_to_rc
$arc_type<$($generics),*>, $rc_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
#[inline]
fn to_arc(&self) -> $arc_type<$($generics),*>
{
self.clone()
}
impl_arc_conversions!(
@fn_method_to
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_to_once
$arc_type<$($generics),*>, $once_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
};
(
@impl_no_once
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$rc_type:ident,
$call_mode:ident,
($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
impl_arc_conversions!(
@method_into_box
$arc_type<$($generics),*>, $box_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_into_rc
$arc_type<$($generics),*>, $rc_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
#[inline]
fn into_arc(self) -> $arc_type<$($generics),*>
{
self
}
impl_arc_conversions!(
@fn_method_into
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_to_box
$arc_type<$($generics),*>, $box_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
impl_arc_conversions!(
@method_to_rc
$arc_type<$($generics),*>, $rc_type,
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
#[inline]
fn to_arc(&self) -> $arc_type<$($generics),*>
{
self.clone()
}
impl_arc_conversions!(
@fn_method_to
$call_mode,
($($arg : $arg_ty),*) $(-> $ret)?
);
};
(
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$rc_type:ident,
$once_type:ident,
Fn($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
impl_arc_conversions!(
@impl
$arc_type<$($generics),*>,
$box_type,
$rc_type,
$once_type,
direct,
($($arg : $arg_ty),*) $(-> $ret)?
);
};
(
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$rc_type:ident,
$once_type:ident,
FnMut($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
impl_arc_conversions!(
@impl
$arc_type<$($generics),*>,
$box_type,
$rc_type,
$once_type,
lock_unwrap,
($($arg : $arg_ty),*) $(-> $ret)?
);
};
(
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$rc_type:ident,
Fn($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
impl_arc_conversions!(
@impl_no_once
$arc_type<$($generics),*>,
$box_type,
$rc_type,
direct,
($($arg : $arg_ty),*) $(-> $ret)?
);
};
(
$arc_type:ident < $($generics:ident),* >,
$box_type:ident,
$rc_type:ident,
FnMut($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
) => {
impl_arc_conversions!(
@impl_no_once
$arc_type<$($generics),*>,
$box_type,
$rc_type,
lock_unwrap,
($($arg : $arg_ty),*) $(-> $ret)?
);
};
}
pub(crate) use impl_arc_conversions;