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
impl Variant
Sourcepub fn type_ref(&self) -> &TypeRef
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.
Trait Implementations§
Auto Trait Implementations§
impl !Send for Variant
impl !Sync for Variant
impl Freeze for Variant
impl RefUnwindSafe for Variant
impl Unpin for Variant
impl UnsafeUnpin for Variant
impl UnwindSafe for Variant
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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