-
Notifications
You must be signed in to change notification settings - Fork 787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should ml_meth be an union? #2129
Comments
The enum shouldn't be a problem, as long as equal repr is guaranteed, but I the transmute just mirrors what is done on the CPython side, and I don't see how it could be problematic... |
The union looks nicer for avoiding the transmute, sure, yet I'm not sure it really wins us much. CPython essentially does the C equivalent of a transmute with the function pointer in Personally I prefer that the current definition exactly matches the C. If the majority want the union I can be won round. Note that changing it would be a breaking change, so I'd also argue if there's not much clear benefit it's not worth the ecosystem churn. (Not sure how many users are using |
Thanks for the feedback 😃 Let me elaborate some more. What I like about it is that it's safer on the Rust side. For example, consider an user just doing this: let wrapped_sum_as_string = PyMethodDef {
ml_name: "sum_as_string\0".as_ptr() as *const c_char,
ml_meth: transmute(sum_as_string)),
ml_flags: METH_FASTCALL,
ml_doc: "returns the sum of two integers as a string\0".as_ptr() as *const c_char,
}; The benefits of an union here are that:
Currently there is barely any protection against accidentally changing a function to an incompatible type or just getting it wrong in the first place. If we can't even get the function types correct I think we should be helping users get this right.
0.16 will already be a breaking change in this area anyway - this code will stop compiling because of the fix to let wrapped_sum_as_string = PyMethodDef {
ml_name: "sum_as_string\0".as_ptr() as *const c_char,
ml_meth: Some(transmute::<_PyCFunctionFast, PyCFunction>(sum_as_string)),
ml_flags: METH_FASTCALL,
ml_doc: "returns the sum of two integers as a string\0".as_ptr() as *const c_char,
}; |
I would also like to add that this would probably be a net improvement for CPython as well, replacing the infinite set of types one can transmute/cast into by the finite set of types which are part of the union. For us, it could also help immediately available documentation, e.g. the doc comments of the union variants could explain exactly which |
Fair enough, the usability arguments are quite compelling. This wouldn't be the only place where the ffi bindings do have
Agreed, though I suspect that it's far too big a breaking change for CPython to consider doing that upstream. |
Sure 👍 |
I think this can be closed now that #2166 was merged? |
Yep, good catch! |
In #2126 (comment) I mentioned that transmuting function pointers makes me nervous.
The particular code example is this:
Representing the
ml_meth
field with an union should make this less tricky and error-prone:It then becomes:
What do you think? Are there any caveats to this?
The text was updated successfully, but these errors were encountered: