Skip to content

Commit

Permalink
Extend the compact gate implementation to provide index method
Browse files Browse the repository at this point in the history
This method essentially returns the unique index associated with the current gate. The plan is to use that number for metrics uniqueness check: they greatly benefit from knowing that the dimension for compact gate is `u64`
  • Loading branch information
akoshelev committed Oct 25, 2024
1 parent 482c15c commit 6293f7f
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 6293f7f

Please sign in to comment.