Skip to content
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

What is a good strategy to extract information out of ParseCallbacks? #2147

Closed
N3xed opened this issue Dec 30, 2021 · 4 comments · Fixed by #2317
Closed

What is a good strategy to extract information out of ParseCallbacks? #2147

N3xed opened this issue Dec 30, 2021 · 4 comments · Fixed by #2317

Comments

@N3xed
Copy link

N3xed commented Dec 30, 2021

I'd like to extract information from ParseCallbacks so that it can be used later in the build script.

For example:

I have three C macros:

  • VERSION_MAJOR
  • VERSION_MINOR
  • VERSION_PATCH

which all define a number and so get forwarded to ParseCallbacks::int_macro. Now in my implementation of that callback, I want to capture these integers for use later in the build script.

Currently, as I see it, the only option is to use Atomic* or Mutex as the bound on ParseCallbacks specifies UnwindSafe which references to types with interior mutability are not, and the callbacks themselves only get a shared reference to self.
This works but is not pretty and may incur performance penalties for Mutex.

So my questions are:

  • What is the preferred strategy for doing something like that?
    (More so with a less trivial example that requires more complex data e.g. ParseCallbacks::func_macro)
  • Why do these functions not get a mutable reference to self if the callbacks don't need to be Send/Sync?
@emilio
Copy link
Contributor

emilio commented Jan 29, 2022

Generally interior mutability is the way we've been doing this, yeah. So a RefCell or so perhaps.

The callbacks don't take mutable references because the BindgenContext they are stored in is immutable as well. We could internally use RefCell<Box<ParseCallbacks>> or so to allow that and move the burden to bindgen.

@N3xed
Copy link
Author

N3xed commented Jan 29, 2022

Thanks for the response.

It seems like I missed RefCell (I only tried Cell and because that's not UnwindSafe thought that all *Cell types wouldn't work).

We could internally use RefCell<Box<ParseCallbacks>> or so to allow that and move the burden to bindgen.

That would be nice.

Until then I'll make a PR to mention this in the docs (if that's okay), because I didn't find this mentioned even though ParseCallbacks::func_macro exists only to extract information out of bindgen.

@emilio
Copy link
Contributor

emilio commented Feb 4, 2022

Yeah, a PR mentioning that seems great.

pvdrz added a commit to ferrous-systems/rust-bindgen that referenced this issue Oct 21, 2022
One of the advantages of doing this is that `ParseCallbacks` no longer
needs to implement `UnwindSafe` which means that users can rely on
`RefCell` and `Cell` to extract information from the callbacks.

Users relying on `catch_unwind` can still achieve similar behavior using
`std::thread::spawn`.

Fixes rust-lang#2147.
@pvdrz
Copy link
Contributor

pvdrz commented Oct 21, 2022

You cannot use RefCell either because it contains a Cell<isize> field. and as you already mentioned Cell does not implement RefUnwindSafe.

I opened #2317 which removes this restriction and uses panic hooks in the CLI. I'm not sure if anyone relies on catch_unwind to run bindgen and do something else if it panics but they should be able to achieve the same using threads anyway iiuc.

emilio pushed a commit that referenced this issue Oct 22, 2022
One of the advantages of doing this is that `ParseCallbacks` no longer
needs to implement `UnwindSafe` which means that users can rely on
`RefCell` and `Cell` to extract information from the callbacks.

Users relying on `catch_unwind` can still achieve similar behavior using
`std::thread::spawn`.

Fixes #2147.
qsdrqs pushed a commit to qsdrqs/rust-bindgen that referenced this issue Oct 26, 2022
One of the advantages of doing this is that `ParseCallbacks` no longer
needs to implement `UnwindSafe` which means that users can rely on
`RefCell` and `Cell` to extract information from the callbacks.

Users relying on `catch_unwind` can still achieve similar behavior using
`std::thread::spawn`.

Fixes rust-lang#2147.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants