@@ -5,12 +5,49 @@ use plotters_bitmap::BitMapBackend;
5
5
use std:: collections:: VecDeque ;
6
6
use std:: error:: Error ;
7
7
use std:: time:: SystemTime ;
8
+ use std:: borrow:: { Borrow , BorrowMut } ;
8
9
const W : usize = 480 ;
9
10
const H : usize = 320 ;
10
11
11
12
const SAMPLE_RATE : f64 = 10_000.0 ;
12
13
const FREAME_RATE : f64 = 30.0 ;
13
14
15
+ struct BufferWrapper ( Vec < u32 > ) ;
16
+ impl Borrow < [ u8 ] > for BufferWrapper {
17
+ fn borrow ( & self ) -> & [ u8 ] {
18
+ // Safe for alignment: align_of(u8) <= align_of(u32)
19
+ // Safe for cast: u32 can be thought of as being transparent over [u8; 4]
20
+ unsafe {
21
+ std:: slice:: from_raw_parts (
22
+ self . 0 . as_ptr ( ) as * const u8 ,
23
+ self . 0 . len ( ) * 4
24
+ )
25
+ }
26
+ }
27
+ }
28
+ impl BorrowMut < [ u8 ] > for BufferWrapper {
29
+ fn borrow_mut ( & mut self ) -> & mut [ u8 ] {
30
+ // Safe for alignment: align_of(u8) <= align_of(u32)
31
+ // Safe for cast: u32 can be thought of as being transparent over [u8; 4]
32
+ unsafe {
33
+ std:: slice:: from_raw_parts_mut (
34
+ self . 0 . as_mut_ptr ( ) as * mut u8 ,
35
+ self . 0 . len ( ) * 4
36
+ )
37
+ }
38
+ }
39
+ }
40
+ impl Borrow < [ u32 ] > for BufferWrapper {
41
+ fn borrow ( & self ) -> & [ u32 ] {
42
+ self . 0 . as_slice ( )
43
+ }
44
+ }
45
+ impl BorrowMut < [ u32 ] > for BufferWrapper {
46
+ fn borrow_mut ( & mut self ) -> & mut [ u32 ] {
47
+ self . 0 . as_mut_slice ( )
48
+ }
49
+ }
50
+
14
51
fn get_window_title ( fx : f64 , fy : f64 , iphase : f64 ) -> String {
15
52
format ! (
16
53
"x={:.1}Hz, y={:.1}Hz, phase={:.1} +/-=Adjust y 9/0=Adjust x <Esc>=Exit" ,
@@ -19,7 +56,7 @@ fn get_window_title(fx: f64, fy: f64, iphase: f64) -> String {
19
56
}
20
57
21
58
fn main ( ) -> Result < ( ) , Box < dyn Error > > {
22
- let mut buf = vec ! [ 0u8 ; W * H * 4 ] ;
59
+ let mut buf = BufferWrapper ( vec ! [ 0u32 ; W * H ] ) ;
23
60
24
61
let mut fx: f64 = 1.0 ;
25
62
let mut fy: f64 = 1.1 ;
@@ -33,7 +70,7 @@ fn main() -> Result<(), Box<dyn Error>> {
33
70
WindowOptions :: default ( ) ,
34
71
) ?;
35
72
let root =
36
- BitMapBackend :: < BGRXPixel > :: with_buffer_and_format ( & mut buf[ .. ] , ( W as u32 , H as u32 ) ) ?
73
+ BitMapBackend :: < BGRXPixel > :: with_buffer_and_format ( buf. borrow_mut ( ) , ( W as u32 , H as u32 ) ) ?
37
74
. into_drawing_area ( ) ;
38
75
root. fill ( & BLACK ) ?;
39
76
@@ -81,7 +118,7 @@ fn main() -> Result<(), Box<dyn Error>> {
81
118
82
119
if epoch - last_flushed > 1.0 / FREAME_RATE {
83
120
let root = BitMapBackend :: < BGRXPixel > :: with_buffer_and_format (
84
- & mut buf[ .. ] ,
121
+ buf. borrow_mut ( ) ,
85
122
( W as u32 , H as u32 ) ,
86
123
) ?
87
124
. into_drawing_area ( ) ;
@@ -134,8 +171,8 @@ fn main() -> Result<(), Box<dyn Error>> {
134
171
break ;
135
172
}
136
173
}
137
- let buf = unsafe { std :: slice :: from_raw_parts ( & buf [ 0 ] as * const _ as * const _ , H * W ) } ;
138
- window. update_with_buffer ( buf) ?;
174
+
175
+ window. update_with_buffer ( buf. borrow ( ) ) ?;
139
176
last_flushed = epoch;
140
177
}
141
178
0 commit comments