forma/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

pub fn clear_buffer(buffer: &mut [u8], clear_color: [u8; 4]) {
    for color in buffer.chunks_exact_mut(4) {
        color.copy_from_slice(&clear_color);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const RED: [u8; 4] = [0xFF, 0x00, 0x00, 0xFF];
    const GREEN: [u8; 4] = [0x00, 0xFF, 0x00, 0xFF];

    #[test]
    fn clear_to_red() {
        let mut buffer = [GREEN; 3].concat();
        clear_buffer(&mut buffer, RED);
        assert_eq!(buffer, [RED, RED, RED].concat());
    }
}