Plain function pointer + Send as type or trait bound


#1

I’ve got a function foo() that takes another function func as a parameter. func needs to only handle Send types for safety reasons, and I’d like to restrict func to being a plain function pointer (func: fn() instead of func: F where F: FnOnce() + Send) for performance reasons. Is there some way to specify that a type must be a plain function pointer and only handle Send types internally? E.g.:

fn foo(func: F) where F: fn() + Send {}

#2

Oh, damn. So in the case of F where F: FnOnce() + Send it looks like the Send bound doesn’t require that the body of F only handle Send types, just that data captured by the closure be Send.

So new question: Is is possible to require that a function only handle Send data internally? Is that even a sensible thing to want to do?


#3

What do you mean when you say that the body of F should only handle Send types? You want to say that a non-Send type can’t be created during the execution of the function?

There’s no way to declare something like that, but I also don’t understand how it could be necessary.


#4

Yeah, the function can’t create or interact with !Send data in any way. I’m working on a fibers library, and fibers can voluntarily suspend themselves, and then later they can be resumed on another thread. If the fiber had !Send data on the stack then I’ve effectively smuggled it across thread boundaries. If possible I’d like the compiler to reject this as a compile error, but I don’t know if it’s possible to define an API that can catch such an issue.