How to call a function just with its type

Basically, I want to call a function with just it's type alone, without a self value. This would make it impossible to call closures that capture values, but I'm okay with that and for my use case, this is preferred. I'd expect some kind of FnZero trait but couldn't find one. I made this as an alternative, but I feel like there should be a simpler way: Rust Playground

Function Pointer

This isn't useful here, as the fn pointer is still a value - I want to encode the information using types alone.

I'm not sure I understand what you really want. But what is wrong with the usual:

struct Thing {}

impl Thing {
    // Look: No 'self' here.
    fn function(a: T, b:U) {
        ...
        ...
    }
}
...
...
    // Look: No 'self' here.
    Thing::function(a, b);

Calling a function with just a type and no self.
The function that actually gets called depends on the type you have. It's encoded in the type.

What am I missing here?

2 Likes

I believe your alternative is actually unsound: if I have a type enum Unconstructable {} and implement Fn() for it, this API will allow me to write Unconstructable::call(), which will run code that should never be run.

1 Like