array_as_struct/lib.rs
1//! Procedural macro to make array-types with simulated fields
2#![forbid(unsafe_code)]
3#![no_std]
4
5/// A trait to name all the associated types and simplify the conversion to and
6/// from the helper types.
7pub trait ArrayStruct {
8 /// Helper type which is identical to the original field-struct declaration
9 type Value;
10 /// The underlying array type
11 type Array;
12 /// Helper type which is similar to the original field-struct declaration,
13 /// but with `&'a T` instead of `T` for the field type
14 type Refs<'a>
15 where
16 Self: 'a;
17 /// Helper type which is similar to the original field-struct declaration,
18 /// but with `&'a mut T` instead of `T` for the field type
19 type Muts<'a>
20 where
21 Self: 'a;
22
23 /// Helper type which contains helper functions to get the index of each field
24 /// by name.
25 ///
26 /// ```
27 /// # use array_as_struct::{array_as_struct_doctest as array_as_struct, ArrayStruct};
28 /// # mod _hider{
29 /// use array_as_struct::{array_as_struct, ArrayStruct};
30 /// # }
31 ///
32 /// #[array_as_struct]
33 /// pub struct Foo {
34 /// bar: u32,
35 /// baz: u32,
36 /// }
37 ///
38 /// assert_eq!(<Foo as ArrayStruct>::Index::bar(), 0);
39 /// assert_eq!(<Foo as ArrayStruct>::Index::baz(), 1);
40 /// ```
41 type Index;
42
43 /// Construct the tuple-struct type from the named-field type
44 ///
45 /// ```
46 /// # use array_as_struct::{array_as_struct_doctest as array_as_struct, ArrayStruct};
47 /// # mod _hider{
48 /// use array_as_struct::{array_as_struct, ArrayStruct};
49 /// # }
50 ///
51 /// #[array_as_struct]
52 /// #[derive(Debug, PartialEq, Eq)]
53 /// pub struct Foo {
54 /// bar: u32,
55 /// baz: u32,
56 /// }
57 ///
58 /// // Workaround rust-lang/rust#86935
59 /// type Value = <Foo as ArrayStruct>::Value;
60 ///
61 /// let f = Foo::from_val(Value { bar: 10, baz: 15 });
62 ///
63 /// assert_eq!(Foo([10, 15]), f);
64 /// ```
65 fn from_val(value: Self::Value) -> Self;
66
67 /// Construct the named-field type from the tuple-struct type
68 ///
69 /// ```
70 /// # use array_as_struct::{array_as_struct_doctest as array_as_struct, ArrayStruct};
71 /// # mod _hider{
72 /// use array_as_struct::{array_as_struct, ArrayStruct};
73 /// # }
74 ///
75 /// #[array_as_struct]
76 /// #[derive(Debug, PartialEq, Eq)]
77 /// pub struct Foo {
78 /// bar: u32,
79 /// baz: u32,
80 /// }
81 ///
82 /// // Workaround rust-lang/rust#86935
83 /// type Value = <Foo as ArrayStruct>::Value;
84 ///
85 /// let f = Foo([10, 15]);
86 ///
87 /// assert_eq!(f.val(), Value { bar: 10, baz: 15 });
88 fn val(self) -> Self::Value;
89
90 /// Construct the tuple-struct type from the underlying array type
91 ///
92 /// This is basically the same as just calling the tuple-constructor
93 ///
94 /// ```
95 /// # use array_as_struct::{array_as_struct_doctest as array_as_struct, ArrayStruct};
96 /// # mod _hider{
97 /// use array_as_struct::{array_as_struct, ArrayStruct};
98 /// # }
99 ///
100 /// #[array_as_struct]
101 /// #[derive(Debug, PartialEq, Eq)]
102 /// pub struct Foo {
103 /// bar: u32,
104 /// baz: u32,
105 /// }
106 ///
107 /// assert_eq!(Foo([1, 2]), <Foo as ArrayStruct>::from_array([1, 2]));
108 /// ```
109 fn from_array(array: Self::Array) -> Self;
110
111 /// Construct the tuple-struct type from the underlying array type
112 ///
113 /// This is basically the same as just calling the `.0`
114 ///
115 /// ```
116 /// # use array_as_struct::{array_as_struct_doctest as array_as_struct, ArrayStruct};
117 /// # mod _hider{
118 /// use array_as_struct::{array_as_struct, ArrayStruct};
119 /// # }
120 ///
121 /// #[array_as_struct]
122 /// #[derive(Copy, Clone)]
123 /// pub struct Foo {
124 /// bar: u32,
125 /// baz: u32,
126 /// }
127 ///
128 /// let f = Foo([1, 2]);
129 ///
130 /// assert_eq!(f.0, f.to_array());
131 /// ```
132 fn to_array(self) -> Self::Array;
133
134 /// Construct the reference-named-field type from the tuple-struct type.
135 ///
136 /// This is the primary way of accessing the named fields
137 ///
138 fn refs(&'_ self) -> Self::Refs<'_>;
139
140 /// Construct the mutable-reference-named-field type from the tuple-struct type
141 fn muts(&'_ mut self) -> Self::Muts<'_>;
142}
143
144pub use array_as_struct_derive::array_as_struct;
145
146#[doc(hidden)]
147pub use array_as_struct_derive::array_as_struct_doctest;