Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
add a C compatible interface #43
add a C compatible interface #43
Changes from 5 commits
a8717fa
e867b16
04735cb
0956964
6f8f8e9
690d3d7
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rageagainsthepc can you explain why you had to introduce a new API here, it's hard to follow for me ?
I would like to avoid changing the core of the Rust library just to provide a C compatibility layer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if we initialize the driver with None, we don't know the type of the driver in advance. But we need to know since we are casting back and forth between void* and the actual type of the driver. As far as I know it's not possible to cast directly from void* to a trait type. There are probably different ways to approach this, but this one seemed pretty straight forward to me. I'm open to alternatives though ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the issue was that pointers to traits are fat pointers. Maybe there is way to pass fat pointers to C code and cast them back to traits afterwards. I'll look into that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There exists a std::raw::TraitObject but it's marked as unstable because its representation may change in the future rust-lang/rust#27751 . Also cbindgen reports TraitObject types as unsupported. In theory we could split the fat pointer into two separate pointers, store them in a struct and transmute the memory back to an Introspectable, but imho that sounds super ugly and unstable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the other hand, we could just loose the support for passing
None
instead of the DriverType, at least for the C-API ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've found the crate downcast-rs. With this crate you are able to check a trait object for its concrete type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, thanks for the update.
I'm okay to merge this as long as you add a comment that this API has been introduced for the solve purpose of the C interoperability, and that it's a workaround, to be deprecated in the future when new better solutions will emerge to deal with this, either from Rust unstable, or simply by switching to dynamic library loading.