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
// devela::data::topol::graph::edge
macro_rules! impl_graph_edge {
// $IDX: the index primitive type. E.g. u8.
//
// $Graph: the graph type name. E.g. GraphU8.
// $Edge: the edge type name. E.g. EdgeU8.
// $Index: the index real type name. E.g. NonExtremeU8.
(@$Graph:ty, $Node:ty, $Edge:ty, $Index:ty, $IDX:ty) => { paste! {
/* definition */
#[doc = "A static [`" $Graph "`] edge."]
///
/// It can contain data `E` that defaults to the unit type without cost.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct $Edge<E = ()> (pub $Node<E, 2>);
/* constructors */
/// Constructors without data.
impl $Edge {
/// Returns a new edge without data, and two optional indices,
/// in compile-time.
///
/// # Errors
#[doc = "Returns [`IndexOutOfBounds`] if `(orig|dest) >= `[`" $IDX "::MAX`]."]
pub const fn new(orig: Option<$IDX>, dest: Option<$IDX>) -> Result<Self> {
let orig = match orig {
None => None,
Some(idx) => {
if let Some(n) = <$Index>::new(idx) { Some(n) }
else { return Err(IndexOutOfBounds(Some(idx as usize))); }
}
};
let dest = match dest {
None => None,
Some(idx) => {
if let Some(n) = <$Index>::new(idx) { Some(n) }
else { return Err(IndexOutOfBounds(Some(idx as usize))); }
}
};
Ok(Self($Node { data: (), links: [orig, dest] }))
}
/// Returns a new edge without data, and two indices,
/// in compile-time.
///
/// # Errors
#[doc = "Returns [`IndexOutOfBounds`] if `(orig|dest) >= `[`" $IDX "::MAX`]."]
pub const fn new_some(orig: $IDX, dest: $IDX) -> Result<Self> {
let Some(orig) = <$Index>::new(orig) else {
return Err(IndexOutOfBounds(Some(orig as usize)));
};
let Some(dest) = <$Index>::new(dest) else {
return Err(IndexOutOfBounds(Some(dest as usize)));
};
Ok(Self($Node { data: (), links: [Some(orig), Some(dest)] }))
}
/// Returns a new edge without data, and two optional validated indices,
/// in compile time.
pub const fn new_valid(orig: Option<$Index>, dest: Option<$Index>) -> Self {
Self($Node { data: (), links: [orig, dest] })
}
/// Returns a new edge without data, and two validated indices,
/// in compile time.
pub const fn new_some_valid(orig: $Index, dest: $Index) -> Self {
Self($Node { data: (), links: [Some(orig), Some(dest)] })
}
}
/// Constructors with data.
impl<E> $Edge<E> {
/// Returns a new graph edge, with `data`, and two optional indices.
///
/// # Errors
#[doc = "Returns [`IndexOutOfBounds`] if `(orig|dest) >= `[`" $IDX "::MAX`]."]
///
/// This method can't be *const* because in case of error,
/// the destructor can't be evaluated.
pub fn with(data: E, orig: Option<$IDX>, dest: Option<$IDX>) -> Result<Self> {
let orig = match orig {
None => None,
Some(idx) => {
if let Some(n) = <$Index>::new(idx) { Some(n) }
else { return Err(IndexOutOfBounds(Some(idx as usize))); }
}
};
let dest = match dest {
None => None,
Some(idx) => {
if let Some(n) = <$Index>::new(idx) { Some(n) }
else { return Err(IndexOutOfBounds(Some(idx as usize))); }
}
};
Ok(Self($Node { data, links: [orig, dest] }))
}
/// Returns a new edge, with `data` and two indices.
///
/// # Errors
#[doc = "Returns [`IndexOutOfBounds`] if `(orig|dest) >= `[`" $IDX "::MAX`]."]
///
/// This method can't be *const* because in case of error,
/// the destructor can't be evaluated.
pub fn with_some(data: E, orig: $IDX, dest: $IDX) -> Result<Self> {
let orig = if let Some(n) = <$Index>::new(orig) { Some(n) } else {
return Err(IndexOutOfBounds(Some(orig as usize)));
};
let dest = if let Some(n) = <$Index>::new(dest) { Some(n) } else {
return Err(IndexOutOfBounds(Some(dest as usize)));
};
Ok(Self($Node { data, links: [orig, dest] }))
}
/// Returns a new edge with the given `data` and two optional validated indices,
/// in compile-time.
pub const fn with_valid(data: E, orig: Option<$Index>, dest: Option<$Index>) -> Self {
Self($Node { data, links: [orig, dest] })
}
/// Returns a new edge with the given `data` and two validated indices,
/// in compile-time.
pub const fn with_some_valid(data: E, orig: $Index, dest: $Index) -> Self {
Self($Node { data, links: [Some(orig), Some(dest)] })
}
}
/// Constructors with copiable data.
impl<E: Copy> $Edge<E> {
/// Returns a new edge, with the given `data`.
///
/// # Errors
#[doc = "Returns [`IndexOutOfBounds`] if `(orig|dest) >= `[`" $IDX "::MAX`]."]
pub fn with_copy(data: E, orig: Option<$IDX>, dest: Option<$IDX>) -> Result<Self> {
let orig = match orig {
None => None,
Some(idx) => {
if let Some(n) = <$Index>::new(idx) { Some(n) }
else { return Err(IndexOutOfBounds(Some(idx as usize))); }
}
};
let dest = match dest {
None => None,
Some(idx) => {
if let Some(n) = <$Index>::new(idx) { Some(n) }
else { return Err(IndexOutOfBounds(Some(idx as usize))); }
}
};
Ok(Self($Node { data, links: [orig, dest] }))
}
/// Returns a new edge, with the given `data`.
///
/// # Errors
#[doc = "Returns [`IndexOutOfBounds`] if `(orig|dest) >= `[`" $IDX "::MAX`]."]
// This can't be const because the destructor can't be evaluated in case of Err.
pub fn with_some_copy(data: E, orig: $IDX, dest: $IDX) -> Result<Self> {
let orig = if let Some(n) = <$Index>::new(orig) { Some(n) } else {
return Err(IndexOutOfBounds(Some(orig as usize)));
};
let dest = if let Some(n) = <$Index>::new(dest) { Some(n) } else {
return Err(IndexOutOfBounds(Some(dest as usize)));
};
Ok(Self($Node { data, links: [orig, dest] }))
}
}
/* methods */
/// Methods.
impl<E> $Edge<E> {
///
#[must_use]
pub const fn orig(&self) -> Option<$IDX> { self.0.get_link_unchecked(0) }
///
#[must_use]
pub const fn dest(&self) -> Option<$IDX> { self.0.get_link_unchecked(1) }
///
#[must_use]
pub const fn is_orig_set(&self) -> bool { self.0.is_link_set_unchecked(0) }
///
#[must_use]
pub const fn is_dest_set(&self) -> bool { self.0.is_link_set_unchecked(1) }
}
/* trait impls */
// T: ConstInit
impl<E: ConstInit> ConstInit for $Edge<E> {
/// Returns an edge with default data and unset links.
const INIT: Self = Self($Node::INIT);
}
}};
}
pub(super) use impl_graph_edge;