Skip to main content

Variant

Struct Variant 

Source
pub struct Variant {
    pub name: Ident,
    pub alternatives: Vec<Alternative>,
    pub origin: Origin<ItemEnum>,
    /* private fields */
}
Expand description

A #[prebindgen] enum whose alternatives carry payloads — a sum type.

Distinct from Enum, which is the fieldless shape, because the two are consumed as different constructs and numbered differently. A sum’s alternatives are identified by position: the mirror an adapter builds carries no repr and numbers its own arms, so a Rust discriminant would be the wrong answer here — which is why there is no slot for one.

Both shapes are spelled enum in Rust and both keep a syn::ItemEnum in their origin. Which one an item is is the classification, and it is decided once: any alternative with a field makes it a Variant.

Fields§

§name: Ident§alternatives: Vec<Alternative>

Alternatives in declaration order; alternatives[i].index == i.

§origin: Origin<ItemEnum>

Implementations§

Source§

impl Variant

Source

pub fn type_ref(&self) -> &TypeRef

A reference to this sum as a type — what a consumer needs when it has to name the sum rather than walk it (jnigen’s SumTag selector, which carries which sum it chooses between).

The declaration answers, so no consumer has to mint a reading from the name and hope it matches what the model would have said.

This returns state the parser took, not a fresh composition, and the difference is the difference between sealing and appearing to.

A version that composed TypeRef::named(&self.name) would hand a Variant assembled with the name String a Named over the spelling String — which the model reads as Str. That is the kind/syntax disagreement TypeRef’s private fields exist to prevent, and it was reachable from outside the crate while being invisible to every doctest there, because assembling the element is not minting the type.

Reading a stored value closes it: whatever a caller does with the other fields, the reading here is the one the model made, and no caller can mint a different one to put in its place.

The Variant is sealed as well — its reading field is pub(super) — so the two cannot even be paired inconsistently:

let assembled = Variant {
    name: syn::parse_str("String").unwrap(),
    alternatives: vec![],
    origin: Origin::new(
        syn::parse_str("enum String { A(u8) }").unwrap(),
        std::rc::Rc::new(Default::default()),
    ),
};
let mismatched = assembled.type_ref();

That doctest pins “a consumer cannot assemble a Variant and nothing finer: measured, it still fails with the field made pub — as E0063 (missing field) rather than E0451 (private field), since a consumer cannot produce a TypeRef to supply either way. The visibility itself is the check the compiler runs on every build.

Source§

impl Variant

Source

pub fn docs(&self) -> Option<String>

This item’s /// documentation.

Trait Implementations§

Source§

impl Clone for Variant

Source§

fn clone(&self) -> Variant

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 Variant

Source§

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

Formats the value using the given formatter. 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.