use ast::Ast;
use std::convert::TryInto;
use std::ffi::CStr;
use std::fmt;
use z3_sys::*;
use Context;
use Pattern;
impl<'ctx> Pattern<'ctx> {
pub fn new(ctx: &'ctx Context, terms: &[&dyn Ast]) -> Pattern<'ctx> {
assert!(!terms.is_empty());
assert!(terms.iter().all(|t| t.get_ctx().z3_ctx == ctx.z3_ctx));
let terms: Vec<_> = terms.iter().map(|t| t.get_z3_ast()).collect();
Pattern {
ctx,
z3_pattern: unsafe {
let p = Z3_mk_pattern(
ctx.z3_ctx,
terms.len().try_into().unwrap(),
terms.as_ptr() as *const Z3_ast,
);
Z3_inc_ref(ctx.z3_ctx, p as Z3_ast);
p
},
}
}
}
impl<'ctx> fmt::Debug for Pattern<'ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let p = unsafe { Z3_pattern_to_string(self.ctx.z3_ctx, self.z3_pattern) };
if p.is_null() {
return Result::Err(fmt::Error);
}
match unsafe { CStr::from_ptr(p) }.to_str() {
Ok(s) => write!(f, "{}", s),
Err(_) => Result::Err(fmt::Error),
}
}
}
impl<'ctx> fmt::Display for Pattern<'ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
<Self as fmt::Debug>::fmt(self, f)
}
}
impl<'ctx> Drop for Pattern<'ctx> {
fn drop(&mut self) {
unsafe {
Z3_dec_ref(self.ctx.z3_ctx, self.z3_pattern as Z3_ast);
}
}
}