1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

extern crate libc;
extern crate clingo_sys;

use libc::c_int;
use libc::c_char;
use std::ffi::CStr;
use std::ffi::CString;

use clingo_sys::*;
pub use clingo_sys::{clingo_propagator, clingo_solve_event_callback_t, clingo_solve_event_type_t,
                     clingo_solve_handle_t, clingo_solve_result_bitset_t,
                     clingo_solve_mode_bitset_t, clingo_solve_mode, clingo_show_type_bitset_t,
                     clingo_show_type, clingo_logger_t, clingo_literal_t, clingo_id_t};


pub fn safe_clingo_version() -> (i32, i32, i32) {
    let mut major = 0;
    let mut minor = 0;
    let mut revision = 0;
    unsafe { clingo_version(&mut major, &mut minor, &mut revision) };

    (major, minor, revision)
}
pub struct ClingoPart<'a> {
    pub name: CString,
    pub params: &'a [clingo_symbol_t],
}
fn from_clingo_part(spart: &ClingoPart) -> clingo_part {
    clingo_part {
        name: spart.name.as_ptr(),
        params: spart.params.as_ptr(),
        size: spart.params.len(),
    }
}
pub fn safe_clingo_error_code() -> clingo_error_t {
    unsafe { clingo_error_code() }
}
pub fn safe_clingo_error_message() -> &'static str {

    let c_buf: *const c_char = unsafe { clingo_error_message() };
    if c_buf.is_null() {
        return "";
    } else {
        let c_str = unsafe { CStr::from_ptr(c_buf) };
        return c_str.to_str().unwrap();
    }
}
pub fn safe_clingo_set_error(code: clingo_error_t, message: &str) {

    let m2 = CString::new(message).unwrap().as_ptr();
    unsafe {
        clingo_set_error(code, m2);
    }
}
pub fn safe_clingo_symbol_to_string(symbol: clingo_symbol_t) -> std::option::Option<CString> {

    let mut size: usize = 0;
    let err = unsafe { clingo_symbol_to_string_size(symbol, &mut size) };
    if !err {
        None
    } else {
        let a1 = vec![1; size];
        let string = unsafe { CString::from_vec_unchecked(a1) };
        let err = unsafe { clingo_symbol_to_string(symbol, string.as_ptr() as *mut c_char, size) };
        if !err {
            None
        } else {
            Some(string)
        }
    }

}
pub fn safe_clingo_symbol_create_number(number: c_int) -> clingo_symbol_t {

    let mut symbol = 0 as clingo_symbol_t;
    unsafe {
        clingo_symbol_create_number(number, &mut symbol);
    }
    symbol
}
pub fn safe_clingo_symbol_create_id(name: &str,
                                    positive: bool)
                                    -> std::option::Option<clingo_symbol_t> {

    let mut symbol = 0 as clingo_symbol_t;
    let err = unsafe {
        clingo_symbol_create_id(CString::new(name).unwrap().as_ptr(), positive, &mut symbol)
    };
    if !err {
        None
    } else {
        Some(symbol)
    }
}
pub fn safe_clingo_symbol_create_function(name: &str,
                                          arguments: &[clingo_symbol_t],
                                          positive: bool)
                                          -> std::option::Option<clingo_symbol_t> {

    let mut symbol = 0 as clingo_symbol_t;
    let err = unsafe {
        clingo_symbol_create_function(CString::new(name).unwrap().as_ptr(),
                                      arguments.as_ptr(),
                                      arguments.len(),
                                      positive,
                                      &mut symbol)
    };
    if !err {
        None
    } else {
        Some(symbol)
    }
}
pub fn safe_clingo_symbol_number(symbol: clingo_symbol_t) -> Option<c_int> {

    let mut number = 0;
    let err = unsafe { clingo_symbol_number(symbol, &mut number) };
    if !err {
        None
    } else {
        Some(number)
    }
}
pub fn safe_clingo_symbol_hash(symbol: clingo_symbol_t) -> usize {
    unsafe { clingo_symbol_hash(symbol) }
}
pub fn safe_clingo_symbol_arguments(symbol: clingo_symbol_t)
                                    -> std::option::Option<Vec<clingo_symbol_t>> {

    let mut a_ptr = std::ptr::null() as *const clingo_symbol_t;
    let mut size: usize = 0;
    let err = unsafe { clingo_symbol_arguments(symbol, &mut a_ptr, &mut size) };
    if !err {
        None
    } else {
        let mut a1 = Vec::<clingo_symbol_t>::with_capacity(size);
        for _ in 0..size {
            let nsymbol = unsafe { *a_ptr };
            a1.push(nsymbol);
        }
        Some(a1)
    }

}
pub fn safe_clingo_symbol_is_equal_to(a: clingo_symbol_t, b: clingo_symbol_t) -> bool {
    unsafe { clingo_symbol_is_equal_to(a, b) }
}
pub fn safe_clingo_symbol_is_less_than(a: clingo_symbol_t, b: clingo_symbol_t) -> bool {
    unsafe { clingo_symbol_is_less_than(a, b) }
}
pub fn new_clingo_control<'a>(arguments: std::env::Args,
                              logger: clingo_logger_t,
                              logger_data: *mut ::std::os::raw::c_void,
                              message_limit: ::std::os::raw::c_uint)
                              -> std::option::Option<&'a mut ClingoControl> {
    let arguments_size = arguments.len() - 1;
    // create a vector of zero terminated strings
    let args = arguments.map(|arg| CString::new(arg).unwrap()).collect::<Vec<CString>>();
    // drop first element
    let (_, tail) = args.split_first().unwrap();
    // convert the strings to raw pointers
    let c_args = tail.iter().map(|arg| arg.as_ptr()).collect::<Vec<*const c_char>>();

    let mut ctl = std::ptr::null_mut();

    let err = unsafe {
        clingo_control_new(c_args.as_ptr(),
                           arguments_size,
                           logger,
                           logger_data,
                           message_limit,
                           &mut ctl)
    };
    if !err {
        None
    } else {
        unsafe { Some(&mut *(ctl as *mut ClingoControl)) }
    }

}

pub struct ClingoControl(clingo_control_t);
impl Drop for ClingoControl {
    fn drop(&mut self) {
        unsafe {
            let ClingoControl(ref mut control) = *self;
            clingo_control_free(control)
        }

    }
}
impl ClingoControl {
    //     pub fn clingo_control_load(control: *mut ClingoControl, file: *const c_char) -> u8;
    pub fn add(&mut self, name: &str, parameters: Vec<&str>, program: &str) -> bool {

        let mname = CString::new(name).unwrap();
        let mprogram = CString::new(program).unwrap();
        let parameters_size = parameters.len();
        // create a vector of zero terminated strings
        let args =
            parameters.into_iter().map(|arg| CString::new(arg).unwrap()).collect::<Vec<CString>>();
        // convert the strings to raw pointers
        let c_args = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<*const c_char>>();

        unsafe {
            let ClingoControl(ref mut control) = *self;
            clingo_control_add(control,
                               mname.as_ptr(),
                               c_args.as_ptr(),
                               parameters_size,
                               mprogram.as_ptr())
        }
    }
    pub fn ground(&mut self,
                  sparts: Vec<ClingoPart>,
                  ground_callback: clingo_ground_callback_t,
                  ground_callback_data: *mut ::std::os::raw::c_void)
                  -> bool {

        let parts = sparts.iter().map(|arg| from_clingo_part(arg)).collect::<Vec<clingo_part>>();
        let parts_size = parts.len();

        unsafe {
            let ClingoControl(ref mut control) = *self;
            clingo_control_ground(control,
                                  parts.as_ptr(),
                                  parts_size,
                                  ground_callback,
                                  ground_callback_data)
        }
    }
    pub fn solve(&mut self,
                 mode: clingo_solve_mode_bitset_t,
                 assumptions: Vec<clingo_symbolic_literal_t>,
                 notify: clingo_solve_event_callback_t,
                 data: *mut ::std::os::raw::c_void)
                 -> Option<&mut ClingoSolveHandle> {
        unsafe {
            let ClingoControl(ref mut control) = *self;

            let mut handle = std::ptr::null_mut() as *mut clingo_solve_handle_t;
            let err = clingo_control_solve(control,
                                           mode,
                                           assumptions.as_ptr(),
                                           assumptions.len(),
                                           notify,
                                           data,
                                           &mut handle);
            if !err {
                None
            } else {
                Some(&mut *(handle as *mut ClingoSolveHandle))
            }

        }
    }

    //     pub fn clingo_control_cleanup(control: *mut ClingoControl) -> u8;
    //     pub fn clingo_control_assign_external(control: *mut ClingoControl,
    //                                           atom: clingo_symbol_t,
    //                                           value: clingo_truth_value_t)
    //                                           -> u8;
    //     pub fn clingo_control_release_external(control: *mut ClingoControl,
    //                                            atom: clingo_symbol_t)
    //                                            -> u8;
    
    pub fn register_propagator(&mut self,
        propagator: *const clingo_propagator_t, data: *mut ::std::os::raw::c_void,
                                                  sequential: bool)
                                                  -> bool {
        let ClingoControl(ref mut control) = *self;
        unsafe { clingo_control_register_propagator(control,
                                                    propagator,
                                                    data,
                                                    sequential)}
    }
    pub fn statistics(&mut self) -> std::option::Option<&mut ClingoStatistics> {
        unsafe {
            let ClingoControl(ref mut control) = *self;
            let mut stat = std::ptr::null_mut() as *mut clingo_statistics_t;
            let err = clingo_control_statistics(control, &mut stat);
            if !err {
                None
            } else {
                Some(&mut *(stat as *mut ClingoStatistics))
            }
        }
    }
    //     pub fn clingo_control_interrupt(control: *mut ClingoControl);
    pub fn configuration(&mut self) -> std::option::Option<&mut ClingoConfiguration> {
        unsafe {
            let ClingoControl(ref mut control) = *self;
            let mut conf = std::ptr::null_mut() as *mut clingo_configuration_t;
            let err = clingo_control_configuration(control, &mut conf);
            if !err {
                None
            } else {
                Some(&mut *(conf as *mut ClingoConfiguration))
            }
        }
    }
    //     pub fn clingo_control_use_enumeration_assumption(control: *mut ClingoControl,
    //                                                      enable: u8)
    //                                                     -> u8;
    //     pub fn clingo_control_get_const(control: *mut ClingoControl,
    //                                     name: *const c_char,
    //                                     symbol: *mut clingo_symbol_t)
    //                                    -> u8;
    //     pub fn clingo_control_has_const(control: *mut ClingoControl,
    //                                     name: *const c_char,
    //                                     exists: *mut u8)
    //                                    -> u8;
    pub fn symbolic_atoms(&mut self) -> std::option::Option<&mut ClingoSymbolicAtoms> {
        unsafe {
            let ClingoControl(ref mut control) = *self;
            let mut atoms = std::ptr::null_mut() as *mut clingo_symbolic_atoms_t;
            let err = clingo_control_symbolic_atoms(control, &mut atoms);
            if !err {
                None
            } else {
                Some(&mut *(atoms as *mut ClingoSymbolicAtoms))
            }
        }
    }
    pub fn theory_atoms(&mut self) -> std::option::Option<&mut ClingoTheoryAtoms> {
        unsafe {
            let ClingoControl(ref mut control) = *self;
            let mut atoms = std::ptr::null_mut() as *mut clingo_theory_atoms_t;
            let err = clingo_control_theory_atoms(control, &mut atoms);
            if !err {
                None
            } else {
                Some(&mut *(atoms as *mut ClingoTheoryAtoms))
            }
        }
    }
    pub fn backend(&mut self) -> std::option::Option<&mut ClingoBackend> {
        unsafe {
            let ClingoControl(ref mut control) = *self;
            let mut backend = std::ptr::null_mut() as *mut clingo_backend_t;
            let err = clingo_control_backend(control, &mut backend);
            if !err {
                None
            } else {
                Some(&mut *(backend as *mut ClingoBackend))
            }
        }
    }
    //     pub fn clingo_control_program_builder(control: *mut ClingoControl,
    //                                           builder: *mut *mut clingo_program_builder_t)
    //                                          -> u8;
}


pub struct ClingoConfiguration(clingo_configuration_t);
impl ClingoConfiguration {
    pub fn configuration_root(&mut self) -> std::option::Option<clingo_id_t> {
        unsafe {
            let ClingoConfiguration(ref mut conf) = *self;
            let mut root_key = 0 as clingo_id_t;
            let err = clingo_configuration_root(conf, &mut root_key);
            if !err {
                None
            } else {
                Some(root_key)
            }
        }
    }
    //     pub fn clingo_configuration_type(configuration: *mut ClingoConfiguration,
    //                                      key: clingo_id_t,
    //                                      type_: *mut clingo_configuration_type_bitset_t)
    //                                      -> u8;
    //     pub fn clingo_configuration_description(configuration: *mut ClingoConfiguration,
    //                                             key: clingo_id_t,
    //                                             description: *mut *const c_char)
    //                                             -> u8;
    //     pub fn clingo_configuration_array_size(configuration: *mut ClingoConfiguration,
    //                                            key: clingo_id_t,
    //                                            size: *mut size_t)
    //                                            -> u8;
    pub fn configuration_array_at(&mut self,
                                  key: clingo_id_t,
                                  offset: usize)
                                  -> std::option::Option<clingo_id_t> {
        unsafe {
            let ClingoConfiguration(ref mut conf) = *self;
            let mut nkey = 0 as clingo_id_t;
            let err = clingo_configuration_array_at(conf, key, offset, &mut nkey);
            if !err {
                None
            } else {
                Some(nkey)
            }
        }
    }
    //     pub fn clingo_configuration_map_size(configuration: *mut ClingoConfiguration,
    //                                          key: clingo_id_t,
    //                                          size: *mut size_t)
    //                                          -> u8;
    //     pub fn clingo_configuration_map_subkey_name(configuration: *mut ClingoConfiguration,
    //                                                 key: clingo_id_t,
    //                                                 offset: size_t,
    //                                                 name: *mut *const c_char)
    //                                                 -> u8;
    pub fn configuration_map_at(&mut self,
                                key: clingo_id_t,
                                name: &str)
                                -> std::option::Option<clingo_id_t> {
        unsafe {
            let ClingoConfiguration(ref mut conf) = *self;
            let mut nkey = 0 as clingo_id_t;
            let err = clingo_configuration_map_at(conf,
                                                  key,
                                                  CString::new(name).unwrap().as_ptr(),
                                                  &mut nkey);

            if !err {
                None
            } else {
                Some(nkey)
            }
        }
    }
    //     pub fn clingo_configuration_value_is_assigned(configuration: *mut ClingoConfiguration,
    //                                                   key: clingo_id_t,
    //                                                   assigned: *mut u8)
    //                                                   -> u8;
    //     pub fn clingo_configuration_value_get_size(configuration: *mut ClingoConfiguration,
    //                                                key: clingo_id_t,
    //                                                size: *mut size_t)
    //                                                -> u8;
    //     pub fn clingo_configuration_value_get(configuration: *mut ClingoConfiguration,
    //                                           key: clingo_id_t,
    //                                           value: *mut c_char,
    //                                           size: size_t)
    //                                           -> u8;
    pub fn configuration_value_set(&mut self, key: clingo_id_t, value: &str) -> bool {
        unsafe {
            let ClingoConfiguration(ref mut conf) = *self;
            clingo_configuration_value_set(conf, key, CString::new(value).unwrap().as_ptr())
        }
    }
}

pub struct ClingoBackend(clingo_backend_t);
impl ClingoBackend {
    pub fn rule(&mut self,
                choice: bool,
                head_vector: &Vec<clingo_atom_t>,
                body_vector: &Vec<clingo_literal_t>)
                -> bool {

        let head = head_vector.as_ptr();
        let head_size = head_vector.len();

        let body = body_vector.as_ptr();
        let body_size = body_vector.len();
        unsafe {
            let ClingoBackend(ref mut backend) = *self;
            clingo_backend_rule(backend, choice, head, head_size, body, body_size)
        }
    }
    //     pub fn clingo_backend_weight_rule(backend: *mut ClingoBackend,
    //                                       choice: u8,
    //                                       head: *const clingo_atom_t,
    //                                       head_size: size_t,
    //                                       lower_bound: clingo_weight_t,
    //                                       body: *const clingo_weighted_literal_t,
    //                                       body_size: size_t)
    //                                       -> u8;
    //     pub fn clingo_backend_minimize(backend: *mut ClingoBackend,
    //                                    priority: clingo_weight_t,
    //                                    literals: *const clingo_weighted_literal_t,
    //                                    size: size_t)
    //                                    -> u8;
    //     pub fn clingo_backend_project(backend: *mut ClingoBackend,
    //                                   atoms: *const clingo_atom_t,
    //                                   size: size_t)
    //                                   -> u8;
    //     pub fn clingo_backend_external(backend: *mut ClingoBackend,
    //                                    atom: clingo_atom_t,
    //                                    type_: clingo_external_type_t)
    //                                    -> u8;
    pub fn assume(&mut self, literals: *const clingo_literal_t, size: usize) -> bool {

        unsafe {
            let ClingoBackend(ref mut backend) = *self;
            clingo_backend_assume(backend, literals, size)
        }
    }
    //     pub fn clingo_backend_heuristic(backend: *mut ClingoBackend,
    //                                     atom: clingo_atom_t,
    //                                     type_: clingo_heuristic_type_t,
    //                                     bias: c_int,
    //                                     priority: ::std::os::raw::c_uint,
    //                                     condition: *const clingo_literal_t,
    //                                     size: size_t)
    //                                     -> u8;
    //     pub fn clingo_backend_acyc_edge(backend: *mut ClingoBackend,
    //                                     node_u: c_int,
    //                                     node_v: c_int,
    //                                     condition: *const clingo_literal_t,
    //                                     size: size_t)
    //                                     -> u8;
    pub fn add_atom(&mut self) -> std::option::Option<clingo_atom_t> {
        unsafe {
            let ClingoBackend(ref mut backend) = *self;
            let mut atom = 0 as clingo_atom_t;
            let err = clingo_backend_add_atom(backend, &mut atom);
            if !err {
                None
            } else {
                Some(atom)
            }
        }
    }
}

pub struct ClingoStatistics(clingo_statistics_t);
impl ClingoStatistics {
    pub fn statistics_root(&mut self) -> std::option::Option<u64> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut root_key = 0 as u64;
            let err = clingo_statistics_root(stats, &mut root_key);
            if !err {
                None
            } else {
                Some(root_key)
            }
        }
    }
    pub fn statistics_type(&mut self, key: u64) -> std::option::Option<clingo_statistics_type_t> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut stype = 0 as clingo_statistics_type_t;
            let err = clingo_statistics_type(stats, key, &mut stype);
            if !err {
                None
            } else {
                Some(stype)
            }
        }
    }
    pub fn statistics_array_size(&mut self, key: u64) -> std::option::Option<usize> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut size = 0 as usize;
            let err = clingo_statistics_array_size(stats, key, &mut size);
            if !err {
                None
            } else {
                Some(size)
            }
        }
    }
    pub fn statistics_array_at(&mut self, key: u64, offset: usize) -> std::option::Option<u64> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut subkey = 0 as u64;
            let err = clingo_statistics_array_at(stats, key, offset, &mut subkey);
            if !err {
                None
            } else {
                Some(subkey)
            }
        }
    }
    pub fn statistics_map_size(&mut self, key: u64) -> std::option::Option<usize> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut size = 0 as usize;
            let err = clingo_statistics_map_size(stats, key, &mut size);
            if !err {
                None
            } else {
                Some(size)
            }
        }
    }
    pub fn statistics_map_subkey_name<'a>(&mut self,
                                          key: u64,
                                          offset: usize)
                                          -> std::option::Option<&'a str> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut name = std::ptr::null() as *const c_char;

            let err = clingo_statistics_map_subkey_name(stats, key, offset, &mut name);
            if !err {
                None
            } else {
                Some(CStr::from_ptr(name).to_str().unwrap())
            }
        }
    }
    pub fn statistics_map_at(&mut self, key: u64, name: &str) -> std::option::Option<u64> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut subkey = 0 as u64;
            let err = clingo_statistics_map_at(stats,
                                               key,
                                               CString::new(name).unwrap().as_ptr(),
                                               &mut subkey);
            if !err {
                None
            } else {
                Some(subkey)
            }
        }
    }
    pub fn statistics_value_get(&mut self, key: u64) -> std::option::Option<f64> {
        unsafe {
            let ClingoStatistics(ref mut stats) = *self;
            let mut value = 0.0 as f64;
            let err = clingo_statistics_value_get(stats, key, &mut value);
            if !err {
                None
            } else {
                Some(value)
            }
        }
    }
}

pub struct ClingoSymbolicAtoms(clingo_symbolic_atoms_t);
impl ClingoSymbolicAtoms {
    pub fn begin(&mut self,
                 signature: *const clingo_signature_t)
                 -> std::option::Option<clingo_symbolic_atom_iterator_t> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut iterator = 0 as clingo_symbolic_atom_iterator_t;
            let err = clingo_symbolic_atoms_begin(atoms, signature, &mut iterator);
            if !err {
                None
            } else {
                Some(iterator)
            }
        }
    }
    pub fn end(&mut self) -> std::option::Option<clingo_symbolic_atom_iterator_t> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut iterator = 0 as clingo_symbolic_atom_iterator_t;
            let err = clingo_symbolic_atoms_end(atoms, &mut iterator);
            if !err {
                None
            } else {
                Some(iterator)
            }
        }
    }
    pub fn find(&mut self,
                symbol: clingo_symbol_t)
                -> std::option::Option<clingo_symbolic_atom_iterator_t> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut iterator = 0 as clingo_symbolic_atom_iterator_t;
            let err = clingo_symbolic_atoms_find(atoms, symbol, &mut iterator);
            if !err {
                None
            } else {
                Some(iterator)
            }
        }
    }
    pub fn iterator_is_equal_to(&mut self,
                                a: clingo_symbolic_atom_iterator_t,
                                b: clingo_symbolic_atom_iterator_t)
                                -> std::option::Option<bool> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut equal = false;
            let err = clingo_symbolic_atoms_iterator_is_equal_to(atoms, a, b, &mut equal);
            if !err {
                None
            } else {
                Some(equal)
            }
        }
    }
    pub fn symbol(&mut self,
                  iterator: clingo_symbolic_atom_iterator_t)
                  -> std::option::Option<clingo_symbol_t> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut symbol = 0 as clingo_symbol_t;
            let err = clingo_symbolic_atoms_symbol(atoms, iterator, &mut symbol);
            if !err {
                None
            } else {
                Some(symbol)
            }
        }
    }
    pub fn is_fact(&mut self,
                   iterator: clingo_symbolic_atom_iterator_t)
                   -> std::option::Option<bool> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut fact = false;
            let err = clingo_symbolic_atoms_is_fact(atoms, iterator, &mut fact);
            if !err {
                None
            } else {
                Some(fact)
            }
        }
    }
    pub fn is_external(&mut self,
                       iterator: clingo_symbolic_atom_iterator_t)
                       -> std::option::Option<bool> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut external = false;
            let err = clingo_symbolic_atoms_is_external(atoms, iterator, &mut external);
            if !err {
                None
            } else {
                Some(external)
            }
        }
    }
    pub fn literal(&mut self,
                   iterator: clingo_symbolic_atom_iterator_t)
                   -> std::option::Option<clingo_literal_t> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut literal = 0 as clingo_literal_t;
            let err = clingo_symbolic_atoms_literal(atoms, iterator, &mut literal);
            if !err {
                None
            } else {
                Some(literal)
            }
        }
    }
    //     pub fn clingo_symbolic_atoms_signatures_size(atoms: *mut ClingoSymbolicAtoms,
    //                                                  size: *mut size_t)
    //                                                  -> u8;
    //     pub fn clingo_symbolic_atoms_signatures(atoms: *mut ClingoSymbolicAtoms,
    //                                             signatures: *mut clingo_signature_t,
    //                                             size: size_t)
    //                                             -> u8;
    pub fn next(&mut self,
                iterator: clingo_symbolic_atom_iterator_t)
                -> std::option::Option<clingo_symbolic_atom_iterator_t> {
        unsafe {
            let ClingoSymbolicAtoms(ref mut atoms) = *self;
            let mut next = 0 as clingo_symbolic_atom_iterator_t;
            let err = clingo_symbolic_atoms_next(atoms, iterator, &mut next);
            if !err {
                None
            } else {
                Some(next)
            }
        }
    }
    //     pub fn clingo_symbolic_atoms_is_valid(atoms: *mut ClingoSymbolicAtoms,
    //                                           iterator: clingo_symbolic_atom_iterator_t,
    //                                           valid: *mut u8)
    //                                           -> u8;
}

pub struct ClingoTheoryAtoms(clingo_theory_atoms_t);
impl ClingoTheoryAtoms {
    //     pub fn clingo_theory_atoms_term_type(atoms: *mut ClingoTheoryAtoms,
    //                                          term: clingo_id_t,
    //                                          type_: *mut clingo_theory_term_type_t)
    //                                          -> u8;
    //     pub fn clingo_theory_atoms_term_number(atoms: *mut ClingoTheoryAtoms,
    //                                            term: clingo_id_t,
    //                                            number: *mut c_int)
    //                                            -> u8;
    pub fn term_name<'a>(&mut self, term: clingo_id_t) -> std::option::Option<&'a str> {
        unsafe {
            let ClingoTheoryAtoms(ref mut atoms) = *self;
            let mut char_ptr = std::ptr::null() as *const c_char;
            let err = clingo_theory_atoms_term_name(atoms, term, &mut char_ptr);
            if !err {
                None
            } else {
                let c_str = CStr::from_ptr(char_ptr);
                Some(c_str.to_str().unwrap())
            }
        }
    }
    //     pub fn clingo_theory_atoms_term_arguments(atoms: *mut ClingoTheoryAtoms,
    //                                               term: clingo_id_t,
    //                                               arguments: *mut *const clingo_id_t,
    //                                               size: *mut size_t)
    //                                               -> u8;
    //     pub fn clingo_theory_atoms_term_to_string_size(atoms: *mut ClingoTheoryAtoms,
    //                                                    term: clingo_id_t,
    //                                                    size: *mut size_t)
    //                                                    -> u8;
    //     pub fn clingo_theory_atoms_term_to_string(atoms: *mut ClingoTheoryAtoms,
    //                                               term: clingo_id_t,
    //                                               string: *mut c_char,
    //                                               size: size_t)
    //                                               -> u8;
    //     pub fn clingo_theory_atoms_element_tuple(atoms: *mut ClingoTheoryAtoms,
    //                                              element: clingo_id_t,
    //                                              tuple: *mut *const clingo_id_t,
    //                                              size: *mut size_t)
    //                                              -> u8;
    //     pub fn clingo_theory_atoms_element_condition(atoms: *mut ClingoTheoryAtoms,
    //                                                  element: clingo_id_t,
    //                                                  condition: *mut *const clingo_literal_t,
    //                                                  size: *mut size_t)
    //                                                  -> u8;
    //     pub fn clingo_theory_atoms_element_condition_id(atoms: *mut ClingoTheoryAtoms,
    //                                                     element: clingo_id_t,
    //                                                     condition: *mut clingo_literal_t)
    //                                                     -> u8;
    //     pub fn clingo_theory_atoms_element_to_string_size(atoms: *mut ClingoTheoryAtoms,
    //                                                       element: clingo_id_t,
    //                                                       size: *mut size_t)
    //                                                       -> u8;
    //     pub fn clingo_theory_atoms_element_to_string(atoms: *mut ClingoTheoryAtoms,
    //                                                  element: clingo_id_t,
    //                                                  string: *mut c_char,
    //                                                  size: size_t)
    //                                                  -> u8;
    pub fn size(&mut self) -> std::option::Option<usize> {
        unsafe {
            let ClingoTheoryAtoms(ref mut atoms) = *self;
            let mut size = 0 as usize;
            let err = clingo_theory_atoms_size(atoms, &mut size);
            if !err {
                None
            } else {
                Some(size)
            }
        }
    }
    pub fn atom_term(&mut self, atom: clingo_id_t) -> std::option::Option<clingo_id_t> {
        unsafe {
            let ClingoTheoryAtoms(ref mut atoms) = *self;
            let mut term = 0 as clingo_id_t;
            let err = clingo_theory_atoms_atom_term(atoms, atom, &mut term);
            if !err {
                None
            } else {
                Some(term)
            }
        }
    }
    //     pub fn clingo_theory_atoms_atom_elements(atoms: *mut ClingoTheoryAtoms,
    //                                              atom: clingo_id_t,
    //                                              elements: *mut *const clingo_id_t,
    //                                              size: *mut size_t)
    //                                              -> u8;
    pub fn atom_has_guard(&mut self, atom: clingo_id_t) -> std::option::Option<bool> {
        unsafe {
            let ClingoTheoryAtoms(ref mut atoms) = *self;
            let mut has_guard = false;
            let err = clingo_theory_atoms_atom_has_guard(atoms, atom, &mut has_guard);
            if !err {
                None
            } else {
                Some(has_guard)
            }
        }
    }
    //     pub fn clingo_theory_atoms_atom_guard(atoms: *mut ClingoTheoryAtoms,
    //                                           atom: clingo_id_t,
    //                                           connective: *mut *const c_char,
    //                                           term: *mut clingo_id_t)
    //                                           -> u8;
    pub fn atom_literal(&mut self, atom: clingo_id_t) -> std::option::Option<clingo_literal_t> {
        unsafe {
            let ClingoTheoryAtoms(ref mut atoms) = *self;
            let mut literal = 0 as clingo_literal_t;
            let err = clingo_theory_atoms_atom_literal(atoms, atom, &mut literal);
            if !err {
                None
            } else {
                Some(literal)
            }
        }
    }
    //     pub fn clingo_theory_atoms_atom_to_string_size(atoms: *mut ClingoTheoryAtoms,
    //                                                    atom: clingo_id_t,
    //                                                    size: *mut size_t)
    //                                                    -> u8;
    //     pub fn clingo_theory_atoms_atom_to_string(atoms: *mut ClingoTheoryAtoms,
    //                                               atom: clingo_id_t,
    //                                               string: *mut c_char,
    //                                               size: size_t)
    //                                               -> u8;
}

pub struct ClingoModel(clingo_model_t);
impl ClingoModel {
    pub fn model_type(&mut self) -> std::option::Option<clingo_model_type_t> {
        unsafe {
            let ClingoModel(ref mut model) = *self;
            let mut mtype = 0 as clingo_model_type_t;
            let err = clingo_model_type(model, &mut mtype);
            if !err {
                None
            } else {
                Some(mtype)
            }
        }
    }
    pub fn number(&mut self) -> std::option::Option<u64> {

        unsafe {
            let ClingoModel(ref mut model) = *self;
            let mut number = 0;
            let err = clingo_model_number(model, &mut number);
            if !err {
                None
            } else {
                Some(number)
            }
        }
    }
    //     pub fn clingo_model_symbols_size(model: *mut ClingoModel,
    //                                      show: clingo_show_type_bitset_t,
    //                                      size: *mut size_t)
    //                                      -> u8;
    pub fn symbols(&mut self,
                   show: clingo_show_type_bitset_t)
                   -> std::option::Option<Vec<clingo_symbol_t>> {
        let ClingoModel(ref mut model) = *self;
        let mut size: usize = 0;
        let size_p = &mut size as *mut usize;

        let err = unsafe { clingo_model_symbols_size(model, show, size_p) };
        if !err {
            println!("no size");
            None
        } else {
            let mut a1 = Vec::<clingo_symbol_t>::with_capacity(size);
            let slice = a1.as_mut_slice();
            let symbols = slice.as_ptr() as *mut clingo_symbol_t;
            let err = unsafe { clingo_model_symbols(model, show, symbols, size) };
            if !err {
                println!("no symbols");
                None
            } else {
                let res = unsafe { Vec::from_raw_parts(symbols, size, size) };
                Some(res)
            }
        }

    }
    //     pub fn clingo_model_contains(model: *mut ClingoModel,
    //                                  atom: clingo_symbol_t,
    //                                  contained: *mut u8)
    //                                  -> u8;
    //     pub fn clingo_model_cost_size(model: *mut ClingoModel, size: *mut size_t) -> u8;
    //     pub fn clingo_model_cost(model: *mut ClingoModel, costs: *mut int64_t, size: size_t) -> u8;
    //     pub fn clingo_model_optimality_proven(model: *mut ClingoModel, proven: *mut u8) -> u8;
    //     pub fn clingo_model_context(model: *mut ClingoModel,
    //                                 control: *mut *mut ClingoSolveControl)
    //                                 -> u8;
}

// impl clingo_symbol_t {
//     pub fn clingo_symbol_create_number(number: c_int, symbol: *mut clingo_symbol_t);
//     pub fn clingo_symbol_create_supremum(symbol: *mut clingo_symbol_t);
//     pub fn clingo_symbol_create_infimum(symbol: *mut clingo_symbol_t);
//     pub fn clingo_symbol_create_string(string: *const c_char, symbol: *mut clingo_symbol_t) -> u8;
//     pub fn clingo_symbol_create_id(name: *const c_char,
//                                    positive: u8,
//                                    symbol: *mut clingo_symbol_t)
//                                    -> u8;
//     pub fn clingo_symbol_create_function(name: *const c_char,
//                                          arguments: *const clingo_symbol_t,
//                                          arguments_size: size_t,
//                                          positive: u8,
//                                          symbol: *mut clingo_symbol_t)
//                                          -> u8;
//     pub fn clingo_symbol_number(symbol: clingo_symbol_t, number: *mut c_int) -> u8;
//     pub fn clingo_symbol_name(symbol: clingo_symbol_t, name: *mut *const c_char) -> u8;
//     pub fn clingo_symbol_string(symbol: clingo_symbol_t, string: *mut *const c_char) -> u8;
//     pub fn clingo_symbol_is_positive(symbol: clingo_symbol_t, positive: *mut u8) -> u8;
//     pub fn clingo_symbol_is_negative(symbol: clingo_symbol_t, negative: *mut u8) -> u8;
//     pub fn clingo_symbol_arguments(symbol: clingo_symbol_t,
//                                    arguments: *mut *const clingo_symbol_t,
//                                    arguments_size: *mut size_t)
//                                    -> u8;
//     pub fn clingo_symbol_type(symbol: clingo_symbol_t) -> clingo_symbol_type_t;
//     pub fn clingo_symbol_to_string_size(symbol: clingo_symbol_t, size: *mut size_t) -> u8;
//     pub fn to_string(&mut self) -> std::result::Result<CString, u8> {
//
//         let mut size: usize = 0;
//         let size_p = &mut size as *mut usize;
//         unsafe {
//             let err1 = clingo_symbol_to_string_size(self, size_p);
//             if err1 == 0 {
//                 Err(err1)
//             } else {
//                 let a1 = vec![1; size];
//                 let string = CString::from_vec_unchecked(a1);
//
//                 let err2 = clingo_symbol_to_string(self, string.as_ptr() as *mut c_char, size);
//                 if err2 == 0 {
//                     Err(err2)
//                 } else {
//                     Ok(string)
//                 }
//             }
//         }
//     }
//     pub fn clingo_symbol_is_equal_to(a: clingo_symbol_t, b: clingo_symbol_t) -> u8;
//     pub fn clingo_symbol_is_less_than(a: clingo_symbol_t, b: clingo_symbol_t) -> u8;
//     pub fn clingo_symbol_hash(symbol: clingo_symbol_t) -> size_t;
// }

pub struct ClingoSolveControl(clingo_solve_control_t);
impl ClingoSolveControl {
    pub fn add_clause(&mut self, clause: *const clingo_literal_t) -> bool {

        unsafe {
            let ClingoSolveControl(ref mut control) = *self;
            let size = 0; //TODO: comute size of clause
            clingo_solve_control_add_clause(control, clause, size)
        }
    }
}

pub struct ClingoPropagateControl(clingo_propagate_control_t);
impl ClingoPropagateControl {
    pub fn thread_id(&mut self) -> clingo_id_t {
        unsafe {
            let ClingoPropagateControl(ref mut control) = *self;
            clingo_propagate_control_thread_id(control)
        }
    }
    //     pub fn clingo_propagate_control_assignment(control: *mut ClingoPropagateControl)
    //                                                -> *mut clingo_assignment_t;
    pub fn add_clause(&mut self,
                      clause: &[clingo_literal_t],
                      type_: clingo_clause_type_t)
                      -> std::option::Option<bool> {
        unsafe {
            let ClingoPropagateControl(ref mut control) = *self;
            let size = 0; //TODO: compute size of claus
            let mut result = false;
            let err = clingo_propagate_control_add_clause(control,
                                                          clause.as_ptr(),
                                                          size,
                                                          type_,
                                                          &mut result);
            if !err {
                None
            } else {
                Some(result)
            }
        }
    }
    pub fn propagate(&mut self) -> std::option::Option<bool> {
        unsafe {
            let ClingoPropagateControl(ref mut control) = *self;
            let mut result = false;
            let err = clingo_propagate_control_propagate(control, &mut result);
            if !err {
                None
            } else {
                Some(result)
            }
        }
    }
}

pub struct ClingoPropagateInit(clingo_propagate_init_t);
impl ClingoPropagateInit {
    //     pub fn clingo_propagate_init_solver_literal(init: *mut ClingoPropagateInit,
    //                                                 aspif_literal: clingo_literal_t,
    //                                                 solver_literal: *mut clingo_literal_t)
    //                                                 -> u8;
    //     pub fn clingo_propagate_init_add_watch(init: *mut ClingoPropagateInit,
    //                                            solver_literal: clingo_literal_t)
    //                                            -> u8;
    //     pub fn clingo_propagate_init_symbolic_atoms(init: *mut ClingoPropagateInit,
    //                                                 atoms: *mut *mut ClingoSymbolicAtoms)
    //                                                 -> u8;
    //     pub fn c_lingo_propagate_init_theory_atoms(init: *mut ClingoPropagateInit,
    //                                               atoms: *mut *mut ClingoTheoryAtoms)
    //                                               -> u8;
    pub fn number_of_threads(&mut self) -> c_int {
        unsafe {
            let ClingoPropagateInit(ref mut init) = *self;
            let ret = clingo_propagate_init_number_of_threads(init);
            (ret as c_int)
        }
    }
}
pub struct ClingoSolveHandle(clingo_solve_handle);
impl ClingoSolveHandle {
    /// Get the next solve result.
    ///
    /// Blocks until the result is ready.
    /// When yielding partial solve results can be obtained, i.e.,
    /// when a model is ready, the result will be satisfiable but neither the search exhausted nor the optimality proven.
    ///
    /// @param[in] handle the target
    /// @param[out] result the solve result
    /// @return whether the call was successful; might set one of the following error codes:
    /// - ::clingo_error_bad_alloc
    /// - ::clingo_error_runtime if solving fails
    pub fn get(&mut self) -> Option<clingo_solve_result_bitset_t> {
        let ClingoSolveHandle(ref mut handle) = *self;
        let mut result = 0;
        let err = unsafe { clingo_solve_handle_get(handle, &mut result) };
        if !err {
            None
        } else {
            Some(result)
        }
    }

    /// Get the next model (or zero if there are no more models).
    ///
    /// @param[in] handle the target
    /// @param[out] model the model (it is NULL if there are no more models)
    /// @return whether the call was successful; might set one of the following error codes:
    /// - ::clingo_error_bad_alloc
    /// - ::clingo_error_runtime if solving fails

    pub fn model(&mut self) -> Option<&mut ClingoModel> {
        let ClingoSolveHandle(ref mut handle) = *self;
        let mut model = std::ptr::null_mut() as *mut clingo_model_t;
        unsafe {
            let err = clingo_solve_handle_model(handle, &mut model);
            if !err {
                None
            } else {
                Some(&mut *(model as *mut ClingoModel))
            }
        }
    }

    /// Discards the last model and starts the search for the next one.
    ///
    /// If the search has been started asynchronously, this function continues the search in the background.
    ///
    /// @note This function does not block.
    ///
    /// @return whether the call was successful; might set one of the following error codes:
    /// - ::clingo_error_bad_alloc
    /// - ::clingo_error_runtime if solving fails

    pub fn resume(&mut self) -> bool {
        let ClingoSolveHandle(ref mut handle) = *self;
        unsafe { clingo_solve_handle_resume(handle) }
    }
    /// Stops the running search and releases the handle.
    ///
    /// Blocks until the search is stopped (as if an implicit cancel was called before the handle is released).
    ///
    /// @param[in] handle the target
    /// @return whether the call was successful; might set one of the following error codes:
    /// - ::clingo_error_bad_alloc
    /// - ::clingo_error_runtime if solving fails
    pub fn close(&mut self) -> bool {
        let ClingoSolveHandle(ref mut handle) = *self;
        unsafe { clingo_solve_handle_close(handle) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn version_test() {
        let (ma, mi, re) = safe_clingo_version();
        assert!(ma == 5);
        assert!(mi == 0);
        assert!(re == 0);
    }
}