Skip to main content

FlatBuilder

Struct FlatBuilder 

Source
pub struct FlatBuilder { /* private fields */ }
Expand description

Collects what to parse, then hands over the model.

Carries no configuration about what the language accepts — that is a property of the language, not of the call site. What it carries is what to parse: feed the inputs, then build once.

§Reading a source directory

A build script’s whole job, in one expression — the Source step included. Pass <source_crate>::PREBINDGEN_OUT_DIR:

use prebindgen_flat::Flat;

let flat = Flat::builder().source("source_ffi").build()?;
assert!(flat.function("test_function").is_some());
assert!(flat.declared_type("TestStruct").is_some());

§Reading a stream

Self::items takes any (syn::Item, SourceLocation) iterator, so everything a Source can express still composes — a group selection, a renamed dependency, several sources at once. The feeders accumulate, so mix them freely:

use prebindgen::Source;
use prebindgen_flat::Flat;

// A dependency renamed in Cargo.toml needs the name THIS crate uses, so it
// is configured rather than named by directory.
let helpers = Source::builder("source_ffi").crate_name("helpers").build();
let flat = Flat::builder()
    .items(helpers.items_in_groups(&["functions"]))
    .build()?;
assert_eq!(flat.functions().count(), 1);

§Why accumulate, rather than parse each input

The rules that make a parse fail are whole-stream rules: one flat namespace across every ingested crate, one const index an array length may reach into, one set of source modules to normalize paths against, and every type reference resolving against every declaration. None can be decided per input, so every input is in hand before any of it is classified.

Implementations§

Source§

impl FlatBuilder

Source

pub fn source<P: AsRef<Path>>(self, dir: P) -> Self

Every #[prebindgen] item captured in dir.

Sugar for Self::items over Source::items_all, which is the whole of what a build script normally needs — pass <source_crate>::PREBINDGEN_OUT_DIR. Reach for a Source directly, and feed it through Self::items, only when it needs configuring.

Panics the way Source::new does if dir is not readable prebindgen output: a build script has nothing to recover with.

use prebindgen_flat::Flat;

let flat = Flat::builder().source("source_ffi").build()?;
assert!(flat.function("test_function").is_some());
assert!(flat.declared_type("TestStruct").is_some());
Source

pub fn source_named<P: AsRef<Path>>( self, dir: P, crate_name: impl Into<String>, ) -> Self

The same, for a dependency this crate renames in Cargo.toml.

The origin recorded at capture time is the dependency’s real package name, which will not resolve from a crate that refers to it by another name. crate_name is the name this crate uses.

Per directory, deliberately: an override on the whole parse could only fix one module, and a flat API may layer several sources.

Source

pub fn items<I>(self, items: I) -> Self
where I: IntoIterator<Item = (Item, SourceLocation)>,

Add a captured item stream.

The general feeder: any (syn::Item, SourceLocation) iterator, so item-level selection and multi-source composition stay upstream where they already are. Call it as often as needed; the streams accumulate.

use prebindgen::Source;
use prebindgen_flat::Flat;

let source = Source::new("source_ffi");
let flat = Flat::builder()
    .items(source.items_in_groups(&["structs"]))
    .build()?;
assert_eq!(flat.types().count(), 1);
Source

pub fn build(self) -> Result<Flat, ParseError>

Parse everything collected so far into the model.

Transactional: an Err yields no model at all, so a refused stream cannot leave a half-built one behind.

Order-independent: source modules are gathered, consts indexed, and every item lowered before any reference is resolved — so a type reference, an array length and a cross-source mention may each name something declared later, in this input or another.

Trait Implementations§

Source§

impl Clone for FlatBuilder

Source§

fn clone(&self) -> FlatBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FlatBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FlatBuilder

Source§

fn default() -> FlatBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.