Skip to content

Commit ff5824b

Browse files
committed
Avoid a bit of unsafe in Windows backend
1 parent 89bd260 commit ff5824b

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/win32.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,43 @@ impl Win32Impl {
6161

6262
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
6363
// Create a new bitmap info struct.
64-
let mut bitmap_info: BitmapInfo = unsafe { mem::zeroed() };
65-
66-
bitmap_info.bmi_header.biSize = mem::size_of::<BITMAPINFOHEADER>() as u32;
67-
bitmap_info.bmi_header.biPlanes = 1;
68-
bitmap_info.bmi_header.biBitCount = 32;
69-
bitmap_info.bmi_header.biCompression = BI_BITFIELDS;
70-
bitmap_info.bmi_header.biWidth = width as i32;
71-
bitmap_info.bmi_header.biHeight = -(height as i32);
72-
bitmap_info.bmi_colors[0].rgbRed = 0xff;
73-
bitmap_info.bmi_colors[1].rgbGreen = 0xff;
74-
bitmap_info.bmi_colors[2].rgbBlue = 0xff;
64+
let bmi_header = BITMAPINFOHEADER {
65+
biSize: mem::size_of::<BITMAPINFOHEADER>() as u32,
66+
biWidth: width as i32,
67+
biHeight: -(height as i32),
68+
biPlanes: 1,
69+
biBitCount: 32,
70+
biCompression: BI_BITFIELDS,
71+
biSizeImage: 0,
72+
biXPelsPerMeter: 0,
73+
biYPelsPerMeter: 0,
74+
biClrUsed: 0,
75+
biClrImportant: 0,
76+
};
77+
let zero_quad = RGBQUAD {
78+
rgbBlue: 0,
79+
rgbGreen: 0,
80+
rgbRed: 0,
81+
rgbReserved: 0,
82+
};
83+
let bmi_colors = [
84+
RGBQUAD {
85+
rgbRed: 0xff,
86+
..zero_quad
87+
},
88+
RGBQUAD {
89+
rgbGreen: 0xff,
90+
..zero_quad
91+
},
92+
RGBQUAD {
93+
rgbBlue: 0xff,
94+
..zero_quad
95+
},
96+
];
97+
let bitmap_info = BitmapInfo {
98+
bmi_header,
99+
bmi_colors,
100+
};
75101

76102
// Stretch the bitmap onto the window.
77103
// SAFETY:

0 commit comments

Comments
 (0)