Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust lifetimes on ModelIter to make them more permissive #324

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))]
);
}
Loading