Skip to main content

Emit

Struct Emit 

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

The capability to render captured Rust syntax.

Unforgeable outside this crate: the field is private and there is no public constructor, so the only way to hold one is to have been handed one. See the module docs for where that happens and why.

Every method here is a rendering — it answers “what did the source write”, never “what does this mean”. The second question is the model’s, and its answers (TypeRef::kind, TypeRef::key, the layer readings) need no capability precisely because they cannot be misused into re-deriving a classification.

§The seal, as compiled assertions

A doctest builds as its own crate against the published API, so these check the property that matters: what an out-of-crate adapter can reach. Each names a route that used to be open.

An element’s item (E0624 — the method is private):

fn leak(e: &Element) -> syn::Item { e.as_syn() }

A declared type’s item:

fn leak(t: &flat::Type) -> syn::Item { t.as_syn() }

A captured function’s own node, through its Origin:

fn leak(f: &flat::Function) -> &syn::ItemFn { f.origin.as_syn() }

…and its tokens, which re-parse to the same item — the door under another name, and the one a reviewer found still open when this type was introduced. No longer closed: Origin::spell is pub now that the registry pipeline’s own tests, this method’s other legitimate caller, are the separate prebindgen-registry crate rather than code inside this one:

fn leak(f: &flat::Function) -> proc_macro2::TokenStream { f.origin.spell() }

A type’s node — the door C5 claimed to have closed and did not:

fn leak(t: &flat::TypeRef) -> &syn::Type { t.as_syn() }

A declared enum’s item, by name. No longer closed, for the same reason as Origin::spell above — Flat::enum_item is a registry-pipeline test helper:

fn leak(f: &Flat) -> Option<&syn::ItemEnum> { f.enum_item("E") }

The delimiters a shape was written with — S { a } vs S(a) vs S:

fn leak(s: &flat::Struct) -> proc_macro2::TokenStream {
    s.spell(Default::default(), &[])
}
fn leak(v: &flat::EnumValue) -> proc_macro2::TokenStream {
    v.spell(Default::default(), &[])
}

A type’s spelling. No longer closed: TypeRef::spell is pub now that write_rust’s own emission code, this method’s other legitimate caller, lives in the separate prebindgen-registry crate:

fn leak(t: &flat::TypeRef) -> proc_macro2::TokenStream { t.spell() }

…its stripped form, and the kind’s reconstruction:

fn leak(t: &flat::TypeRef) -> syn::Type { t.stripped_syntax() }
fn leak(k: &flat::TypeKind) -> syn::Type { k.to_syn() }

Minting one by naming the struct literal is not available either — the field is private:

let forged = Emit { _seal: () };

Implementations§

Source§

impl Emit

Source

pub fn new() -> Emit

Mint one. Previously pub(crate), the whole enforcement mechanism when the registry pipeline that is this method’s sole legitimate caller lived in this crate; now pub, since that pipeline is the separate prebindgen-registry crate and a module-path seal cannot reach across the boundary. write_rust there mints the one Emit per generation and hands out only borrows of it — see the module doc.

No Default impl on purpose: Emit::default() would be one more trivially-derivable way to mint one, undermining the “only where a capability is deliberately needed” convention new itself relies on now that visibility alone cannot enforce it.

Source

pub fn spell(&self, ty: &TypeRef) -> TokenStream

The type as the source spelled it — what generated Rust must say.

Not a canonical form rebuilt from the classification to check the lowering against: this is the crate’s own tokens, so a generated signature names the type the way the source crate does and compiles in its scope.

Source

pub fn spell_ty(&self, ty: &TypeRef) -> Type

Self::spell as a node, for an emitter that builds a syn::Type around it (*mut #ty, &[#elem]).

A convenience over parse_quote!(#spelled), which is what the call sites wrote before.

Source

pub fn spell_stripped(&self, ty: &TypeRef) -> Type

The type under every transparent wrapper, spelled — Box<Payload>Payload.

The spelling peer of TypeRef::stripped_key, for an emitter that must name what a declaration is about rather than what the use site wrote.

Source

pub fn item(&self, e: &Element) -> Item

A captured item, verbatim — attributes, visibility and body included.

The legitimate reason to reach for an item at all: an emitter re-stating one as written. Reading a fact off an item is a missing accessor, and the model is where it belongs.

Source

pub fn type_item(&self, t: &Type) -> Item

A declared type’s item, verbatim. The Type peer of Self::item.

Source

pub fn verbatim_fn(&self, f: &Function) -> TokenStream

A captured function’s tokens, as written.

One of four per-shape peers of Self::item, for the callback that already holds the specific element rather than an Element. An adapter that re-emits its input unchanged is the whole use — both in-tree adapters build wrappers instead, so this is what a pass-through generator would call.

Source

pub fn verbatim_struct(&self, s: &Struct) -> TokenStream

A captured struct’s tokens, as written. See Self::verbatim_fn.

Source

pub fn verbatim_variant(&self, v: &Variant) -> TokenStream

A captured sum’s tokens, as written. See Self::verbatim_fn.

Source

pub fn verbatim_enum(&self, e: &Enum) -> TokenStream

A captured fieldless enum’s tokens, as written. See Self::verbatim_fn.

Source

pub fn const_alias(&self, c: &Constant, source_module: &Path) -> TokenStream

A constant re-emitted as an alias into source_module, so the initializer is never copied and a const referencing source-crate internals stays valid in the generated file.

Takes the element rather than its item because the alias needs four facts off it and nothing else; handing over the whole syn::ItemConst to read four fields is what an accessor is for.

Source

pub fn const_verbatim(&self, c: &Constant) -> TokenStream

A constant re-emitted verbatim, for an adapter with no source module.

Source

pub fn guard(&self, g: &Guard) -> ItemConst

A Guard’s anonymous const _, as written.

Source

pub fn discriminant(&self, v: &EnumValue) -> Option<TokenStream>

An enum value’s discriminant as written= 0x07 stays 0x07.

None when the source wrote none. Distinct from EnumValue::discriminant, which is the evaluated number and this shape’s identity: a C mirror re-states the spelling, a destination language that transmits a value wants the number.

Source

pub fn shape<S>( &self, s: &S, head: TokenStream, parts: &[TokenStream], ) -> TokenStream
where S: Shaped,

A struct, alternative or enum value spelled with the delimiters the source wroteS { a: x }, S(x), S — for a pattern or a constructor alike.

B and B() are both payload-free and still spelled differently, which is why this is a rendering rather than something kind could answer.

Note what is not here: Field::member and Field::bind stay ungated, because they read the field’s name and index — model facts, no captured syntax.

Source

pub fn member(&self, f: &Field) -> Member

How a field is addressed in a pattern or an initializer — by name when it has one, else by position.

Source

pub fn bind(&self, f: &Field, bind: &impl ToTokens) -> TokenStream

A field bound to bind, shaped for whichever address it uses: id: __f0 for a named field, __f0 for a positional one.

Trait Implementations§

Source§

impl Debug for Emit

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Emit

§

impl RefUnwindSafe for Emit

§

impl Send for Emit

§

impl Sync for Emit

§

impl Unpin for Emit

§

impl UnsafeUnpin for Emit

§

impl UnwindSafe for Emit

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> 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, 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.