Skip to content

Commit 022f4d0

Browse files
authored
Merge pull request #67 from nbigaouette-eai/bindgen
Modify tensorflow-sys/src/lib.rs to be bindgen generated
2 parents bc5b8e0 + 257d491 commit 022f4d0

File tree

8 files changed

+999
-580
lines changed

8 files changed

+999
-580
lines changed

src/buffer.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::ops::Range;
1717
use std::ops::RangeFrom;
1818
use std::ops::RangeFull;
1919
use std::ops::RangeTo;
20+
use std::os::raw::c_void as std_c_void;
2021
use std::ptr;
2122
use std::slice;
2223
use super::BufferTrait;
@@ -65,7 +66,7 @@ impl<T: TensorType> Buffer<T> {
6566
let msg = CStr::from_ptr(c_msg);
6667
panic!("Failed to allocate: {}", msg.to_str().unwrap());
6768
}
68-
(*inner).data = ptr;
69+
(*inner).data = ptr as *mut std_c_void;
6970
(*inner).length = len;
7071
(*inner).data_deallocator = Some(deallocator);
7172
Buffer {
@@ -81,7 +82,7 @@ impl<T: TensorType> Buffer<T> {
8182
/// The underlying data is *not* freed when the buffer is destroyed.
8283
pub unsafe fn from_ptr(ptr: *mut T, len: usize) -> Self {
8384
let inner = tf::TF_NewBuffer();
84-
(*inner).data = ptr as *const c_void;
85+
(*inner).data = ptr as *const std_c_void;
8586
(*inner).length = len;
8687
Buffer {
8788
inner: inner,
@@ -155,8 +156,8 @@ impl<T: TensorType> BufferTrait for Buffer<T> {
155156
}
156157
}
157158

158-
unsafe extern "C" fn deallocator(data: *mut c_void, _length: size_t) {
159-
libc::free(data);
159+
unsafe extern "C" fn deallocator(data: *mut std_c_void, _length: size_t) {
160+
libc::free(data as *mut c_void);
160161
}
161162

162163
impl<T: TensorType> Drop for Buffer<T> {

src/graph.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std;
99
use std::ffi::CStr;
1010
use std::ffi::CString;
1111
use std::ffi::NulError;
12+
use std::os::raw::c_void as std_c_void;
1213
use std::ptr;
1314
use std::str::Utf8Error;
1415
use std::sync::Arc;
@@ -302,7 +303,7 @@ impl Operation {
302303
pub fn output_type(&self, index: usize) -> DataType {
303304
unsafe {
304305
DataType::from_c(tf::TF_OperationOutputType(tf::TF_Output {
305-
operation: self.inner,
306+
oper: self.inner,
306307
index: index as c_int,
307308
}))
308309
}
@@ -332,7 +333,7 @@ impl Operation {
332333
pub fn input_type(&self, index: usize) -> DataType {
333334
unsafe {
334335
DataType::from_c(tf::TF_OperationInputType(tf::TF_Input {
335-
operation: self.inner,
336+
oper: self.inner,
336337
index: index as c_int,
337338
}))
338339
}
@@ -359,11 +360,11 @@ impl Operation {
359360
pub fn input(&self, index: usize) -> (Operation, usize) {
360361
unsafe {
361362
let port = tf::TF_OperationInput(tf::TF_Input {
362-
operation: self.inner,
363+
oper: self.inner,
363364
index: index as c_int,
364365
});
365366
(Operation {
366-
inner: port.operation,
367+
inner: port.oper,
367368
gimpl: self.gimpl.clone(),
368369
},
369370
port.index as usize)
@@ -374,7 +375,7 @@ impl Operation {
374375
pub fn output_num_consumers(&self, index: usize) -> usize {
375376
unsafe {
376377
tf::TF_OperationOutputNumConsumers(tf::TF_Output {
377-
operation: self.inner,
378+
oper: self.inner,
378379
index: index as c_int,
379380
}) as usize
380381
}
@@ -387,12 +388,12 @@ impl Operation {
387388
pub fn output_consumers(&self, index: usize) -> Vec<(Operation, usize)> {
388389
unsafe {
389390
let num_consumers = tf::TF_OperationOutputNumConsumers(tf::TF_Output {
390-
operation: self.inner,
391+
oper: self.inner,
391392
index: index as c_int,
392393
});
393394
let mut vec = <Vec<tf::TF_Input>>::with_capacity(num_consumers as usize);
394395
let len = tf::TF_OperationOutputConsumers(tf::TF_Output {
395-
operation: self.inner,
396+
oper: self.inner,
396397
index: index as c_int,
397398
},
398399
vec.as_mut_ptr(),
@@ -401,7 +402,7 @@ impl Operation {
401402
vec.into_iter()
402403
.map(|port| {
403404
(Operation {
404-
inner: port.operation,
405+
inner: port.oper,
405406
gimpl: self.gimpl.clone(),
406407
},
407408
port.index as usize)
@@ -483,7 +484,7 @@ pub struct Input<'a> {
483484
impl<'a> Input<'a> {
484485
fn to_c(&self) -> tf::TF_Input {
485486
tf::TF_Input {
486-
operation: self.operation.inner,
487+
oper: self.operation.inner,
487488
index: self.index,
488489
}
489490
}
@@ -505,7 +506,7 @@ pub struct Output<'a> {
505506
impl<'a> Output<'a> {
506507
fn to_c(&self) -> tf::TF_Output {
507508
tf::TF_Output {
508-
operation: self.operation.inner,
509+
oper: self.operation.inner,
509510
index: self.index,
510511
}
511512
}
@@ -606,7 +607,7 @@ impl<'a> OperationDescription<'a> {
606607
unsafe {
607608
tf::TF_SetAttrString(self.inner,
608609
c_attr_name.as_ptr(),
609-
c_value.as_ptr() as *const c_void,
610+
c_value.as_ptr() as *const std_c_void,
610611
c_value.len() as size_t);
611612
}
612613
Ok(())
@@ -625,7 +626,7 @@ impl<'a> OperationDescription<'a> {
625626
unsafe {
626627
tf::TF_SetAttrStringList(self.inner,
627628
c_attr_name.as_ptr(),
628-
ptrs.as_ptr(),
629+
ptrs.as_ptr() as *const *const std_c_void,
629630
lens.as_ptr(),
630631
ptrs.len() as c_int);
631632
}
@@ -821,7 +822,7 @@ impl<'a> OperationDescription<'a> {
821822
unsafe {
822823
tf::TF_SetAttrTensorShapeProto(self.inner,
823824
c_attr_name.as_ptr(),
824-
value.as_ptr() as *const c_void,
825+
value.as_ptr() as *const std_c_void,
825826
value.len() as size_t,
826827
status.inner());
827828
}
@@ -843,7 +844,7 @@ impl<'a> OperationDescription<'a> {
843844
unsafe {
844845
tf::TF_SetAttrTensorShapeProtoList(self.inner,
845846
c_attr_name.as_ptr(),
846-
ptrs.as_ptr(),
847+
ptrs.as_ptr() as *const *const std_c_void,
847848
lens.as_ptr(),
848849
ptrs.len() as c_int,
849850
status.inner());
@@ -878,7 +879,7 @@ impl<'a> OperationDescription<'a> {
878879
let ptrs: Vec<*mut tf::TF_Tensor> = value.into_iter().map(|x| x.into_ptr()).collect();
879880
tf::TF_SetAttrTensorList(self.inner,
880881
c_attr_name.as_ptr(),
881-
ptrs.as_ptr(),
882+
ptrs.as_ptr() as *const *const tf::TF_Tensor,
882883
ptrs.len() as c_int,
883884
status.inner());
884885
}
@@ -893,7 +894,7 @@ impl<'a> OperationDescription<'a> {
893894
unsafe {
894895
tf::TF_SetAttrValueProto(self.inner,
895896
c_attr_name.as_ptr(),
896-
value.as_ptr() as *const c_void,
897+
value.as_ptr() as *const std_c_void,
897898
// Allow trivial_numeric_casts because usize is not
898899
// necessarily size_t.
899900
value.len() as size_t,

src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::ops::Deref;
3030
use std::ops::DerefMut;
3131
use std::ops::Drop;
3232
use std::ops::Index;
33+
use std::os::raw::c_void as std_c_void;
3334
use std::str::Utf8Error;
3435

3536
////////////////////////
@@ -844,9 +845,9 @@ pub struct Tensor<T: TensorType> {
844845
owned: bool,
845846
}
846847

847-
unsafe extern "C" fn noop_deallocator(_: *mut c_void, _: size_t, _: *mut c_void) -> () {}
848+
unsafe extern "C" fn noop_deallocator(_: *mut std_c_void, _: size_t, _: *mut std_c_void) -> () {}
848849

849-
unsafe extern "C" fn deallocator(_: *mut c_void, _: size_t, buffer: *mut c_void) -> () {
850+
unsafe extern "C" fn deallocator(_: *mut std_c_void, _: size_t, buffer: *mut std_c_void) -> () {
850851
tf::TF_DeleteBuffer(buffer as *mut tf::TF_Buffer);
851852
}
852853

src/session.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Session {
6969
tf::TF_SessionRun(self.inner,
7070
ptr::null(),
7171
step.input_ports.as_ptr(),
72-
input_tensors.as_mut_ptr(),
72+
input_tensors.as_ptr() as *const *const tf::TF_Tensor,
7373
input_tensors.len() as c_int,
7474
step.output_ports.as_ptr(),
7575
step.output_tensors.as_mut_ptr(),
@@ -143,7 +143,7 @@ impl<'l> StepWithGraph<'l> {
143143
index: c_int,
144144
tensor: &'l Tensor<T>) {
145145
self.input_ports.push(tf::TF_Output {
146-
operation: operation.inner(),
146+
oper: operation.inner(),
147147
index: index,
148148
});
149149
self.input_tensors.push(tensor.inner);
@@ -153,7 +153,7 @@ impl<'l> StepWithGraph<'l> {
153153
/// Returns an index that you can then use to fetch this output from the step after running it.
154154
pub fn request_output(&mut self, operation: &Operation, index: c_int) -> OutputToken {
155155
self.output_ports.push(tf::TF_Output {
156-
operation: operation.inner(),
156+
oper: operation.inner(),
157157
index: index,
158158
});
159159
self.output_tensors.push(ptr::null_mut());

tensorflow-sys/examples/multiplication.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate libc;
22
extern crate tensorflow_sys as ffi;
33

4-
use libc::{c_int, c_void, int64_t, size_t};
4+
use libc::{c_int, int64_t, size_t};
55
use std::ffi::{CStr, CString};
66
use std::path::Path;
77

@@ -107,7 +107,7 @@ fn main() {
107107
ffi::TF_DeleteSessionOptions(options);
108108
}
109109

110-
unsafe extern "C" fn noop(_: *mut c_void, _: size_t, _: *mut c_void) {}
110+
unsafe extern "C" fn noop(_: *mut std::os::raw::c_void, _: size_t, _: *mut std::os::raw::c_void) {}
111111
}
112112

113113
fn read<T: AsRef<Path>>(path: T) -> Vec<u8> {

tensorflow-sys/generate_bindgen_rs.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
if ! which bindgen &> /dev/null; then
4+
echo "ERROR: Please install 'bindgen' using cargo:"
5+
echo " cargo install bindgen"
6+
echo "See https://github.com/servo/rust-bindgen for more information."
7+
exit 1
8+
fi
9+
10+
# See https://github.com/servo/rust-bindgen/issues/550 as to why
11+
# this is blacklisted.
12+
bindgen_options="--blacklist-type max_align_t"
13+
header="/usr/include/tensorflow/c_api.h"
14+
15+
cmd="bindgen ${bindgen_options} ${header} --output src/bindgen.rs"
16+
echo ${cmd}
17+
${cmd}

0 commit comments

Comments
 (0)