Commit 58ed34c7 authored by Tim Crawford's avatar Tim Crawford
Browse files

color: Use repr transparent instead of packed



Color is a single field struct with a 4 byte value. It is unnecessary to
pack it, because it will always be aligned. Mark it as transparent so
the compiler can treat it as a plain u32.

Add a test to show that arrays of colors contain no extra padding.

Use core::fmt so Debug is available in all environments.
Signed-off-by: Tim Crawford's avatarTim Crawford <tcrawford@system76.com>
parent 08ac423a
......@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
* Change Rust edition from 2015 to 2018
* Change `Color` from `repr(packed)` to `repr(transparent)`
## 0.3.31
......
// SPDX-License-Identifier: MIT
#[cfg(not(feature = "no_std"))]
use std::fmt;
use core::fmt;
/// A color
#[derive(Copy, Clone)]
#[repr(packed)]
#[repr(transparent)]
pub struct Color {
pub data: u32,
}
......@@ -85,7 +84,6 @@ impl PartialEq for Color {
}
}
#[cfg(not(feature = "no_std"))]
impl fmt::Debug for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:#010X}", { self.data })
......@@ -102,4 +100,13 @@ mod tests {
assert_ne!(Color::rgb(1, 2, 3), Color::rgba(11, 2, 3, 200));
assert_eq!(Color::rgba(1, 2, 3, 200), Color::rgba(1, 2, 3, 200));
}
#[test]
fn alignment() {
assert_eq!(4, core::mem::size_of::<Color>());
assert_eq!(8, core::mem::size_of::<[Color; 2]>());
assert_eq!(12, core::mem::size_of::<[Color; 3]>());
assert_eq!(16, core::mem::size_of::<[Color; 4]>());
assert_eq!(20, core::mem::size_of::<[Color; 5]>());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment