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 ParseCallbacks handler for generated TokenStreams #1638
Conversation
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.
So this means that every time we update proc_macro2 we need to do a breaking bump which is not great...
And more generally that all the code generated by bindgen is part of its public API, which I don't think it's great either.
Could you elaborate on the use cases for this?
My use case is to further process the output. extern "C" {
pub fn foo();
} i need: pub extern "C" fn foo() {
// generated implementation
} Currently i send the bindgen output to Essentially, i think the TokenStream is a better representation for what bindgen is actually producing: a syntax tree. Regarding the dependency on proc_macro2: |
It is not bad as in "common" or such, but it's very easy to forget about... At the very least we need to add a comment in Right now bindgen necessarily needs to be updated in order to update proc_macro2 and related crates of course, but if bindgen updates correctly there's no need to change bindgen's output, you just change how you use proc_macro2... A bit more concerning, I think, is that any change to bindgen's output becomes breaking. Though I guess that is kind of the case already, to some extent... Anyhow I see the use case, though I'm not quite convinced that exposing the whole syntax tree bindgen generates and proc_macro2's token tree is the right level of abstraction... At the very least, can we just expose one token tree? Right after we call the function, we just call In any case, this is just a bit more convenient way to do this than passing a |
…String) before writing/formatting
Ok i changed the PR a bit, now the callback gets invoked in the write method (after TokenStream::to_string() and before rustfmt). This is how i use it: extern crate bindgen;
extern crate syn;
extern crate quote;
use bindgen;
use syn;
use syn::visit_mut::VisitMut;
use quote::ToTokens;
#[derive(Debug)]
struct MyCallbacks;
impl bindgen::callbacks::ParseCallbacks for MyCallbacks {
fn bindings(&self, bindings: &str) -> Option<String> {
let mut file = syn::parse_file(bindings)
.expect("Unable to parse bindgen output");
let mut converter = Converter::new();
converter.visit_file_mut(&mut file);
Some(file.into_token_stream().to_string())
}
} |
My point is, why is this different to passing a string to |
☔ The latest upstream changes (presumably 49af9b7) made this pull request unmergeable. Please resolve the merge conflicts. |
@jandob @emilio I was doing some triaging and wanted to drop my 2 cents about the topic:
An alternative would be exposing However, I don't think exposing the code as a token stream instead of a regular string would be a great improvement as |
This small feature enables advances users to modify the generated code before it gets converted to a string representation.