I'm trying to implement a ToObject trait for FnMut closures, with trait bounds on the closure's arguments and return values:
pub trait ToObject {
fn to_object(&self) -> crate::Result<Object>;
}
impl<A, R, F> ToObject for F
where
A: LuaPoppable,
R: LuaPushable,
F: FnMut(A) -> crate::Result<R> + 'static,
{
fn to_object(&self) -> crate::Result<Object> {
todo!()
}
}
this won't compile because both A and R are unconstrained by the impl trait, self type or predicates. I can make it work by making the ToObject trait generic:
pub trait ToObject<A, R> {
fn to_object(&self) -> crate::Result<Object>;
}
impl<A, R, F> ToObject<A, R> for F
where
A: LuaPoppable,
R: LuaPushable,
F: FnMut(A) -> crate::Result<R> + 'static,
{
fn to_object(&self) -> crate::Result<Object> {
todo!()
}
}
but this makes it pretty awkard when implementing the ToObject trait for all other types as I have two unused A and B generics:
impl<A, B> ToObject<A, B> for usize {
fn to_object(&self) -> crate::Result<Object> {
todo!()
}
}
Is there a less awkward solution?