@@ -6,6 +6,7 @@ use defines::{ColorMap, MarkerType};
6
6
use error:: HANDLE_ERROR ;
7
7
use self :: libc:: { c_int, c_uint, c_float, c_double, c_char} ;
8
8
use std:: ffi:: CString ;
9
+ use std:: ptr;
9
10
10
11
type MutWndHandle = * mut self :: libc:: c_ulonglong ;
11
12
type WndHandle = self :: libc:: c_ulonglong ;
@@ -73,7 +74,7 @@ extern {
73
74
pub struct Cell {
74
75
pub row : i32 ,
75
76
pub col : i32 ,
76
- pub title : String ,
77
+ pub title : * const c_char ,
77
78
pub cmap : ColorMap ,
78
79
}
79
80
@@ -148,12 +149,12 @@ impl Window {
148
149
pub fn new ( width : i32 , height : i32 , title : String ) -> Window {
149
150
unsafe {
150
151
let mut temp: u64 = 0 ;
151
- let cstr_ret = CString :: new ( title. as_bytes ( ) ) ;
152
+ let cstr_ret = CString :: new ( title) ;
152
153
match cstr_ret {
153
154
Ok ( cstr) => {
154
155
let err_val = af_create_window ( & mut temp as MutWndHandle ,
155
156
width as c_int , height as c_int ,
156
- cstr. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ) ;
157
+ cstr. as_ptr ( ) ) ;
157
158
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
158
159
Window :: from ( temp)
159
160
} ,
@@ -182,11 +183,11 @@ impl Window {
182
183
/// - `title` is the string to be displayed on window title bar
183
184
pub fn set_title ( & self , title : String ) {
184
185
unsafe {
185
- let cstr_ret = CString :: new ( title. as_bytes ( ) ) ;
186
+ let cstr_ret = CString :: new ( title) ;
186
187
match cstr_ret {
187
188
Ok ( cstr) => {
188
189
let err_val = af_set_title ( self . handle as WndHandle ,
189
- cstr. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ) ;
190
+ cstr. as_ptr ( ) ) ;
190
191
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
191
192
} ,
192
193
Err ( _) => HANDLE_ERROR ( AfError :: ERR_INTERNAL ) ,
@@ -284,15 +285,15 @@ impl Window {
284
285
/// - `ylabel` is y axis title
285
286
/// - `zlabel` is z axis title
286
287
pub fn set_axes_titles ( & mut self , xlabel : String , ylabel : String , zlabel : String ) {
287
- 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 cprops = & Cell { row : self . row , col : self . col , title : ptr :: null ( ) , cmap : self . cmap } ;
289
+ let xstr = CString :: new ( xlabel) . unwrap ( ) ;
290
+ let ystr = CString :: new ( ylabel) . unwrap ( ) ;
291
+ let zstr = CString :: new ( zlabel) . unwrap ( ) ;
291
292
unsafe {
292
293
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 ,
294
+ xstr. as_ptr ( ) ,
295
+ ystr. as_ptr ( ) ,
296
+ zstr. as_ptr ( ) ,
296
297
cprops as * const Cell as CellPtr ) ;
297
298
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
298
299
}
@@ -314,7 +315,7 @@ impl Window {
314
315
/// to next power of 2 and the magnitude remains the same.
315
316
pub fn set_axes_limits_compute ( & mut self , xrange : & Array , yrange : & Array ,
316
317
zrange : Option < & Array > , exact : bool ) {
317
- let cprops = & Cell { row : self . row , col : self . col , title : String :: from ( "" ) , cmap : self . cmap } ;
318
+ let cprops = & Cell { row : self . row , col : self . col , title : ptr :: null ( ) , cmap : self . cmap } ;
318
319
unsafe {
319
320
let err_val = af_set_axes_limits_compute ( self . handle as WndHandle ,
320
321
xrange. get ( ) as AfArray ,
@@ -343,7 +344,7 @@ impl Window {
343
344
/// are to extracted. If exact is false then the most significant digit is rounded up
344
345
/// to next power of 2 and the magnitude remains the same.
345
346
pub fn set_axes_limits_2d ( & mut self , xmin : f32 , xmax : f32 , ymin : f32 , ymax : f32 , exact : bool ) {
346
- let cprops = & Cell { row : self . row , col : self . col , title : String :: from ( "" ) , cmap : self . cmap } ;
347
+ let cprops = & Cell { row : self . row , col : self . col , title : ptr :: null ( ) , cmap : self . cmap } ;
347
348
unsafe {
348
349
let err_val = af_set_axes_limits_2d ( self . handle as WndHandle , xmin as c_float ,
349
350
xmax as c_float , ymin as c_float , ymax as c_float ,
@@ -370,7 +371,7 @@ impl Window {
370
371
/// to next power of 2 and the magnitude remains the same.
371
372
pub fn set_axes_limits_3d ( & mut self , xmin : f32 , xmax : f32 , ymin : f32 , ymax : f32 ,
372
373
zmin : f32 , zmax : f32 , exact : bool ) {
373
- let cprops = & Cell { row : self . row , col : self . col , title : String :: from ( "" ) , cmap : self . cmap } ;
374
+ let cprops = & Cell { row : self . row , col : self . col , title : ptr :: null ( ) , cmap : self . cmap } ;
374
375
unsafe {
375
376
let err_val = af_set_axes_limits_3d ( self . handle as WndHandle , xmin as c_float ,
376
377
xmax as c_float , ymin as c_float , ymax as c_float ,
@@ -392,7 +393,8 @@ impl Window {
392
393
Some ( s) => s,
393
394
None => format ! ( "Cell({},{}))" , self . col, self . row)
394
395
} ;
395
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
396
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
397
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
396
398
unsafe {
397
399
let err_val = af_draw_image ( self . handle as WndHandle , input. get ( ) as AfArray ,
398
400
cprops as * const Cell as CellPtr ) ;
@@ -413,7 +415,8 @@ impl Window {
413
415
Some ( s) => s,
414
416
None => format ! ( "Cell({},{}))" , self . col, self . row)
415
417
} ;
416
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
418
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
419
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
417
420
unsafe {
418
421
let err_val = af_draw_plot_2d ( self . handle as WndHandle ,
419
422
x. get ( ) as AfArray , y. get ( ) as AfArray ,
@@ -436,7 +439,8 @@ impl Window {
436
439
Some ( s) => s,
437
440
None => format ! ( "Cell({},{}))" , self . col, self . row)
438
441
} ;
439
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
442
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
443
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
440
444
unsafe {
441
445
let err_val = af_draw_plot_3d ( self . handle as WndHandle ,
442
446
x. get ( ) as AfArray , y. get ( ) as AfArray , z. get ( ) as AfArray ,
@@ -457,7 +461,8 @@ impl Window {
457
461
Some ( s) => s,
458
462
None => format ! ( "Cell({},{}))" , self . col, self . row)
459
463
} ;
460
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
464
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
465
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
461
466
unsafe {
462
467
let err_val = af_draw_plot_nd ( self . handle as WndHandle , points. get ( ) as AfArray ,
463
468
cprops as * const Cell as CellPtr ) ;
@@ -479,7 +484,8 @@ impl Window {
479
484
Some ( s) => s,
480
485
None => format ! ( "Cell({},{}))" , self . col, self . row)
481
486
} ;
482
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
487
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
488
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
483
489
unsafe {
484
490
let err_val = af_draw_hist ( self . handle as WndHandle , hst. get ( ) as AfArray ,
485
491
minval as c_double , maxval as c_double ,
@@ -502,7 +508,8 @@ impl Window {
502
508
Some ( s) => s,
503
509
None => format ! ( "Cell({},{}))" , self . col, self . row)
504
510
} ;
505
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
511
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
512
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
506
513
unsafe {
507
514
let err_val = af_draw_surface ( self . handle as WndHandle ,
508
515
xvals. get ( ) as AfArray ,
@@ -528,7 +535,8 @@ impl Window {
528
535
Some ( s) => s,
529
536
None => format ! ( "Cell({},{}))" , self . col, self . row)
530
537
} ;
531
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
538
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
539
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
532
540
unsafe {
533
541
let err_val = af_draw_scatter_2d ( self . handle as WndHandle ,
534
542
xvals. get ( ) as AfArray , yvals. get ( ) as AfArray ,
@@ -553,7 +561,8 @@ impl Window {
553
561
Some ( s) => s,
554
562
None => format ! ( "Cell({},{}))" , self . col, self . row)
555
563
} ;
556
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
564
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
565
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
557
566
unsafe {
558
567
let err_val = af_draw_scatter_3d ( self . handle as WndHandle , xvals. get ( ) as AfArray ,
559
568
yvals. get ( ) as AfArray , zvals. get ( ) as AfArray ,
@@ -575,7 +584,8 @@ impl Window {
575
584
Some ( s) => s,
576
585
None => format ! ( "Cell({},{}))" , self . col, self . row)
577
586
} ;
578
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
587
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
588
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
579
589
unsafe {
580
590
let err_val = af_draw_scatter_nd ( self . handle as WndHandle , vals. get ( ) as AfArray ,
581
591
marker as c_int , cprops as * const Cell as CellPtr ) ;
@@ -599,7 +609,8 @@ impl Window {
599
609
Some ( s) => s,
600
610
None => format ! ( "Cell({},{}))" , self . col, self . row)
601
611
} ;
602
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
612
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
613
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
603
614
unsafe {
604
615
let err_val = af_draw_vector_field_2d ( self . handle as WndHandle ,
605
616
xpnts. get ( ) as AfArray , ypnts. get ( ) as AfArray ,
@@ -628,7 +639,8 @@ impl Window {
628
639
Some ( s) => s,
629
640
None => format ! ( "Cell({},{}))" , self . col, self . row)
630
641
} ;
631
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
642
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
643
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
632
644
unsafe {
633
645
let err_val = af_draw_vector_field_3d ( self . handle as WndHandle , xpnts. get ( ) as AfArray ,
634
646
ypnts. get ( ) as AfArray , zpnts. get ( ) as AfArray ,
@@ -653,7 +665,8 @@ impl Window {
653
665
Some ( s) => s,
654
666
None => format ! ( "Cell({},{}))" , self . col, self . row)
655
667
} ;
656
- let cprops = & Cell { row : self . row , col : self . col , title : tstr. clone ( ) , cmap : self . cmap } ;
668
+ let tstr = CString :: new ( tstr) . unwrap ( ) ;
669
+ let cprops = & Cell { row : self . row , col : self . col , title : tstr. as_ptr ( ) , cmap : self . cmap } ;
657
670
unsafe {
658
671
let err_val = af_draw_vector_field_nd ( self . handle as WndHandle ,
659
672
points. get ( ) as AfArray , directions. get ( ) as AfArray ,
0 commit comments