Skip to content

Commit 32f0868

Browse files
committed
Simplify string passing between Rust and C
1 parent 6e41590 commit 32f0868

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/graphics.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ impl Window {
148148
pub fn new(width: i32, height: i32, title: String) -> Window {
149149
unsafe {
150150
let mut temp: u64 = 0;
151-
let cstr_ret = CString::new(title.as_bytes());
151+
let cstr_ret = CString::new(title);
152152
match cstr_ret {
153153
Ok(cstr) => {
154154
let err_val = af_create_window(&mut temp as MutWndHandle,
155155
width as c_int, height as c_int,
156-
cstr.to_bytes_with_nul().as_ptr() as *const c_char);
156+
cstr.as_ptr());
157157
HANDLE_ERROR(AfError::from(err_val));
158158
Window::from(temp)
159159
},
@@ -182,11 +182,11 @@ impl Window {
182182
/// - `title` is the string to be displayed on window title bar
183183
pub fn set_title(&self, title: String) {
184184
unsafe {
185-
let cstr_ret = CString::new(title.as_bytes());
185+
let cstr_ret = CString::new(title);
186186
match cstr_ret {
187187
Ok(cstr) => {
188188
let err_val = af_set_title(self.handle as WndHandle,
189-
cstr.to_bytes_with_nul().as_ptr() as *const c_char);
189+
cstr.as_ptr());
190190
HANDLE_ERROR(AfError::from(err_val));
191191
},
192192
Err(_) => HANDLE_ERROR(AfError::ERR_INTERNAL),
@@ -285,14 +285,14 @@ impl Window {
285285
/// - `zlabel` is z axis title
286286
pub fn set_axes_titles(&mut self, xlabel: String, ylabel: String, zlabel: String) {
287287
let cprops = &Cell {row: self.row, col: self.col, title: String::from(""), cmap: self.cmap};
288-
let xstr = CString::new(xlabel.as_bytes()).unwrap();
289-
let ystr = CString::new(ylabel.as_bytes()).unwrap();
290-
let zstr = CString::new(zlabel.as_bytes()).unwrap();
288+
let xstr = CString::new(xlabel).unwrap();
289+
let ystr = CString::new(ylabel).unwrap();
290+
let zstr = CString::new(zlabel).unwrap();
291291
unsafe {
292292
let err_val = af_set_axes_titles(self.handle as WndHandle,
293-
xstr.to_bytes_with_nul().as_ptr() as *const c_char,
294-
ystr.to_bytes_with_nul().as_ptr() as *const c_char,
295-
zstr.to_bytes_with_nul().as_ptr() as *const c_char,
293+
xstr.as_ptr(),
294+
ystr.as_ptr(),
295+
zstr.as_ptr(),
296296
cprops as *const Cell as CellPtr);
297297
HANDLE_ERROR(AfError::from(err_val));
298298
}

0 commit comments

Comments
 (0)