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
use std::slice; extern crate libc; use self::libc::c_void; #[repr(C)] pub struct Array { pub data: *const c_void, pub len: libc::size_t, } use super::convert_to_bng_threaded_vec; use super::convert_to_lonlat_threaded_vec; use super::convert_to_osgb36_threaded_vec; use super::convert_to_etrs89_threaded_vec; use super::convert_etrs89_to_osgb36_threaded_vec; use super::convert_etrs89_to_ll_threaded_vec; use super::convert_osgb36_to_ll_threaded_vec; use super::convert_osgb36_to_etrs89_threaded_vec; use super::convert_epsg3857_to_wgs84_threaded_vec; /// Free memory which Rust has allocated across the FFI boundary (f64 values) /// /// # Examples /// /// ``` /// # extern crate libc; /// let lon_vec: Vec<f64> = vec![-2.0183041005533306]; /// let lat_vec: Vec<f64> = vec![54.589097162646141]; /// let lon_arr = Array { /// data: lon_vec.as_ptr() as *const libc::c_void, /// len: lon_vec.len() as libc::size_t, /// }; /// let lat_arr = Array { /// data: lat_vec.as_ptr() as *const libc::c_void, /// len: lat_vec.len() as libc::size_t, /// }; /// let (eastings, northings) = convert_to_bng_threaded(lon_arr, lat_arr); /// drop_float_array(eastings, northings); /// ``` /// An example FFI implementation is available at [Convertbng](https://github.com/urschrei/convertbng/blob/master/convertbng/util.py), specifically in the `_void_array_to_list` function. /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn drop_float_array(lons: Array, lats: Array) { if lons.data.is_null() { return; } if lats.data.is_null() { return; } let _: &mut [f64] = lons.into(); let _: &mut [f64] = lats.into(); } // Build an Array from &mut[T], so it can be leaked across the FFI boundary impl<'a, T> From<&'a mut [T]> for Array { fn from(sl: &mut [T]) -> Self { let array = Array { data: sl.as_ptr() as *const libc::c_void, len: sl.len() as libc::size_t, }; array } } // Build &mut[f64] from an Array, so it can be dropped impl<'a> From<Array> for &'a mut [f64] { fn from(arr: Array) -> Self { unsafe { slice::from_raw_parts_mut(arr.data as *mut f64, arr.len) } } } /// A threaded, FFI-compatible wrapper for `lonlat_bng::convert_osgb36` /// /// # Examples /// /// ``` /// extern crate libc; /// let lon_vec: Vec<f64> = vec![-2.0183041005533306, /// 0.95511887434519682, /// 0.44975855518383501, /// -0.096813621191803811, /// -0.36807065656416427, /// 0.63486335458665621]; /// let lat_vec: Vec<f64> = vec![54.589097162646141, /// 51.560873800587828, /// 50.431429161121699, /// 54.535021436247419, /// 50.839059313135706, /// 55.412189281234419]; /// let lon_arr = Array { /// data: lon_vec.as_ptr() as *const libc::c_void, /// len: lon_vec.len() as libc::size_t, /// }; /// let lat_arr = Array { /// data: lat_vec.as_ptr() as *const libc::c_void, /// len: lat_vec.len() as libc::size_t, /// }; /// let (eastings, northings) = convert_to_bng_threaded(lon_arr, lat_arr); /// ``` /// For an FFI implementation, see the code at [Convertbng](https://github.com/urschrei/convertbng/blob/master/convertbng/util.py). /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_to_bng_threaded(longitudes: Array, latitudes: Array) -> (Array, Array) { let (res_lon, res_lat) = convert_to_bng_threaded_vec(longitudes.into(), latitudes.into()); (res_lon.into(), res_lat.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_osgb36_to_ll`](fn.convert_osgb36_to_ll.html) /// /// # Examples /// /// See [`lonlat_bng::convert_to_bng_threaded`](fn.convert_to_bng_threaded.html) /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_to_lonlat_threaded(eastings: Array, northings: Array) -> (Array, Array) { let (res_eastings, res_northings) = convert_to_lonlat_threaded_vec(eastings.into(), northings.into()); (res_eastings.into(), res_northings.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_osgb36`](fn.convert_osgb36.html) /// /// # Examples /// /// See [`lonlat_bng::convert_to_bng_threaded`](fn.convert_to_bng_threaded.html) for examples, substituting f64 vectors /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_to_osgb36_threaded(longitudes: Array, latitudes: Array) -> (Array, Array) { let (res_lon, res_lat) = convert_to_osgb36_threaded_vec(longitudes.into(), latitudes.into()); (res_lon.into(), res_lat.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_etrs89`](fn.convert_etrs89.html) /// /// # Examples /// /// See `lonlat_bng::convert_to_bng_threaded` for examples, substituting f64 vectors /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_to_etrs89_threaded(longitudes: Array, latitudes: Array) -> (Array, Array) { let (res_lon, res_lat) = convert_to_etrs89_threaded_vec(longitudes.into(), latitudes.into()); (res_lon.into(), res_lat.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_etrs89_to_osgb36`](fn.convert_etrs89_to_osgb36.html) /// /// # Examples /// /// See `lonlat_bng::convert_to_bng_threaded` for examples /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_etrs89_to_osgb36_threaded(eastings: Array, northings: Array) -> (Array, Array) { let (res_eastings, res_northings) = convert_etrs89_to_osgb36_threaded_vec(eastings.into(), northings.into()); (res_eastings.into(), res_northings.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_etrs89_to_ll`](fn.convert_etrs89_to_ll.html) /// /// # Examples /// /// See `lonlat_bng::convert_to_bng_threaded` for examples /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_etrs89_to_ll_threaded(eastings: Array, northings: Array) -> (Array, Array) { let (res_eastings, res_northings) = convert_etrs89_to_ll_threaded_vec(eastings.into(), northings.into()); (res_eastings.into(), res_northings.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_osgb36_to_ll`](fn.convert_osgb36_to_ll.html) /// /// # Examples /// /// See `lonlat_bng::convert_to_bng_threaded` for examples /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_osgb36_to_ll_threaded(eastings: Array, northings: Array) -> (Array, Array) { let (res_eastings, res_northings) = convert_osgb36_to_ll_threaded_vec(eastings.into(), northings.into()); (res_eastings.into(), res_northings.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_osgb36_to_etrs89`](fn.convert_osgb36_to_etrs89.html) /// /// # Examples /// /// See `lonlat_bng::convert_to_bng_threaded` for examples /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_osgb36_to_etrs89_threaded(eastings: Array, northings: Array) -> (Array, Array) { let (res_eastings, res_northings) = convert_osgb36_to_etrs89_threaded_vec(eastings.into(), northings.into()); (res_eastings.into(), res_northings.into()) } /// A threaded, FFI-compatible wrapper for [`lonlat_bng::convert_epsg3857_to_wgs84`](fn.convert_epsg3857_to_wgs84.html) /// /// # Examples /// /// See `lonlat_bng::convert_to_bng_threaded` for examples /// /// # Safety /// /// This function is unsafe because it accesses a raw pointer which could contain arbitrary data #[no_mangle] pub extern "C" fn convert_epsg3857_to_wgs84_threaded(x: Array, y: Array) -> (Array, Array) { let (res_x, res_y) = convert_epsg3857_to_wgs84_threaded_vec(x.into(), y.into()); (res_x.into(), res_y.into()) }