pub struct Source { /* private fields */ }Expand description
Provides access to prebindgen data generated by the #[prebindgen] macro
The Source struct reads prebindgen data files from a directory that was
initialized by init_prebindgen_out_dir in
the source project’s build.rs. This allows destination projects to access
and process the collected FFI interface definitions.
§Workflow
- Source project: Calls
init_prebindgen_out_dir()inbuild.rsand uses#[prebindgen]macro - Destination project: Uses
Source::new()to read the collected data
§Groups
Items are organized into groups based on the first parameter of the #[prebindgen]
macro. For example, #[prebindgen("structs")] creates items in the “structs” group.
Items without an explicit group are placed in the “default” group.
§Example
The PREBINDGEN_OUT_DIR constant is defined in the source FFI crate using the
prebindgen_out_dir!() macro:
// In source_ffi/src/lib.rs
use prebindgen_proc_macro::{prebindgen, prebindgen_out_dir};
pub const PREBINDGEN_OUT_DIR: &str = prebindgen_out_dir!();
#[prebindgen]
pub fn my_function() -> i32 { 42 }Then in the destination project’s build.rs:
let source = prebindgen::Source::new(source_ffi::PREBINDGEN_OUT_DIR);
// Process all items
for (item, location) in source.items_all() {
// Process each syn::Item...
}Implementations§
Source§impl Source
impl Source
Sourcepub fn new<P: AsRef<Path>>(input_dir: P) -> Self
pub fn new<P: AsRef<Path>>(input_dir: P) -> Self
Parameters:
input_dir: Path to the directory containing prebindgen data files
Sourcepub fn crate_name(&self) -> &str
pub fn crate_name(&self) -> &str
Returns the name of the source crate that generated the prebindgen data
This is typically used by FfiConverter to
generate proper function calls to the original crate.
§Example
let source = prebindgen::Source::new(source_ffi::PREBINDGEN_OUT_DIR);
let crate_name = source.crate_name();Sourcepub fn items_in_groups(
&self,
groups: &[&str],
) -> impl Iterator<Item = (Item, SourceLocation)>
pub fn items_in_groups( &self, groups: &[&str], ) -> impl Iterator<Item = (Item, SourceLocation)>
Returns an iterator over items from specific groups
§Parameters
groups- Array of group names to include
§Example
let source = prebindgen::Source::new(source_ffi::PREBINDGEN_OUT_DIR);
// Process only items from "structs" and "functions" groups
let items = source.items_in_groups(&["structs"]).collect::<Vec<_>>();
assert_eq!(items.len(), 1); // only TestStruct should be presentSourcepub fn items_except_groups(
&self,
groups: &[&str],
) -> impl Iterator<Item = (Item, SourceLocation)>
pub fn items_except_groups( &self, groups: &[&str], ) -> impl Iterator<Item = (Item, SourceLocation)>
Returns an iterator over items excluding specific groups
§Parameters
groups- Array of group names to exclude
§Example
let source = prebindgen::Source::new(source_ffi::PREBINDGEN_OUT_DIR);
let items = source.items_except_groups(&["structs"]).collect::<Vec<_>>();
assert_eq!(items.len(), 1); // only test_function should be presentSourcepub fn items_all(&self) -> impl Iterator<Item = (Item, SourceLocation)>
pub fn items_all(&self) -> impl Iterator<Item = (Item, SourceLocation)>
Returns an iterator over all items from all groups
This is the most commonly used method for processing all prebindgen items.
§Example
let source = prebindgen::Source::new(source_ffi::PREBINDGEN_OUT_DIR);
let items: Vec<_> = source.items_all().collect();
assert_eq!(items.len(), 2); // should contain TestStruct and test_function