Implement Trait for a specific function

Hello,

I wanted to know if I could implement a Trait for a specific function (not a signature, as here)?
E.g., I would like to be able to define a derivative for an existing function so that I can later call function.diff to produce the derivative.

fn affine(x: isize) -> isize {
    x * 4 + 2
}

pub trait Differentiable<Args> {
    type Output;
    fn diff(&self, args: Args) -> Self::Output;
}

impl Differentiable<()> for affine { // expected type, found function `crate::affine`
    type Output = isize;
    fn diff(&self, _args: (isize,)) -> Self::Output {
        4
    }
}

pub fn main() {
    affine.diff((4,));
}

If Rust automatically implements Fn trait on closures / functions, shouldn't there be some hidden type for each closure / function?

It's true that the function has its own type, but its not possible to implement traits for it.

Ok, thanks for the fast reply!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.