Skip to content

Commit

Permalink
Adjust lifetimes on ModelIter to make them more permissive (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgpthomas authored Nov 26, 2024
1 parent cb10013 commit eb5796f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
12 changes: 6 additions & 6 deletions z3/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'ctx> Model<'ctx> {
}
}

pub fn iter(&'ctx self) -> ModelIter<'ctx> {
pub fn iter<'a>(&'a self) -> ModelIter<'a, 'ctx> {
self.into_iter()
}
}
Expand Down Expand Up @@ -162,15 +162,15 @@ impl<'ctx> Drop for Model<'ctx> {

#[derive(Debug)]
/// <https://z3prover.github.io/api/html/classz3py_1_1_model_ref.html#a7890b7c9bc70cf2a26a343c22d2c8367>
pub struct ModelIter<'ctx> {
model: &'ctx Model<'ctx>,
pub struct ModelIter<'a, 'ctx> {
model: &'a Model<'ctx>,
idx: u32,
len: u32,
}

impl<'ctx> IntoIterator for &'ctx Model<'ctx> {
impl<'a, 'ctx> IntoIterator for &'a Model<'ctx> {
type Item = FuncDecl<'ctx>;
type IntoIter = ModelIter<'ctx>;
type IntoIter = ModelIter<'a, 'ctx>;

fn into_iter(self) -> Self::IntoIter {
ModelIter {
Expand All @@ -181,7 +181,7 @@ impl<'ctx> IntoIterator for &'ctx Model<'ctx> {
}
}

impl<'ctx> Iterator for ModelIter<'ctx> {
impl<'a, 'ctx> Iterator for ModelIter<'a, 'ctx> {
type Item = FuncDecl<'ctx>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
29 changes: 29 additions & 0 deletions z3/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1973,3 +1973,32 @@ fn test_atleast() {
assert!(matches!(solver.check(), SatResult::Unsat));
solver.pop(1);
}

#[test]
fn test_model_iter() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);

let a = ast::Int::new_const(&ctx, "a");
let two = ast::Int::from_u64(&ctx, 2);
solver.assert(&a._eq(&two));
solver.check();

let model = solver.get_model().unwrap();

// consume a model and return some asts constructed from it
// this didn't compile before pr#324
fn consume_model(model: Model) -> Vec<ast::Dynamic> {
let mut asts = vec![];
for func in &model {
asts.push(func.apply(&[]));
}
asts
}

assert_eq!(
consume_model(model),
vec![ast::Dynamic::new_const(&ctx, "a", &Sort::int(&ctx))]
);
}

0 comments on commit eb5796f

Please sign in to comment.