Struct Source

Source
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

  1. Source project: Calls init_prebindgen_out_dir() in build.rs and uses #[prebindgen] macro
  2. 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

Source

pub fn new<P: AsRef<Path>>(input_dir: P) -> Self

Parameters:

  • input_dir: Path to the directory containing prebindgen data files
Source

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();
Source

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 present
Source

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 present
Source

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

Auto Trait Implementations§

§

impl Freeze for Source

§

impl RefUnwindSafe for Source

§

impl !Send for Source

§

impl !Sync for Source

§

impl Unpin for Source

§

impl UnwindSafe for Source

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.