-
Notifications
You must be signed in to change notification settings - Fork 33
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
Feature/multiple return values #90
base: master
Are you sure you want to change the base?
Feature/multiple return values #90
Conversation
Probably need to remove references to Function which are not inside of CType, but it's not clear if this is possible
The function info is not picking up the new function's parameters yet
Sorry for delay, few days off, will look into this once back. |
Alright, I managed to have a quick look, some thoughts:
About your comments:
Yeah ... it's a bit of a mess. Ideally you have
Many attributes (should be all, but I might not have come around) should have a |
DO NOT MERGE THIS
This is a first exploration on the discussion here. I have not made much attempt at making the code elegant at this point.
I have added a new pattern, the
AugmentedFunction
. Currently, this pattern can be used to return multiple return values, but theoretically it could be updated to also handle fallible functions. The goal would be to eventually add something similar for the service pattern, but I have left this as out of scope for the current exploration.I have not attempted to write the back-end writers for cpython and C# yet, as I'm not experienced with creating bindings there. I have for now only shown how the C back-end would be modified.
I am having some difficulty understanding how to run all the tests. For now, I have done some basic testing inside of the
hello_world
example by callingcargo test
there, and inspecting the generated C header file that gets generated.I've been using
cargo expand
inside of thehello_world
example to inspect what the proc macros are generating. In a nutshell, the idea is that clients write:This function will then get renamed by the macro to
my_function3_interoptopus_internal
. The macro will also generate another new function with the original namemy_function3
that contains OUT parameters. The output of of this macro, obtained bycargo expand
, for this example is currently:For now I have not attempted to add user-defined names for the OUT parameters. This could be done by adding arguments to the
ffi_function
attribute macro, but has been left as an open point for now.The header file that is generated contains the following:
The function
my_function3_interoptopus_internal
is not exposed in the header function.The idea is that the backend writers realise that they are dealing with an
AugmentedFunction
and translate the OUT parameters into something that is idiomatic for them. For instance, Python could return a tuple directly.Note that, since the macro works with tokens and not type information, the user cannot write something like
The macro needs to see the tuple in the token stream in order to realise that this is an augmented function with multiple return values.
At this stage I am not 100% sure whether the
AugmentedFunction
pattern has been hooked up properly to the writers. The function is still exposed via thefunction!
macro. I'm not sure if this function will have to be edited, or whether the definition ofFunctionInfo
should be changed such thatAugmentedFunction
will be received by the backend writers.Finally, fallible functions could be added to this
AugmentedFunction
by adding another OUT parameter representing the error type. The function would then return an enum for the success status of the function call.