1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use core::convert::Infallible;

use super::{FieldCopyAccess, PrimitiveField};
use crate::endianness::Endianness;
use crate::fields::primitive::view::FieldView;
use crate::fields::{Field, StorageIntoFieldView, StorageToFieldView};

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<(), E, OFFSET_> {
    /// See [FieldCopyAccess::ReadError]
    type ReadError = Infallible;
    /// See [FieldCopyAccess::WriteError]
    type WriteError = Infallible;
    /// See [FieldCopyAccess::HighLevelType]
    type HighLevelType = ();

    doc_comment::doc_comment! {
        concat! {"
                'Read' the `", stringify!(()), "`-typed field from a given data region, assuming the defined layout, using the [Field] API.

                # Example:

                ```
                use binary_layout::prelude::*;

                binary_layout!(my_layout, LittleEndian, {
                    //... other fields ...
                    some_zst_field: ", stringify!(()), "
                    //... other fields ...
                });

                fn func(storage_data: &[u8]) {
                    let read: ", stringify!(()), " = my_layout::some_zst_field::try_read(storage_data).unwrap();
                    read
                }
                ```

                In reality, this method doesn't do any work; `",
                stringify!(()), "` is a zero-sized type, so there's no work to
                do. This implementation exists solely to make writing derive
                macros simpler.
                "},
        #[inline(always)]
        #[allow(clippy::unused_unit)] // I don't want to remove this as it's part of the trait.
        fn try_read(_storage: &[u8]) -> Result<(), Infallible> {
            Ok(())
        }
    }

    doc_comment::doc_comment! {
        concat! {"
                'Write' the `", stringify!(()), "`-typed field to a given data region, assuming the defined layout, using the [Field] API.

                # Example:

                ```
                use binary_layout::prelude::*;

                binary_layout!(my_layout, LittleEndian, {
                    //... other fields ...
                    some_zst_field: ", stringify!(()), "
                    //... other fields ...
                });

                fn func(storage_data: &mut [u8]) {
                    my_layout::some_zst_field::try_write(storage_data, ()).unwrap();
                }
                ```

                # WARNING

                In reality, this method doesn't do any work; `",
                stringify!(()), "` is a zero-sized type, so there's no work to
                do. This implementation exists solely to make writing derive
                macros simpler.
                "},
        #[inline(always)]
        #[allow(clippy::unused_unit)] // I don't want to remove this as it's part of the trait.
        fn try_write(_storage: &mut [u8], _value: ()) -> Result<(), Infallible> {
            Ok(())
        }
    }
}

impl_field_traits!(());

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use crate::PrimitiveField;

    macro_rules! test_unit_copy_access {
        ($endian:ident, $endian_type:ty) => {
            $crate::internal::paste! {
                #[test]
                fn [<test_unit_ $endian endian_metadata>]() {
                    type Field1 = PrimitiveField<(), $endian_type, 5>;
                    type Field2 = PrimitiveField<(), $endian_type, 123>;

                    assert_eq!(Some(0), Field1::SIZE);
                    assert_eq!(5, Field1::OFFSET);
                    assert_eq!(Some(0), Field2::SIZE);
                    assert_eq!(123, Field2::OFFSET);
                }

                #[allow(clippy::unit_cmp)]
                #[test]
                fn [<test_unit_ $endian endian_fieldapi>]() {
                    let mut storage = [0; 1024];

                    type Field1 = PrimitiveField<(), $endian_type, 5>;
                    type Field2 = PrimitiveField<(), $endian_type, 123>;

                    Field1::write(&mut storage, ());
                    Field2::write(&mut storage, ());

                    assert_eq!((), Field1::read(&storage));
                    assert_eq!((), Field2::read(&storage));

                    // Zero-sized types do not mutate the storage, so it should remain
                    // unchanged for all of time.
                    assert_eq!(storage, [0; 1024]);
                }

                #[allow(clippy::unit_cmp)]
                #[test]
                fn [<test_unit_ $endian endian_viewapi>]() {
                    binary_layout!(layout, $endian_type, {
                        field1: (),
                        field2: (),
                    });
                    let mut storage = [0; 1024];
                    let mut view = layout::View::new(&mut storage);

                    view.field1_mut().write(());
                    view.field2_mut().write(());

                    assert_eq!((), view.field1().read());
                    assert_eq!((), view.field2().read());

                    // Zero-sized types do not mutate the storage, so it should remain
                    // unchanged for all of time.
                    assert_eq!(storage, [0; 1024]);
                }
            }
        };
    }

    test_unit_copy_access!(little, LittleEndian);
    test_unit_copy_access!(big, BigEndian);
    test_unit_copy_access!(native, NativeEndian);
}