process-image 0.2.3

Zero-cost abstraction for convenient access to process image tags.
Documentation
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
//! Macros for accessing _tags_ in a _process image_.
//!
//! # What's this?
//! In industrial controls, namely PLC (programmable logic controller) programming, data is usually
//! stored in _process images_. A process image is nothing more than a chunk of memory that
//! contains a snapshot of all values known about the system that is being controlled.
//!
//! This data is commonly split into a `PII` (process image of inputs) and a `PIQ` (process image
//! of outputs). The `PII` contains things like sensor data, while the `PIQ` is filled with the
//! states of solenoid valves or speed setpoints for motors, for example.
//!
//! IEC 61131-3 defines a syntax for addressing data in process images.  It works like this:
//!
//! ```text
//!  +------- The process image that is referenced: I=inputs, Q=outputs, M=internal memory
//!  |+------ The type of the tag: X=bit, B=byte, W=word, D=double word, L=long word
//!  || +---- The byte offset inside the process image
//!  || |  +- The bit offset in the addressed byte
//!  vv v  v
//! %IX100.4 = bit 4 in byte address 100
//! %IB8     = byte at address 8
//! %IW16    = word starting at address 16
//! ```
//!
//! | Specifier | Rust | Type |
//! | --- | --- | --- |
//! | `X` (may be omitted) | `bool` | Boolean Bit |
//! | `B` | `u8` | Byte |
//! | `W` | `u16` | Word |
//! | `D` | `u32` | Double Word |
//! | `L` | `u64` | Long Word |
//!
//! The meaning of each bit and byte is defined by the hardware configuration of the PLC and the
//! equipment connected to it.  Usually the input and output addresses are also referenced in
//! electrical wiring diagrams of the control system.
//!
//! Because directly fiddling with bits and bytes in a chunk of memory to fetch and set process
//! data is inconvenient, _tags_ are assigned to the addresses in a process image, giving the data
//! symbolic names.  This crate provides macros to assign such tags to a memory buffer in rust.
//!
//! # How To
//! The [`tag!()`][`tag`] and [`tag_mut!()`][`tag`] macros are used to immediately address a value
//! inside a buffer slice.  This is meant for fetching data by address directly.
//!
//! The [`process_image!()`][`process_image`] and [`process_image_owned!()`][`process_image_owned`]
//! macros are used to build more permanent definitions of the data inside a process image.  These
//! macros generate struct-wrappers around a buffer with methods to access the individual tags.
//! Please see the respective documentation for details.
//!
//! The syntax for addresses is slightly different from the IEC 61131-3 syntax, to stay within the
//! bounds of rust declarative macros.  Here are a few examples that should be self-explanatory:
//!
//! ```
//! // Process Image of Inputs
//! let pii = [0x00; 16];
//!
//! let b1: bool = process_image::tag!(&pii, X, 0, 0); // %IX0.0
//! let b2: bool = process_image::tag!(&pii, 0, 1);    // %IX0.1
//! let by: u8   = process_image::tag!(&pii, B, 1);    // %IB1
//! let w: u16   = process_image::tag!(&pii, W, 2);    // %IW2
//! let d: u32   = process_image::tag!(&pii, D, 4);    // %ID4
//! let l: u64   = process_image::tag!(&pii, L, 8);    // %IL8
//! ```
//!
//! # Endianness
//! All data is accessed in big-endian (MSB-first) byte order.
//!
//! # Alignment
//! By default, addresses of _words, double words,_ and _long words_ must be aligned to the size of
//! the data type.  Unaligned addresses will lead to a panic at runtime.
//!
//! | Type | Alignment |
//! | --- | --- |
//! | Boolean Bit | 1 (none) |
//! | Byte | 1 (none) |
//! | Word | 2 |
//! | Double Word | 4 |
//! | Long Word | 8 |
//!
//! However, there are some situations where this behavior is undesired.  In some control systems,
//! the process image is constructed without alignment.  To cater to such uses,
//! alignment-enforcement can be disabled globally with the `allow_unaligned_tags` crate feature.
//! Do note that this will disable alignment globally for all users of `process-image` in the
//! crate dependency-graph.
//!
//! In the future, alignment-enforcement might be dropped entirely.
#![cfg_attr(not(test), no_std)]

mod access;
pub use access::{BitMut, DWordMut, LWordMut, WordMut};

#[cfg(feature = "allow_unaligned_tags")]
#[doc(hidden)]
#[macro_export]
macro_rules! alignment_assert {
    ($align:literal, $addr:expr) => {};
}

#[cfg(not(feature = "allow_unaligned_tags"))]
#[doc(hidden)]
#[macro_export]
macro_rules! alignment_assert {
    (2, $addr:expr) => {
        assert!($addr % 2 == 0, "Word address must be divisible by 2");
    };
    (4, $addr:expr) => {
        assert!($addr % 4 == 0, "Double word address must be divisible by 4");
    };
    (8, $addr:expr) => {
        assert!($addr % 8 == 0, "Long word address must be divisible by 8");
    };
}

/// Read tag values from a process image with absolute addressing.
///
/// Addresses must be aligned to the size of the datatype (i.e. word=2, dword=4, lword=8).
///
/// Multi-byte datatypes are always accessed in big-endian order.
///
/// # Example
/// ```
/// let pi = [0x00; 16];
///
/// // Bit access
/// let b1: bool = process_image::tag!(&pi, X, 0, 0);   // %MX0.0
/// let b2: bool = process_image::tag!(&pi, 0, 1);      // %MX0.1
///
/// // Byte access
/// let by: u8 = process_image::tag!(&pi, B, 1);        // %MB1
///
/// // Word access
/// let w: u16 = process_image::tag!(&pi, W, 2);        // %MW2
///
/// // Double word access
/// let d: u32 = process_image::tag!(&pi, D, 4);        // %MD4
///
/// // Long word access
/// let l: u64 = process_image::tag!(&pi, L, 8);        // %ML8
/// ```
#[macro_export]
macro_rules! tag {
    ($buf:expr, X, $addr1:expr, $addr2:expr) => {{
        let buffer: &[u8] = $buf;
        buffer[$addr1] & (1 << $addr2) != 0
    }};
    ($buf:expr, B, $addr:expr) => {{
        let buffer: &[u8] = $buf;
        buffer[$addr]
    }};
    ($buf:expr, W, $addr:expr) => {{
        let buffer: &[u8] = $buf;
        $crate::alignment_assert!(2, $addr);
        u16::from_be_bytes(buffer[$addr..$addr + 2].try_into().unwrap())
    }};
    ($buf:expr, D, $addr:expr) => {{
        let buffer: &[u8] = $buf;
        $crate::alignment_assert!(4, $addr);
        u32::from_be_bytes(buffer[$addr..$addr + 4].try_into().unwrap())
    }};
    ($buf:expr, L, $addr:expr) => {{
        let buffer: &[u8] = $buf;
        $crate::alignment_assert!(8, $addr);
        u64::from_be_bytes(buffer[$addr..$addr + 8].try_into().unwrap())
    }};
    ($buf:expr, $addr1:expr, $addr2:expr) => {{
        let buffer: &[u8] = $buf;
        buffer[$addr1] & (1 << $addr2) != 0
    }};
}

/// Mutable access to tag values from a process image with absolute addressing.
///
/// Addresses must be aligned to the size of the datatype (i.e. word=2, dword=4, lword=8).
///
/// Multi-byte datatypes are always accessed in big-endian order.
///
/// # Example
/// ```
/// let mut pi = [0x00; 16];
///
/// // Bit access
/// *process_image::tag_mut!(&mut pi, X, 0, 0) = true;  // %MX0.0
/// *process_image::tag_mut!(&mut pi, 0, 1) = true;     // %MX0.1
///
/// // Byte access
/// *process_image::tag_mut!(&mut pi, B, 1) = 42u8;     // %MB1
///
/// // Word access
/// *process_image::tag_mut!(&mut pi, W, 2) = 1337u16;  // %MW2
///
/// // Double word access
/// *process_image::tag_mut!(&mut pi, D, 4) = 0xdeadbeef; // %MD4
///
/// // Long word access
/// *process_image::tag_mut!(&mut pi, L, 8) = 1;        // %ML8
/// ```
#[macro_export]
macro_rules! tag_mut {
    ($buf:expr, X, $addr1:expr, $addr2:expr) => {{
        let buffer: &mut [u8] = $buf;
        $crate::BitMut::new(&mut buffer[$addr1], $addr2)
    }};
    ($buf:expr, B, $addr:expr) => {{
        let buffer: &mut [u8] = $buf;
        &mut buffer[$addr]
    }};
    ($buf:expr, W, $addr:expr) => {{
        let buffer: &mut [u8] = $buf;
        $crate::alignment_assert!(2, $addr);
        $crate::WordMut::new((&mut buffer[$addr..$addr + 2]).try_into().unwrap())
    }};
    ($buf:expr, D, $addr:expr) => {{
        let buffer: &mut [u8] = $buf;
        $crate::alignment_assert!(4, $addr);
        $crate::DWordMut::new((&mut buffer[$addr..$addr + 4]).try_into().unwrap())
    }};
    ($buf:expr, L, $addr:expr) => {{
        let buffer: &mut [u8] = $buf;
        $crate::alignment_assert!(8, $addr);
        $crate::LWordMut::new((&mut buffer[$addr..$addr + 8]).try_into().unwrap())
    }};
    ($buf:expr, $addr1:expr, $addr2:expr) => {{
        let buffer: &mut [u8] = $buf;
        $crate::BitMut::new(&mut buffer[$addr1], $addr2)
    }};
}

#[doc(hidden)]
#[macro_export]
macro_rules! tag_method {
    ($vis:vis, $name:ident, mut, X, $addr1:literal, $addr2:literal) => {
        #[inline(always)]
        $vis fn $name(&mut self) -> $crate::BitMut<'_> {
            $crate::BitMut::new(&mut self.buf[$addr1], $addr2)
        }
    };
    ($vis:vis, $name:ident, mut, B, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&mut self) -> &mut u8 {
            &mut self.buf[$addr]
        }
    };
    ($vis:vis, $name:ident, mut, W, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&mut self) -> $crate::WordMut<'_> {
            $crate::alignment_assert!(2, $addr);
            $crate::WordMut::new((&mut self.buf[$addr..$addr + 2]).try_into().unwrap())
        }
    };
    ($vis:vis, $name:ident, mut, D, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&mut self) -> $crate::DWordMut<'_> {
            $crate::alignment_assert!(4, $addr);
            $crate::DWordMut::new((&mut self.buf[$addr..$addr + 4]).try_into().unwrap())
        }
    };
    ($vis:vis, $name:ident, mut, L, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&mut self) -> $crate::LWordMut<'_> {
            $crate::alignment_assert!(8, $addr);
            $crate::LWordMut::new((&mut self.buf[$addr..$addr + 8]).try_into().unwrap())
        }
    };
    ($vis:vis, $name:ident, mut, $addr1:literal, $addr2:literal) => {
        #[inline(always)]
        $vis fn $name(&mut self) -> $crate::BitMut<'_> {
            $crate::BitMut::new(&mut self.buf[$addr1], $addr2)
        }
    };
    ($vis:vis, $name:ident, const, X, $addr1:literal, $addr2:literal) => {
        #[inline(always)]
        $vis fn $name(&self) -> bool {
            self.buf[$addr1] & (1 << $addr2) != 0
        }
    };
    ($vis:vis, $name:ident, const, B, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&self) -> u8 {
            self.buf[$addr]
        }
    };
    ($vis:vis, $name:ident, const, W, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&self) -> u16 {
            $crate::alignment_assert!(2, $addr);
            u16::from_be_bytes(self.buf[$addr..$addr + 2].try_into().unwrap())
        }
    };
    ($vis:vis, $name:ident, const, D, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&self) -> u32 {
            $crate::alignment_assert!(4, $addr);
            u32::from_be_bytes(self.buf[$addr..$addr + 4].try_into().unwrap())
        }
    };
    ($vis:vis, $name:ident, const, L, $addr:literal) => {
        #[inline(always)]
        $vis fn $name(&self) -> u64 {
            $crate::alignment_assert!(8, $addr);
            u64::from_be_bytes(self.buf[$addr..$addr + 8].try_into().unwrap())
        }
    };
    ($vis:vis, $name:ident, const, $addr1:literal, $addr2:literal) => {
        #[inline(always)]
        $vis fn $name(&self) -> bool {
            self.buf[$addr1] & (1 << $addr2) != 0
        }
    };
}

/// Build tag table for symbolic access into a process image buffer.
///
/// - You will get two structs, one for mutable and one for immutable access (or just one of them,
///   if you want).
/// - The process image has a fixed size which is always enforced.
/// - The tag addresses are in the format described in the [`tag!()`][`tag`] macro.
/// - The process image buffer is referenced, for owned buffers,
///   see [`process_image_owned!{}`][`process_image_owned`].
///
/// ## Example
/// ```
/// process_image::process_image! {
///     //                                      +-- Size of the process image in bytes
///     //                                      V
///     pub struct PiExample, mut PiExampleMut: 16 {
///         //  +-- Tag Name  +-- Absolute Address
///         //  V             V
///         pub sensor_left: (X, 0, 0),     // %MX0.0
///         pub sensor_right: (X, 0, 1),    // %MX0.1
///         pub temperature: (D, 4),        // %MD4
///         pub setpoint: (W, 2),           // %MW2
///     }
/// }
///
/// let mut pi_buf = [0x00; 16];
/// let pi = PiExample::from(&pi_buf);
///
/// dbg!(pi.sensor_left());
/// dbg!(pi.sensor_left());
///
/// // You need to use try_from() when using a slice.  The unwrap() will panic when the size of the
/// // slice does not match the size of the process image.
/// let pi_slice = &pi_buf[..];
/// let pi = PiExample::try_from(pi_slice).unwrap();
///
/// // Mutable access:
/// let pi_slice_mut = &mut pi_buf[..];
/// let mut pi = PiExampleMut::try_from(pi_slice_mut).unwrap();
/// *pi.temperature() = 1234;
/// *pi.setpoint() = 72;
/// *pi.sensor_left() = false;
/// ```
///
/// As mentioned above, you can also generate just the mutable or just the immutable version:
///
/// ```
/// # let buffer_in = [0x00u8; 16];
/// # let mut buffer_out = [0x00u8; 8];
/// process_image::process_image! {
///     pub struct PiInputs: 16 {
///         pub sensor_left: (X, 0, 0),     // %IX0.0
///         pub sensor_right: (X, 0, 1),    // %IX0.1
///         pub temperature: (D, 12),       // %ID12
///     }
/// }
/// process_image::process_image! {
///     pub struct mut PiOutputs: 8 {
///         pub indicator_green: (X, 1, 0), // %QX1.0
///         pub indicator_red: (X, 1, 2),   // %QX1.2
///         pub setpoint: (W, 2),           // %QW2
///     }
/// }
///
/// let inp = PiInputs::try_from(&buffer_in).unwrap();
/// let mut out = PiOutputs::try_from(&mut buffer_out).unwrap();
///
/// let left_or_right = inp.sensor_left() || inp.sensor_right();
/// *out.indicator_green() = !left_or_right;
/// *out.indicator_red() = left_or_right;
/// ```
#[macro_export]
macro_rules! process_image {
    (
        $( #[$meta:meta] )*
        $vis:vis struct $ProcessImage:ident, mut $ProcessImageMut:ident: $SIZE:literal {
            $(
                $( #[$field_meta:meta] )*
                $field_vis:vis $field_name:ident: ($($tag:tt)+)
            ),*
            $(,)?
        }
    ) => {
        $( #[$meta] )*
        $vis struct $ProcessImage<'a> {
            buf: &'a [u8; $SIZE],
        }

        impl<'a> $ProcessImage<'a> {
            $(
                $( #[$field_meta] )*
                $crate::tag_method!($vis, $field_name, const, $($tag)+);
            )*
        }

        impl<'a> ::core::convert::From<&'a [u8; $SIZE]> for $ProcessImage<'a> {
            #[inline(always)]
            fn from(buf: &'a [u8; $SIZE]) -> Self {
                Self { buf }
            }
        }

        impl<'a> ::core::convert::TryFrom<&'a [u8]> for $ProcessImage<'a> {
            type Error = ::core::array::TryFromSliceError;

            #[inline(always)]
            fn try_from(buf: &'a [u8]) -> Result<Self, Self::Error> {
                buf.try_into().map(|buf| Self { buf })
            }
        }

        impl<'a> ::core::convert::AsRef<[u8]> for $ProcessImage<'a> {
            #[inline(always)]
            fn as_ref(&self) -> &[u8] {
                &self.buf[..]
            }
        }

        impl<'a> ::core::fmt::Debug for $ProcessImage<'a> {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                f.debug_struct(::core::stringify!($ProcessImage))
                    $(
                    .field(::core::stringify!($field_name), &self.$field_name())
                    )*
                    .finish()
            }
        }

        $( #[$meta] )*
        $vis struct $ProcessImageMut<'a> {
            buf: &'a mut [u8; $SIZE],
        }

        impl<'a> ::core::convert::From<&'a mut [u8; $SIZE]> for $ProcessImageMut<'a> {
            #[inline(always)]
            fn from(buf: &'a mut [u8; $SIZE]) -> Self {
                Self { buf }
            }
        }

        impl<'a> ::core::convert::TryFrom<&'a mut [u8]> for $ProcessImageMut<'a> {
            type Error = ::core::array::TryFromSliceError;

            #[inline(always)]
            fn try_from(buf: &'a mut [u8]) -> Result<Self, Self::Error> {
                buf.try_into().map(|buf| Self { buf })
            }
        }

        impl<'a> ::core::convert::AsRef<[u8]> for $ProcessImageMut<'a> {
            #[inline(always)]
            fn as_ref(&self) -> &[u8] {
                &self.buf[..]
            }
        }

        impl<'a> ::core::convert::AsMut<[u8]> for $ProcessImageMut<'a> {
            #[inline(always)]
            fn as_mut(&mut self) -> &mut [u8] {
                &mut self.buf[..]
            }
        }

        impl<'a> ::core::fmt::Debug for $ProcessImageMut<'a> {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                let pi = $ProcessImage::from(&*self.buf);
                f.debug_struct(::core::stringify!($ProcessImageMut))
                    $(
                    .field(::core::stringify!($field_name), &pi.$field_name())
                    )*
                    .finish()
            }
        }

        impl<'a> $ProcessImageMut<'a> {
            $(
                $( #[$field_meta] )*
                $crate::tag_method!($vis, $field_name, mut, $($tag)+);
            )*
        }
    };
    (
        $( #[$meta:meta] )*
        $vis:vis struct mut $ProcessImageMut:ident: $SIZE:literal {
            $(
                $( #[$field_meta:meta] )*
                $field_vis:vis $field_name:ident: ($($tag:tt)+)
            ),*
            $(,)?
        }
    ) => {
        $( #[$meta] )*
        $vis struct $ProcessImageMut<'a> {
            buf: &'a mut [u8; $SIZE],
        }

        impl<'a> ::core::convert::From<&'a mut [u8; $SIZE]> for $ProcessImageMut<'a> {
            #[inline(always)]
            fn from(buf: &'a mut [u8; $SIZE]) -> Self {
                Self { buf }
            }
        }

        impl<'a> ::core::convert::TryFrom<&'a mut [u8]> for $ProcessImageMut<'a> {
            type Error = ::core::array::TryFromSliceError;

            #[inline(always)]
            fn try_from(buf: &'a mut [u8]) -> Result<Self, Self::Error> {
                buf.try_into().map(|buf| Self { buf })
            }
        }

        impl<'a> ::core::convert::AsRef<[u8]> for $ProcessImageMut<'a> {
            #[inline(always)]
            fn as_ref(&self) -> &[u8] {
                &self.buf[..]
            }
        }

        impl<'a> ::core::convert::AsMut<[u8]> for $ProcessImageMut<'a> {
            #[inline(always)]
            fn as_mut(&mut self) -> &mut [u8] {
                &mut self.buf[..]
            }
        }

        impl<'a> $ProcessImageMut<'a> {
            $(
                $( #[$field_meta] )*
                $crate::tag_method!($vis, $field_name, mut, $($tag)+);
            )*
        }
    };
    (
        $( #[$meta:meta] )*
        $vis:vis struct $ProcessImage:ident: $SIZE:literal {
            $(
                $( #[$field_meta:meta] )*
                $field_vis:vis $field_name:ident: ($($tag:tt)+)
            ),*
            $(,)?
        }
    ) => {
        $( #[$meta] )*
        $vis struct $ProcessImage<'a> {
            buf: &'a [u8; $SIZE],
        }

        impl<'a> $ProcessImage<'a> {
            $(
                $( #[$field_meta] )*
                $crate::tag_method!($vis, $field_name, const, $($tag)+);
            )*
        }

        impl<'a> ::core::convert::From<&'a [u8; $SIZE]> for $ProcessImage<'a> {
            #[inline(always)]
            fn from(buf: &'a [u8; $SIZE]) -> Self {
                Self { buf }
            }
        }

        impl<'a> ::core::convert::TryFrom<&'a [u8]> for $ProcessImage<'a> {
            type Error = ::core::array::TryFromSliceError;

            #[inline(always)]
            fn try_from(buf: &'a [u8]) -> Result<Self, Self::Error> {
                buf.try_into().map(|buf| Self { buf })
            }
        }

        impl<'a> ::core::convert::AsRef<[u8]> for $ProcessImage<'a> {
            #[inline(always)]
            fn as_ref(&self) -> &[u8] {
                &self.buf[..]
            }
        }

        impl<'a> ::core::fmt::Debug for $ProcessImage<'a> {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                f.debug_struct(::core::stringify!($ProcessImage))
                    $(
                    .field(::core::stringify!($field_name), &self.$field_name())
                    )*
                    .finish()
            }
        }
    };
}

/// Build tag table for symbolic access into an _owned_ process image buffer.
///
/// - This macro generates a struct that owns the process image buffer.  For a referenced variant,
///   see [`process_image!{}`][`process_image`].
/// - The method immediately available on the struct provide immutable access.  For mutable access,
///   call `.as_mut()` to get a mutable accessor struct.  The methods on that one then provide mutable
///   access.
/// - The tag addresses are in the format described in the [`tag!()`][`tag`] macro.
/// - You can construct a `process_image_owned` from zeros (`new_zeroed()`) or from a
///   pre-initialized buffer by using `From<[u8; SIZE]` or `TryFrom<&[u8]>`.
///
/// ## Example
/// ```
/// process_image::process_image_owned! {
///     //                                      +-- Size of the process image in bytes
///     //                                      V
///     pub struct PiExampleOwned, mut PiExampleMut: 16 {
///         //  +-- Tag Name  +-- Absolute Address
///         //  V             V
///         pub sensor_left:  (X, 0, 0),    // %MX0.0
///         pub sensor_right: (X, 0, 1),    // %MX0.1
///         pub temperature:  (D, 4),       // %MD4
///         pub setpoint:     (W, 2),       // %MW2
///     }
/// }
///
/// let pi = PiExampleOwned::new_zeroed();
///
/// dbg!(pi.sensor_left());
/// dbg!(pi.sensor_left());
///
/// // You need to use try_from() when using a slice.  The unwrap() will panic when the size of the
/// // slice does not match the size of the process image.
/// let pi_buf = [0u8; 16];
/// let pi_slice = &pi_buf[..];
/// let mut pi = PiExampleOwned::try_from(pi_slice).unwrap();
///
/// // Mutable access:
/// *pi.as_mut().temperature() = 1234;
/// *pi.as_mut().setpoint() = 72;
/// *pi.as_mut().sensor_left() = false;
/// ```
#[macro_export]
macro_rules! process_image_owned {
    (
        $( #[$meta:meta] )*
        $vis:vis struct $ProcessImage:ident, mut $ProcessImageMut:ident: $SIZE:literal {
            $(
                $( #[$field_meta:meta] )*
                $field_vis:vis $field_name:ident: ($($tag:tt)+)
            ),*
            $(,)?
        }
    ) => {
        $( #[$meta] )*
        $vis struct $ProcessImage {
            buf: [u8; $SIZE],
        }

        impl $ProcessImage {
            #[allow(dead_code)]
            #[inline(always)]
            pub fn new_zeroed() -> Self {
                Self {
                    buf: [0u8; $SIZE],
                }
            }

            #[allow(dead_code)]
            #[inline(always)]
            pub fn as_mut(&mut self) -> $ProcessImageMut {
                $ProcessImageMut::from(&mut self.buf)
            }

            #[allow(dead_code)]
            #[inline(always)]
            pub fn as_slice(&self) -> &[u8] {
                &self.buf[..]
            }

            #[allow(dead_code)]
            #[inline(always)]
            pub fn as_slice_mut(&mut self) -> &mut [u8] {
                &mut self.buf[..]
            }

            $(
                $( #[$field_meta] )*
                $crate::tag_method!($vis, $field_name, const, $($tag)+);
            )*
        }

        impl ::core::convert::From<&[u8; $SIZE]> for $ProcessImage {
            #[inline(always)]
            fn from(buf_in: &[u8; $SIZE]) -> Self {
                let mut buf = [0u8; $SIZE];
                buf.copy_from_slice(buf_in);
                Self { buf }
            }
        }

        impl ::core::convert::TryFrom<&[u8]> for $ProcessImage {
            type Error = ::core::array::TryFromSliceError;

            #[inline(always)]
            fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
                buf.try_into().map(|buf: &[u8; $SIZE]| Self { buf: buf.clone() })
            }
        }

        impl ::core::convert::AsRef<[u8]> for $ProcessImage {
            #[inline(always)]
            fn as_ref(&self) -> &[u8] {
                &self.buf[..]
            }
        }

        impl ::core::convert::AsMut<[u8]> for $ProcessImage {
            #[inline(always)]
            fn as_mut(&mut self) -> &mut [u8] {
                &mut self.buf[..]
            }
        }

        impl ::core::fmt::Debug for $ProcessImage {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                f.debug_struct(::core::stringify!($ProcessImage))
                    $(
                    .field(::core::stringify!($field_name), &self.$field_name())
                    )*
                    .finish()
            }
        }

        $crate::process_image! {
            $(#[$meta])*
            $vis struct mut $ProcessImageMut: $SIZE {
                $(
                    $(#[$field_meta])*
                    $field_vis $field_name: ($($tag)+),
                )*
            }
        }
    };
}

#[cfg(test)]
mod tests {
    #[test]
    fn tag_macro_smoke1() {
        let mut pi = [0x55, 0xaa, 0x00, 0xff];

        assert_eq!(tag!(&pi, X, 2, 0), false);
        assert_eq!(tag!(&pi, 3, 0), true);
        assert_eq!(tag!(&pi, X, 0, 0), true);
        assert_eq!(tag!(&pi, 0, 1), false);

        *tag_mut!(&mut pi, X, 0, 0) = false;
        assert_eq!(tag!(&pi, X, 0, 0), false);

        assert_eq!(tag!(&pi, B, 2), 0x00);
        *tag_mut!(&mut pi, X, 2, 7) = true;
        assert_eq!(tag!(&pi, B, 2), 0x80);

        assert_eq!(tag!(&pi, W, 2), 0x80ff);
        assert_eq!(*tag_mut!(&mut pi, W, 2), 0x80ff);
        let addr = 2;
        assert_eq!(*tag_mut!(&mut pi, W, addr), 0x80ff);
        assert_eq!(tag!(&pi, D, 0), 0x54aa80ff);
        assert_eq!(*tag_mut!(&mut pi, D, 0), 0x54aa80ff);

        *tag_mut!(&mut pi, W, 2) = 0xbeef;
        assert_eq!(tag!(&pi, W, 2), 0xbeef);
    }

    process_image! {
        pub struct TestPi, mut TestPiMut: 4 {
            pub btn_start: (X, 1, 0),
            pub btn_stop: (1, 1),
            pub btn_reset: (X, 1, 2),
            pub speed: (W, 2),
            pub length: (B, 0),
        }
    }

    #[test]
    fn pi_macro_smoke1() {
        let mut pi_buffer = [128, 0x55, 0xde, 0xad];

        let pi = TestPi::try_from(&pi_buffer).unwrap();
        assert_eq!(pi.btn_start(), true);
        assert_eq!(pi.btn_stop(), false);
        assert_eq!(pi.btn_reset(), true);
        assert_eq!(pi.speed(), 0xdead);
        assert_eq!(pi.length(), 128);

        let mut pi = TestPiMut::try_from(&mut pi_buffer).unwrap();
        assert_eq!(*pi.btn_start(), true);
        assert_eq!(*pi.btn_stop(), false);
        assert_eq!(*pi.btn_reset(), true);
        assert_eq!(*pi.speed(), 0xdead);
        assert_eq!(*pi.length(), 128);

        *pi.btn_start() = false;
        *pi.btn_stop() = true;

        *pi.speed() = 1337;
        *pi.length() = 1;

        let pi = TestPi::try_from(&pi_buffer).unwrap();
        assert_eq!(pi.btn_start(), false);
        assert_eq!(pi.btn_stop(), true);
        assert_eq!(pi.btn_reset(), true);
        assert_eq!(pi.speed(), 1337);
        assert_eq!(pi.length(), 1);

        assert_eq!(tag!(&pi_buffer, 1, 0), false);
        assert_eq!(tag!(&pi_buffer, W, 2), 1337);
        assert_eq!(tag!(&pi_buffer, B, 0), 1);
    }

    process_image_owned! {
        pub struct TestPiOwned, mut TestPiOwnedMut: 4 {
            pub btn_start: (X, 1, 0),
            pub btn_stop: (1, 1),
            pub btn_reset: (X, 1, 2),
            pub speed: (W, 2),
            pub length: (B, 0),
        }
    }

    #[test]
    fn pi_owned_macro_smoke() {
        let pi_buffer = [128, 0x55, 0xde, 0xad];

        let mut pi = TestPiOwned::new_zeroed();
        assert_eq!(pi.btn_start(), false);
        assert_eq!(pi.btn_stop(), false);
        assert_eq!(pi.btn_reset(), false);
        assert_eq!(pi.speed(), 0);
        assert_eq!(pi.length(), 0);

        pi.as_slice_mut().copy_from_slice(&pi_buffer);
        assert_eq!(pi.btn_start(), true);
        assert_eq!(pi.btn_stop(), false);
        assert_eq!(pi.btn_reset(), true);
        assert_eq!(pi.speed(), 0xdead);
        assert_eq!(pi.length(), 128);

        let mut pi = TestPiOwned::try_from(&pi_buffer).unwrap();
        assert_eq!(pi.btn_start(), true);
        assert_eq!(pi.btn_stop(), false);
        assert_eq!(pi.btn_reset(), true);
        assert_eq!(pi.speed(), 0xdead);
        assert_eq!(pi.length(), 128);

        *pi.as_mut().btn_start() = false;
        *pi.as_mut().btn_stop() = true;
        *pi.as_mut().btn_reset() = true;

        *pi.as_mut().speed() = 1337;
        *pi.as_mut().length() = 1;

        assert_eq!(pi.btn_start(), false);
        assert_eq!(pi.btn_stop(), true);
        assert_eq!(pi.btn_reset(), true);
        assert_eq!(pi.speed(), 1337);
        assert_eq!(pi.length(), 1);

        let pi_buffer = pi.as_slice();
        assert_eq!(tag!(&pi_buffer, 1, 0), false);
        assert_eq!(tag!(&pi_buffer, W, 2), 1337);
        assert_eq!(tag!(&pi_buffer, B, 0), 1);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Word address must be divisible by 2")
    )]
    fn test_unaligned_word_tag() {
        let buf = [
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ];
        assert_eq!(tag!(&buf, W, 1), 0xadbe);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Word address must be divisible by 2")
    )]
    fn test_unaligned_word_tag_mut() {
        let mut buf = [
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ];
        *tag_mut!(&mut buf, W, 1) = 0xcafe;
        assert_eq!(tag!(&buf, W, 1), 0xcafe);
    }

    process_image_owned! {
        pub struct TestPiPanic, mut TestPiPanicMut: 12 {
            pub unaligned_word: (W, 1),
            pub unaligned_dword: (D, 2),
            pub unaligned_lword: (L, 4),
        }
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Word address must be divisible by 2")
    )]
    fn test_unaligned_word() {
        let pi = TestPiPanic::try_from(&[
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ])
        .unwrap();
        assert_eq!(pi.unaligned_word(), 0xadbe);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Word address must be divisible by 2")
    )]
    fn test_unaligned_word_mut() {
        let mut pi = TestPiPanic::try_from(&[
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ])
        .unwrap();
        *pi.as_mut().unaligned_word() = 0xcafe;
        assert_eq!(pi.unaligned_word(), 0xcafe);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Double word address must be divisible by 4")
    )]
    fn test_unaligned_dword() {
        let pi = TestPiPanic::try_from(&[
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ])
        .unwrap();
        assert_eq!(pi.unaligned_dword(), 0xbeefdead);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Double word address must be divisible by 4")
    )]
    fn test_unaligned_dword_mut() {
        let mut pi = TestPiPanic::try_from(&[
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ])
        .unwrap();
        *pi.as_mut().unaligned_dword() = 0xc0ffee77;
        assert_eq!(pi.unaligned_dword(), 0xc0ffee77);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Long word address must be divisible by 8")
    )]
    fn test_unaligned_lword() {
        let pi = TestPiPanic::try_from(&[
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ])
        .unwrap();
        assert_eq!(pi.unaligned_lword(), 0xdeadbeefdeadbeef);
    }

    #[test]
    #[cfg_attr(
        not(feature = "allow_unaligned_tags"),
        should_panic(expected = "Long word address must be divisible by 8")
    )]
    fn test_unaligned_lword_mut() {
        let mut pi = TestPiPanic::try_from(&[
            0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
        ])
        .unwrap();
        *pi.as_mut().unaligned_lword() = 0x7fff000000c0ffee;
        assert_eq!(pi.unaligned_lword(), 0x7fff000000c0ffee);
    }
}