Skip to content

Commit e515228

Browse files
committed
Clean up some TODOs, compiler warnings, and lints
1 parent dfa36ec commit e515228

19 files changed

+245
-256
lines changed

examples/addition.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::error::Error;
22
use std::fs::File;
33
use std::io::Read;
44
use std::path::Path;
5-
use std::process::exit;
65
use std::result::Result;
76
use tensorflow::Code;
87
use tensorflow::Graph;
@@ -17,20 +16,7 @@ use tensorflow::Tensor;
1716
#[cfg(feature = "examples_system_alloc")]
1817
static ALLOCATOR: std::alloc::System = std::alloc::System;
1918

20-
fn main() {
21-
// Putting the main code in another function serves two purposes:
22-
// 1. We can use the `?` operator.
23-
// 2. We can call exit safely, which does not run any destructors.
24-
exit(match run() {
25-
Ok(_) => 0,
26-
Err(e) => {
27-
println!("{}", e);
28-
1
29-
}
30-
})
31-
}
32-
33-
fn run() -> Result<(), Box<dyn Error>> {
19+
fn main() -> Result<(), Box<dyn Error>> {
3420
let filename = "examples/addition/model.pb"; // z = x + y
3521
if !Path::new(filename).exists() {
3622
return Err(Box::new(

examples/expressions.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ extern crate random;
22
extern crate tensorflow;
33

44
use std::error::Error;
5-
use std::process::exit;
65
use std::result::Result;
76
use tensorflow::expr::{Compiler, Placeholder};
87
use tensorflow::Code;
@@ -17,19 +16,6 @@ use tensorflow::Tensor;
1716
#[cfg(feature = "examples_system_alloc")]
1817
static ALLOCATOR: std::alloc::System = std::alloc::System;
1918

20-
fn main() {
21-
// Putting the main code in another function serves two purposes:
22-
// 1. We can use the `?` operator.
23-
// 2. We can call exit safely, which does not run any destructors.
24-
exit(match run() {
25-
Ok(_) => 0,
26-
Err(e) => {
27-
println!("{}", e);
28-
1
29-
}
30-
})
31-
}
32-
3319
struct Checker {
3420
success: bool,
3521
epsilon: f32,
@@ -55,7 +41,7 @@ impl Checker {
5541
self.success &= success;
5642
}
5743

58-
fn result(&self) -> Result<(), Box<Error>> {
44+
fn result(&self) -> Result<(), Box<dyn Error>> {
5945
if self.success {
6046
Ok(())
6147
} else {
@@ -67,7 +53,7 @@ impl Checker {
6753
}
6854
}
6955

70-
fn run() -> Result<(), Box<Error>> {
56+
fn main() -> Result<(), Box<dyn Error>> {
7157
// Build the graph
7258
let mut g = Graph::new();
7359
let y_node = {

examples/regression.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::error::Error;
44
use std::fs::File;
55
use std::io::Read;
66
use std::path::Path;
7-
use std::process::exit;
87
use std::result::Result;
98
use tensorflow::Code;
109
use tensorflow::Graph;
@@ -19,20 +18,7 @@ use tensorflow::Tensor;
1918
#[cfg(feature = "examples_system_alloc")]
2019
static ALLOCATOR: std::alloc::System = std::alloc::System;
2120

22-
fn main() {
23-
// Putting the main code in another function serves two purposes:
24-
// 1. We can use the `?` operator.
25-
// 2. We can call exit safely, which does not run any destructors.
26-
exit(match run() {
27-
Ok(_) => 0,
28-
Err(e) => {
29-
println!("{}", e);
30-
1
31-
}
32-
})
33-
}
34-
35-
fn run() -> Result<(), Box<dyn Error>> {
21+
fn main() -> Result<(), Box<dyn Error>> {
3622
let filename = "examples/regression/model.pb"; // y = w * x + b
3723
if !Path::new(filename).exists() {
3824
return Err(Box::new(

examples/regression_checkpoint.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::error::Error;
44
use std::fs::File;
55
use std::io::Read;
66
use std::path::Path;
7-
use std::process::exit;
87
use std::result::Result;
98
use tensorflow::Code;
109
use tensorflow::Graph;
@@ -19,20 +18,7 @@ use tensorflow::Tensor;
1918
#[cfg(feature = "examples_system_alloc")]
2019
static ALLOCATOR: std::alloc::System = std::alloc::System;
2120

22-
fn main() {
23-
// Putting the main code in another function serves two purposes:
24-
// 1. We can use the `?` operator.
25-
// 2. We can call exit safely, which does not run any destructors.
26-
exit(match run() {
27-
Ok(_) => 0,
28-
Err(e) => {
29-
println!("{}", e);
30-
1
31-
}
32-
})
33-
}
34-
35-
fn run() -> Result<(), Box<dyn Error>> {
21+
fn main() -> Result<(), Box<dyn Error>> {
3622
let filename = "examples/regression_checkpoint/model.pb"; // y = w * x + b
3723
if !Path::new(filename).exists() {
3824
return Err(Box::new(

examples/regression_savedmodel.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use random;
22
use random::Source;
33
use std::error::Error;
44
use std::path::Path;
5-
use std::process::exit;
65
use std::result::Result;
76
use tensorflow::Code;
87
use tensorflow::Graph;
@@ -16,20 +15,7 @@ use tensorflow::Tensor;
1615
#[cfg(feature = "examples_system_alloc")]
1716
static ALLOCATOR: std::alloc::System = std::alloc::System;
1817

19-
fn main() {
20-
// Putting the main code in another function serves two purposes:
21-
// 1. We can use the `?` operator.
22-
// 2. We can call exit safely, which does not run any destructors.
23-
exit(match run() {
24-
Ok(_) => 0,
25-
Err(e) => {
26-
println!("{}", e);
27-
1
28-
}
29-
})
30-
}
31-
32-
fn run() -> Result<(), Box<dyn Error>> {
18+
fn main() -> Result<(), Box<dyn Error>> {
3319
let export_dir = "examples/regression_savedmodel"; // y = w * x + b
3420
if !Path::new(export_dir).exists() {
3521
return Err(Box::new(

src/buffer.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<T: TensorType> Buffer<T> {
8181
(*inner).data = ptr as *const std_c_void;
8282
(*inner).length = len;
8383
Buffer {
84-
inner: inner,
84+
inner,
8585
owned: true,
8686
phantom: PhantomData,
8787
}
@@ -110,7 +110,7 @@ impl<T: TensorType> Buffer<T> {
110110
pub unsafe fn from_c(buf: *mut tf::TF_Buffer, owned: bool) -> Self {
111111
Buffer {
112112
inner: buf,
113-
owned: owned,
113+
owned,
114114
phantom: PhantomData,
115115
}
116116
}
@@ -229,7 +229,7 @@ impl<T: TensorType> Index<usize> for Buffer<T> {
229229
index,
230230
self.length()
231231
);
232-
unsafe { &*self.data().offset(index as isize) }
232+
unsafe { &*self.data().add(index) }
233233
}
234234
}
235235

@@ -242,7 +242,7 @@ impl<T: TensorType> IndexMut<usize> for Buffer<T> {
242242
index,
243243
self.length()
244244
);
245-
unsafe { &mut *self.data_mut().offset(index as isize) }
245+
unsafe { &mut *self.data_mut().add(index) }
246246
}
247247
}
248248

@@ -263,7 +263,7 @@ impl<T: TensorType> Index<Range<usize>> for Buffer<T> {
263263
index.end,
264264
self.length()
265265
);
266-
unsafe { slice::from_raw_parts(&*self.data().offset(index.start as isize), index.len()) }
266+
unsafe { slice::from_raw_parts(&*self.data().add(index.start), index.len()) }
267267
}
268268
}
269269

@@ -282,12 +282,7 @@ impl<T: TensorType> IndexMut<Range<usize>> for Buffer<T> {
282282
index.end,
283283
self.length()
284284
);
285-
unsafe {
286-
slice::from_raw_parts_mut(
287-
&mut *self.data_mut().offset(index.start as isize),
288-
index.len(),
289-
)
290-
}
285+
unsafe { slice::from_raw_parts_mut(&mut *self.data_mut().add(index.start), index.len()) }
291286
}
292287
}
293288

@@ -331,10 +326,7 @@ impl<T: TensorType> Index<RangeFrom<usize>> for Buffer<T> {
331326
self.length()
332327
);
333328
unsafe {
334-
slice::from_raw_parts(
335-
&*self.data().offset(index.start as isize),
336-
self.length() - index.start,
337-
)
329+
slice::from_raw_parts(&*self.data().add(index.start), self.length() - index.start)
338330
}
339331
}
340332
}
@@ -350,7 +342,7 @@ impl<T: TensorType> IndexMut<RangeFrom<usize>> for Buffer<T> {
350342
);
351343
unsafe {
352344
slice::from_raw_parts_mut(
353-
&mut *self.data_mut().offset(index.start as isize),
345+
&mut *self.data_mut().add(index.start),
354346
self.length() - index.start,
355347
)
356348
}

0 commit comments

Comments
 (0)