diff --git a/ipa-step-derive/src/lib.rs b/ipa-step-derive/src/lib.rs index ea5d19268..2298e3969 100644 --- a/ipa-step-derive/src/lib.rs +++ b/ipa-step-derive/src/lib.rs @@ -165,6 +165,18 @@ fn derive_gate_impl(ast: &DeriveInput) -> TokenStream { ::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, diff --git a/ipa-step-test/src/lib.rs b/ipa-step-test/src/lib.rs index 84eab760d..3789e6fcf 100644 --- a/ipa-step-test/src/lib.rs +++ b/ipa-step-test/src/lib.rs @@ -17,15 +17,21 @@ 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)) @@ -33,6 +39,13 @@ mod tests { .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()