Skip to content

Commit

Permalink
Merge pull request #1373 from akoshelev/step-index
Browse files Browse the repository at this point in the history
Extend the compact gate implementation to provide `index` method
  • Loading branch information
akoshelev authored Oct 25, 2024
2 parents 482c15c + 6293f7f commit 82f08b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ipa-step-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ fn derive_gate_impl(ast: &DeriveInput) -> TokenStream {
<Self as ::std::fmt::Display>::fmt(self, f)
}
}

impl #name {
/// Returns the current index. It matches the index of the latest step
/// this gate has been narrowed to.
///
/// If gate hasn't been narrowed yet, it returns the index of the default value.
#[must_use]
pub fn index(&self) -> ::ipa_step::CompactGateIndex {
self.0
}
}

};

// This environment variable is set by build scripts,
Expand Down
13 changes: 13 additions & 0 deletions ipa-step-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@ mod tests {

#[test]
fn narrows() {
assert_eq!(ComplexGate::default().index(), 0);
assert_eq!(ComplexGate::default().as_ref(), "/");
assert_eq!(
ComplexGate::default().narrow(&ComplexStep::One).as_ref(),
"/one"
);
assert_eq!(ComplexGate::default().narrow(&ComplexStep::One).index(), 1,);
assert_eq!(
ComplexGate::default().narrow(&ComplexStep::Two(2)).as_ref(),
"/two2"
);
assert_eq!(
ComplexGate::default().narrow(&ComplexStep::Two(2)).index(),
10,
);
assert_eq!(
ComplexGate::default()
.narrow(&ComplexStep::Two(2))
.narrow(&BasicStep::One)
.as_ref(),
"/two2/one"
);
assert_eq!(
ComplexGate::default()
.narrow(&ComplexStep::Two(2))
.narrow(&BasicStep::One)
.index(),
11,
);
assert_eq!(
ComplexGate::from("/two2/one"),
ComplexGate::default()
Expand Down

0 comments on commit 82f08b2

Please sign in to comment.