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
extern crate gurobi_sys as ffi;

use util;
use std::ffi::CString;
use std::ptr::null;


pub trait From<T> {
  fn from(T) -> Self;
}

impl From<i32> for ffi::c_int {
  fn from(val: i32) -> ffi::c_int { val }
}

impl From<i8> for ffi::c_char {
  fn from(val: i8) -> ffi::c_char { val }
}

impl From<f64> for ffi::c_double {
  fn from(val: f64) -> ffi::c_double { val }
}

impl From<String> for ffi::c_str {
  fn from(val: String) -> ffi::c_str { CString::new(val.as_str()).unwrap().as_ptr() }
}


/// make an empty instance.
pub trait Init {
  fn init() -> Self;
}

impl Init for ffi::c_char {
  fn init() -> ffi::c_char { 0 }
}

impl Init for ffi::c_int {
  fn init() -> i32 { 0 }
}

impl Init for ffi::c_double {
  fn init() -> ffi::c_double { 0.0 }
}

impl Init for Vec<ffi::c_char> {
  fn init() -> Vec<ffi::c_char> { Vec::with_capacity(4096) }
}

impl Init for ffi::c_str {
  fn init() -> ffi::c_str { null() }
}


/// convert into different type.
pub trait Into<T> {
  fn into(self) -> T;
}

impl Into<i32> for ffi::c_int {
  fn into(self) -> i32 { self }
}

impl Into<f64> for ffi::c_double {
  fn into(self) -> f64 { self }
}

impl Into<String> for Vec<ffi::c_char> {
  fn into(self) -> String { unsafe { util::from_c_str(self.as_ptr()) } }
}

impl Into<i8> for ffi::c_char {
  fn into(self) -> i8 { self }
}

impl Into<String> for ffi::c_str {
  fn into(self) -> String { unsafe { util::from_c_str(self).to_owned() } }
}


/// convert to Raw C Pointer.
pub trait AsRawPtr<T> {
  fn as_rawptr(&mut self) -> T;
}

impl AsRawPtr<*mut ffi::c_int> for i32 {
  fn as_rawptr(&mut self) -> *mut ffi::c_int { self }
}

impl AsRawPtr<*mut ffi::c_char> for i8 {
  fn as_rawptr(&mut self) -> *mut ffi::c_char { self }
}

impl AsRawPtr<*mut ffi::c_double> for f64 {
  fn as_rawptr(&mut self) -> *mut ffi::c_double { self }
}

impl AsRawPtr<*mut ffi::c_str> for ffi::c_str {
  fn as_rawptr(&mut self) -> *mut ffi::c_str { self }
}

impl AsRawPtr<*mut ffi::c_char> for Vec<ffi::c_char> {
  fn as_rawptr(&mut self) -> *mut ffi::c_char { self.as_mut_ptr() }
}



pub trait FromRaw<T> {
  fn from(T) -> Self;
}

impl FromRaw<i32> for ffi::c_int {
  fn from(val: i32) -> ffi::c_int { val }
}

impl FromRaw<f64> for ffi::c_double {
  fn from(val: f64) -> ffi::c_double { val }
}

impl FromRaw<String> for ffi::c_str {
  fn from(val: String) -> *const ffi::c_char { CString::new(val.as_str()).unwrap().as_ptr() }
}