rgb/formats/
gray_alpha.rs

1use crate::formats::gray_a::GrayA;
2use core::ops::{Deref, DerefMut};
3
4#[repr(C)]
5#[cfg_attr(feature = "unstable-experimental", deprecated(note = "renamed to GrayA"))]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
8#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
9/// A pixel for grayscale value + alpha components (rgb crate v0.8)
10///
11/// Through a `Deref` hack it renames the fields from `.0` and `.1`
12/// to `.v` (value) and `.a` (alpha)
13#[allow(non_camel_case_types)]
14pub struct GrayAlpha_v08<T, A = T>(
15    /// Grayscale Component
16    ///
17    /// This field has been renamed to `.v`
18    #[deprecated(note = "Please use the .v field instaed (it's available through the magic of Deref to GrayA type)")]
19    pub T,
20    /// Alpha Component. This field has been renamed to `.a`.
21    #[deprecated(note = "Please use the .a field instead (it's available through the magic of Deref to GrayA type)")]
22    pub A,
23);
24
25impl<T: Copy> GrayAlpha_v08<T> {
26    /// Reads the `.v` field
27    ///
28    /// Please use the `.v` field directly whenever possible. This function isn't necessary, and exists only to ease migration between major versions of the RGB crate.
29    #[allow(deprecated)]
30    pub fn value(self) -> T {
31        self.0
32    }
33
34    /// Exposes the `.v` field for writing
35    ///
36    /// Please use the `.v` field directly whenever possible.  This function isn't necessary, and exists only to ease migration between major versions of the RGB crate.
37    #[allow(deprecated)]
38    pub fn value_mut(&mut self) -> &mut T {
39        &mut self.0
40    }
41}
42
43impl<T, A> Deref for GrayAlpha_v08<T, A> {
44    type Target = GrayA<T, A>;
45
46    /// A trick that allows using `.v` and `.a` on the old `GrayAlpha` type.
47    fn deref(&self) -> &GrayA<T, A> {
48        unsafe {
49            &*(self as *const Self).cast::<GrayA::<T, A>>()
50        }
51    }
52}
53
54impl<T, A> DerefMut for GrayAlpha_v08<T, A> {
55    /// A trick that allows using `.v` and `.a` on the old `GrayAlpha` type.
56    fn deref_mut(&mut self) -> &mut GrayA<T, A> {
57        unsafe {
58            &mut *(self as *mut Self).cast::<GrayA::<T, A>>()
59        }
60    }
61}
62
63#[test]
64#[allow(deprecated)]
65fn swizzle() {
66    let mut g = GrayAlpha_v08(10u8, 20u8);
67    assert_eq!(10, g.v);
68    assert_eq!(20, g.a);
69    g.a = 7;
70    assert_eq!(10, g.v);
71    assert_eq!(7, g.1);
72}