1use super::blockchain::*;
4use super::extensions::*;
5use molecule::prelude::*;
6#[derive(Clone)]
7pub struct PingPayload(molecule::bytes::Bytes);
8impl ::core::fmt::LowerHex for PingPayload {
9 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10 use molecule::hex_string;
11 if f.alternate() {
12 write!(f, "0x")?;
13 }
14 write!(f, "{}", hex_string(self.as_slice()))
15 }
16}
17impl ::core::fmt::Debug for PingPayload {
18 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
19 write!(f, "{}({:#x})", Self::NAME, self)
20 }
21}
22impl ::core::fmt::Display for PingPayload {
23 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
24 write!(f, "{}(", Self::NAME)?;
25 self.to_enum().display_inner(f)?;
26 write!(f, ")")
27 }
28}
29impl ::core::default::Default for PingPayload {
30 fn default() -> Self {
31 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
32 PingPayload::new_unchecked(v)
33 }
34}
35impl PingPayload {
36 const DEFAULT_VALUE: [u8; 16] = [0, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
37 pub const ITEMS_COUNT: usize = 2;
38 pub fn item_id(&self) -> molecule::Number {
39 molecule::unpack_number(self.as_slice())
40 }
41 pub fn to_enum(&self) -> PingPayloadUnion {
42 let inner = self.0.slice(molecule::NUMBER_SIZE..);
43 match self.item_id() {
44 0 => Ping::new_unchecked(inner).into(),
45 1 => Pong::new_unchecked(inner).into(),
46 _ => panic!("{}: invalid data", Self::NAME),
47 }
48 }
49 pub fn as_reader<'r>(&'r self) -> PingPayloadReader<'r> {
50 PingPayloadReader::new_unchecked(self.as_slice())
51 }
52}
53impl molecule::prelude::Entity for PingPayload {
54 type Builder = PingPayloadBuilder;
55 const NAME: &'static str = "PingPayload";
56 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
57 PingPayload(data)
58 }
59 fn as_bytes(&self) -> molecule::bytes::Bytes {
60 self.0.clone()
61 }
62 fn as_slice(&self) -> &[u8] {
63 &self.0[..]
64 }
65 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
66 PingPayloadReader::from_slice(slice).map(|reader| reader.to_entity())
67 }
68 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
69 PingPayloadReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
70 }
71 fn new_builder() -> Self::Builder {
72 ::core::default::Default::default()
73 }
74 fn as_builder(self) -> Self::Builder {
75 Self::new_builder().set(self.to_enum())
76 }
77}
78#[derive(Clone, Copy)]
79pub struct PingPayloadReader<'r>(&'r [u8]);
80impl<'r> ::core::fmt::LowerHex for PingPayloadReader<'r> {
81 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
82 use molecule::hex_string;
83 if f.alternate() {
84 write!(f, "0x")?;
85 }
86 write!(f, "{}", hex_string(self.as_slice()))
87 }
88}
89impl<'r> ::core::fmt::Debug for PingPayloadReader<'r> {
90 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
91 write!(f, "{}({:#x})", Self::NAME, self)
92 }
93}
94impl<'r> ::core::fmt::Display for PingPayloadReader<'r> {
95 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
96 write!(f, "{}(", Self::NAME)?;
97 self.to_enum().display_inner(f)?;
98 write!(f, ")")
99 }
100}
101impl<'r> PingPayloadReader<'r> {
102 pub const ITEMS_COUNT: usize = 2;
103 pub fn item_id(&self) -> molecule::Number {
104 molecule::unpack_number(self.as_slice())
105 }
106 pub fn to_enum(&self) -> PingPayloadUnionReader<'r> {
107 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
108 match self.item_id() {
109 0 => PingReader::new_unchecked(inner).into(),
110 1 => PongReader::new_unchecked(inner).into(),
111 _ => panic!("{}: invalid data", Self::NAME),
112 }
113 }
114}
115impl<'r> molecule::prelude::Reader<'r> for PingPayloadReader<'r> {
116 type Entity = PingPayload;
117 const NAME: &'static str = "PingPayloadReader";
118 fn to_entity(&self) -> Self::Entity {
119 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
120 }
121 fn new_unchecked(slice: &'r [u8]) -> Self {
122 PingPayloadReader(slice)
123 }
124 fn as_slice(&self) -> &'r [u8] {
125 self.0
126 }
127 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
128 use molecule::verification_error as ve;
129 let slice_len = slice.len();
130 if slice_len < molecule::NUMBER_SIZE {
131 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
132 }
133 let item_id = molecule::unpack_number(slice);
134 let inner_slice = &slice[molecule::NUMBER_SIZE..];
135 match item_id {
136 0 => PingReader::verify(inner_slice, compatible),
137 1 => PongReader::verify(inner_slice, compatible),
138 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
139 }?;
140 Ok(())
141 }
142}
143#[derive(Clone, Debug, Default)]
144pub struct PingPayloadBuilder(pub(crate) PingPayloadUnion);
145impl PingPayloadBuilder {
146 pub const ITEMS_COUNT: usize = 2;
147 pub fn set<I>(mut self, v: I) -> Self
148 where
149 I: ::core::convert::Into<PingPayloadUnion>,
150 {
151 self.0 = v.into();
152 self
153 }
154}
155impl molecule::prelude::Builder for PingPayloadBuilder {
156 type Entity = PingPayload;
157 const NAME: &'static str = "PingPayloadBuilder";
158 fn expected_length(&self) -> usize {
159 molecule::NUMBER_SIZE + self.0.as_slice().len()
160 }
161 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
162 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
163 writer.write_all(self.0.as_slice())
164 }
165 fn build(&self) -> Self::Entity {
166 let mut inner = Vec::with_capacity(self.expected_length());
167 self.write(&mut inner)
168 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
169 PingPayload::new_unchecked(inner.into())
170 }
171}
172#[derive(Debug, Clone)]
173pub enum PingPayloadUnion {
174 Ping(Ping),
175 Pong(Pong),
176}
177#[derive(Debug, Clone, Copy)]
178pub enum PingPayloadUnionReader<'r> {
179 Ping(PingReader<'r>),
180 Pong(PongReader<'r>),
181}
182impl ::core::default::Default for PingPayloadUnion {
183 fn default() -> Self {
184 PingPayloadUnion::Ping(::core::default::Default::default())
185 }
186}
187impl ::core::fmt::Display for PingPayloadUnion {
188 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
189 match self {
190 PingPayloadUnion::Ping(ref item) => {
191 write!(f, "{}::{}({})", Self::NAME, Ping::NAME, item)
192 }
193 PingPayloadUnion::Pong(ref item) => {
194 write!(f, "{}::{}({})", Self::NAME, Pong::NAME, item)
195 }
196 }
197 }
198}
199impl<'r> ::core::fmt::Display for PingPayloadUnionReader<'r> {
200 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
201 match self {
202 PingPayloadUnionReader::Ping(ref item) => {
203 write!(f, "{}::{}({})", Self::NAME, Ping::NAME, item)
204 }
205 PingPayloadUnionReader::Pong(ref item) => {
206 write!(f, "{}::{}({})", Self::NAME, Pong::NAME, item)
207 }
208 }
209 }
210}
211impl PingPayloadUnion {
212 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
213 match self {
214 PingPayloadUnion::Ping(ref item) => write!(f, "{}", item),
215 PingPayloadUnion::Pong(ref item) => write!(f, "{}", item),
216 }
217 }
218}
219impl<'r> PingPayloadUnionReader<'r> {
220 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
221 match self {
222 PingPayloadUnionReader::Ping(ref item) => write!(f, "{}", item),
223 PingPayloadUnionReader::Pong(ref item) => write!(f, "{}", item),
224 }
225 }
226}
227impl ::core::convert::From<Ping> for PingPayloadUnion {
228 fn from(item: Ping) -> Self {
229 PingPayloadUnion::Ping(item)
230 }
231}
232impl ::core::convert::From<Pong> for PingPayloadUnion {
233 fn from(item: Pong) -> Self {
234 PingPayloadUnion::Pong(item)
235 }
236}
237impl<'r> ::core::convert::From<PingReader<'r>> for PingPayloadUnionReader<'r> {
238 fn from(item: PingReader<'r>) -> Self {
239 PingPayloadUnionReader::Ping(item)
240 }
241}
242impl<'r> ::core::convert::From<PongReader<'r>> for PingPayloadUnionReader<'r> {
243 fn from(item: PongReader<'r>) -> Self {
244 PingPayloadUnionReader::Pong(item)
245 }
246}
247impl PingPayloadUnion {
248 pub const NAME: &'static str = "PingPayloadUnion";
249 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
250 match self {
251 PingPayloadUnion::Ping(item) => item.as_bytes(),
252 PingPayloadUnion::Pong(item) => item.as_bytes(),
253 }
254 }
255 pub fn as_slice(&self) -> &[u8] {
256 match self {
257 PingPayloadUnion::Ping(item) => item.as_slice(),
258 PingPayloadUnion::Pong(item) => item.as_slice(),
259 }
260 }
261 pub fn item_id(&self) -> molecule::Number {
262 match self {
263 PingPayloadUnion::Ping(_) => 0,
264 PingPayloadUnion::Pong(_) => 1,
265 }
266 }
267 pub fn item_name(&self) -> &str {
268 match self {
269 PingPayloadUnion::Ping(_) => "Ping",
270 PingPayloadUnion::Pong(_) => "Pong",
271 }
272 }
273 pub fn as_reader<'r>(&'r self) -> PingPayloadUnionReader<'r> {
274 match self {
275 PingPayloadUnion::Ping(item) => item.as_reader().into(),
276 PingPayloadUnion::Pong(item) => item.as_reader().into(),
277 }
278 }
279}
280impl<'r> PingPayloadUnionReader<'r> {
281 pub const NAME: &'r str = "PingPayloadUnionReader";
282 pub fn as_slice(&self) -> &'r [u8] {
283 match self {
284 PingPayloadUnionReader::Ping(item) => item.as_slice(),
285 PingPayloadUnionReader::Pong(item) => item.as_slice(),
286 }
287 }
288 pub fn item_id(&self) -> molecule::Number {
289 match self {
290 PingPayloadUnionReader::Ping(_) => 0,
291 PingPayloadUnionReader::Pong(_) => 1,
292 }
293 }
294 pub fn item_name(&self) -> &str {
295 match self {
296 PingPayloadUnionReader::Ping(_) => "Ping",
297 PingPayloadUnionReader::Pong(_) => "Pong",
298 }
299 }
300}
301impl From<Ping> for PingPayload {
302 fn from(value: Ping) -> Self {
303 Self::new_builder().set(value).build()
304 }
305}
306impl From<Pong> for PingPayload {
307 fn from(value: Pong) -> Self {
308 Self::new_builder().set(value).build()
309 }
310}
311#[derive(Clone)]
312pub struct PingMessage(molecule::bytes::Bytes);
313impl ::core::fmt::LowerHex for PingMessage {
314 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
315 use molecule::hex_string;
316 if f.alternate() {
317 write!(f, "0x")?;
318 }
319 write!(f, "{}", hex_string(self.as_slice()))
320 }
321}
322impl ::core::fmt::Debug for PingMessage {
323 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
324 write!(f, "{}({:#x})", Self::NAME, self)
325 }
326}
327impl ::core::fmt::Display for PingMessage {
328 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
329 write!(f, "{} {{ ", Self::NAME)?;
330 write!(f, "{}: {}", "payload", self.payload())?;
331 let extra_count = self.count_extra_fields();
332 if extra_count != 0 {
333 write!(f, ", .. ({} fields)", extra_count)?;
334 }
335 write!(f, " }}")
336 }
337}
338impl ::core::default::Default for PingMessage {
339 fn default() -> Self {
340 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
341 PingMessage::new_unchecked(v)
342 }
343}
344impl PingMessage {
345 const DEFAULT_VALUE: [u8; 24] = [
346 24, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0,
347 ];
348 pub const FIELD_COUNT: usize = 1;
349 pub fn total_size(&self) -> usize {
350 molecule::unpack_number(self.as_slice()) as usize
351 }
352 pub fn field_count(&self) -> usize {
353 if self.total_size() == molecule::NUMBER_SIZE {
354 0
355 } else {
356 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
357 }
358 }
359 pub fn count_extra_fields(&self) -> usize {
360 self.field_count() - Self::FIELD_COUNT
361 }
362 pub fn has_extra_fields(&self) -> bool {
363 Self::FIELD_COUNT != self.field_count()
364 }
365 pub fn payload(&self) -> PingPayload {
366 let slice = self.as_slice();
367 let start = molecule::unpack_number(&slice[4..]) as usize;
368 if self.has_extra_fields() {
369 let end = molecule::unpack_number(&slice[8..]) as usize;
370 PingPayload::new_unchecked(self.0.slice(start..end))
371 } else {
372 PingPayload::new_unchecked(self.0.slice(start..))
373 }
374 }
375 pub fn as_reader<'r>(&'r self) -> PingMessageReader<'r> {
376 PingMessageReader::new_unchecked(self.as_slice())
377 }
378}
379impl molecule::prelude::Entity for PingMessage {
380 type Builder = PingMessageBuilder;
381 const NAME: &'static str = "PingMessage";
382 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
383 PingMessage(data)
384 }
385 fn as_bytes(&self) -> molecule::bytes::Bytes {
386 self.0.clone()
387 }
388 fn as_slice(&self) -> &[u8] {
389 &self.0[..]
390 }
391 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
392 PingMessageReader::from_slice(slice).map(|reader| reader.to_entity())
393 }
394 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
395 PingMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
396 }
397 fn new_builder() -> Self::Builder {
398 ::core::default::Default::default()
399 }
400 fn as_builder(self) -> Self::Builder {
401 Self::new_builder().payload(self.payload())
402 }
403}
404#[derive(Clone, Copy)]
405pub struct PingMessageReader<'r>(&'r [u8]);
406impl<'r> ::core::fmt::LowerHex for PingMessageReader<'r> {
407 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
408 use molecule::hex_string;
409 if f.alternate() {
410 write!(f, "0x")?;
411 }
412 write!(f, "{}", hex_string(self.as_slice()))
413 }
414}
415impl<'r> ::core::fmt::Debug for PingMessageReader<'r> {
416 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
417 write!(f, "{}({:#x})", Self::NAME, self)
418 }
419}
420impl<'r> ::core::fmt::Display for PingMessageReader<'r> {
421 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
422 write!(f, "{} {{ ", Self::NAME)?;
423 write!(f, "{}: {}", "payload", self.payload())?;
424 let extra_count = self.count_extra_fields();
425 if extra_count != 0 {
426 write!(f, ", .. ({} fields)", extra_count)?;
427 }
428 write!(f, " }}")
429 }
430}
431impl<'r> PingMessageReader<'r> {
432 pub const FIELD_COUNT: usize = 1;
433 pub fn total_size(&self) -> usize {
434 molecule::unpack_number(self.as_slice()) as usize
435 }
436 pub fn field_count(&self) -> usize {
437 if self.total_size() == molecule::NUMBER_SIZE {
438 0
439 } else {
440 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
441 }
442 }
443 pub fn count_extra_fields(&self) -> usize {
444 self.field_count() - Self::FIELD_COUNT
445 }
446 pub fn has_extra_fields(&self) -> bool {
447 Self::FIELD_COUNT != self.field_count()
448 }
449 pub fn payload(&self) -> PingPayloadReader<'r> {
450 let slice = self.as_slice();
451 let start = molecule::unpack_number(&slice[4..]) as usize;
452 if self.has_extra_fields() {
453 let end = molecule::unpack_number(&slice[8..]) as usize;
454 PingPayloadReader::new_unchecked(&self.as_slice()[start..end])
455 } else {
456 PingPayloadReader::new_unchecked(&self.as_slice()[start..])
457 }
458 }
459}
460impl<'r> molecule::prelude::Reader<'r> for PingMessageReader<'r> {
461 type Entity = PingMessage;
462 const NAME: &'static str = "PingMessageReader";
463 fn to_entity(&self) -> Self::Entity {
464 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
465 }
466 fn new_unchecked(slice: &'r [u8]) -> Self {
467 PingMessageReader(slice)
468 }
469 fn as_slice(&self) -> &'r [u8] {
470 self.0
471 }
472 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
473 use molecule::verification_error as ve;
474 let slice_len = slice.len();
475 if slice_len < molecule::NUMBER_SIZE {
476 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
477 }
478 let total_size = molecule::unpack_number(slice) as usize;
479 if slice_len != total_size {
480 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
481 }
482 if slice_len < molecule::NUMBER_SIZE * 2 {
483 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
484 }
485 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
486 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
487 return ve!(Self, OffsetsNotMatch);
488 }
489 if slice_len < offset_first {
490 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
491 }
492 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
493 if field_count < Self::FIELD_COUNT {
494 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
495 } else if !compatible && field_count > Self::FIELD_COUNT {
496 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
497 };
498 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
499 .chunks_exact(molecule::NUMBER_SIZE)
500 .map(|x| molecule::unpack_number(x) as usize)
501 .collect();
502 offsets.push(total_size);
503 if offsets.windows(2).any(|i| i[0] > i[1]) {
504 return ve!(Self, OffsetsNotMatch);
505 }
506 PingPayloadReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
507 Ok(())
508 }
509}
510#[derive(Clone, Debug, Default)]
511pub struct PingMessageBuilder {
512 pub(crate) payload: PingPayload,
513}
514impl PingMessageBuilder {
515 pub const FIELD_COUNT: usize = 1;
516 pub fn payload<T>(mut self, v: T) -> Self
517 where
518 T: ::core::convert::Into<PingPayload>,
519 {
520 self.payload = v.into();
521 self
522 }
523}
524impl molecule::prelude::Builder for PingMessageBuilder {
525 type Entity = PingMessage;
526 const NAME: &'static str = "PingMessageBuilder";
527 fn expected_length(&self) -> usize {
528 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.payload.as_slice().len()
529 }
530 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
531 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
532 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
533 offsets.push(total_size);
534 total_size += self.payload.as_slice().len();
535 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
536 for offset in offsets.into_iter() {
537 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
538 }
539 writer.write_all(self.payload.as_slice())?;
540 Ok(())
541 }
542 fn build(&self) -> Self::Entity {
543 let mut inner = Vec::with_capacity(self.expected_length());
544 self.write(&mut inner)
545 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
546 PingMessage::new_unchecked(inner.into())
547 }
548}
549#[derive(Clone)]
550pub struct Ping(molecule::bytes::Bytes);
551impl ::core::fmt::LowerHex for Ping {
552 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
553 use molecule::hex_string;
554 if f.alternate() {
555 write!(f, "0x")?;
556 }
557 write!(f, "{}", hex_string(self.as_slice()))
558 }
559}
560impl ::core::fmt::Debug for Ping {
561 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
562 write!(f, "{}({:#x})", Self::NAME, self)
563 }
564}
565impl ::core::fmt::Display for Ping {
566 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
567 write!(f, "{} {{ ", Self::NAME)?;
568 write!(f, "{}: {}", "nonce", self.nonce())?;
569 let extra_count = self.count_extra_fields();
570 if extra_count != 0 {
571 write!(f, ", .. ({} fields)", extra_count)?;
572 }
573 write!(f, " }}")
574 }
575}
576impl ::core::default::Default for Ping {
577 fn default() -> Self {
578 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
579 Ping::new_unchecked(v)
580 }
581}
582impl Ping {
583 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
584 pub const FIELD_COUNT: usize = 1;
585 pub fn total_size(&self) -> usize {
586 molecule::unpack_number(self.as_slice()) as usize
587 }
588 pub fn field_count(&self) -> usize {
589 if self.total_size() == molecule::NUMBER_SIZE {
590 0
591 } else {
592 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
593 }
594 }
595 pub fn count_extra_fields(&self) -> usize {
596 self.field_count() - Self::FIELD_COUNT
597 }
598 pub fn has_extra_fields(&self) -> bool {
599 Self::FIELD_COUNT != self.field_count()
600 }
601 pub fn nonce(&self) -> Uint32 {
602 let slice = self.as_slice();
603 let start = molecule::unpack_number(&slice[4..]) as usize;
604 if self.has_extra_fields() {
605 let end = molecule::unpack_number(&slice[8..]) as usize;
606 Uint32::new_unchecked(self.0.slice(start..end))
607 } else {
608 Uint32::new_unchecked(self.0.slice(start..))
609 }
610 }
611 pub fn as_reader<'r>(&'r self) -> PingReader<'r> {
612 PingReader::new_unchecked(self.as_slice())
613 }
614}
615impl molecule::prelude::Entity for Ping {
616 type Builder = PingBuilder;
617 const NAME: &'static str = "Ping";
618 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
619 Ping(data)
620 }
621 fn as_bytes(&self) -> molecule::bytes::Bytes {
622 self.0.clone()
623 }
624 fn as_slice(&self) -> &[u8] {
625 &self.0[..]
626 }
627 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
628 PingReader::from_slice(slice).map(|reader| reader.to_entity())
629 }
630 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
631 PingReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
632 }
633 fn new_builder() -> Self::Builder {
634 ::core::default::Default::default()
635 }
636 fn as_builder(self) -> Self::Builder {
637 Self::new_builder().nonce(self.nonce())
638 }
639}
640#[derive(Clone, Copy)]
641pub struct PingReader<'r>(&'r [u8]);
642impl<'r> ::core::fmt::LowerHex for PingReader<'r> {
643 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
644 use molecule::hex_string;
645 if f.alternate() {
646 write!(f, "0x")?;
647 }
648 write!(f, "{}", hex_string(self.as_slice()))
649 }
650}
651impl<'r> ::core::fmt::Debug for PingReader<'r> {
652 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
653 write!(f, "{}({:#x})", Self::NAME, self)
654 }
655}
656impl<'r> ::core::fmt::Display for PingReader<'r> {
657 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
658 write!(f, "{} {{ ", Self::NAME)?;
659 write!(f, "{}: {}", "nonce", self.nonce())?;
660 let extra_count = self.count_extra_fields();
661 if extra_count != 0 {
662 write!(f, ", .. ({} fields)", extra_count)?;
663 }
664 write!(f, " }}")
665 }
666}
667impl<'r> PingReader<'r> {
668 pub const FIELD_COUNT: usize = 1;
669 pub fn total_size(&self) -> usize {
670 molecule::unpack_number(self.as_slice()) as usize
671 }
672 pub fn field_count(&self) -> usize {
673 if self.total_size() == molecule::NUMBER_SIZE {
674 0
675 } else {
676 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
677 }
678 }
679 pub fn count_extra_fields(&self) -> usize {
680 self.field_count() - Self::FIELD_COUNT
681 }
682 pub fn has_extra_fields(&self) -> bool {
683 Self::FIELD_COUNT != self.field_count()
684 }
685 pub fn nonce(&self) -> Uint32Reader<'r> {
686 let slice = self.as_slice();
687 let start = molecule::unpack_number(&slice[4..]) as usize;
688 if self.has_extra_fields() {
689 let end = molecule::unpack_number(&slice[8..]) as usize;
690 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
691 } else {
692 Uint32Reader::new_unchecked(&self.as_slice()[start..])
693 }
694 }
695}
696impl<'r> molecule::prelude::Reader<'r> for PingReader<'r> {
697 type Entity = Ping;
698 const NAME: &'static str = "PingReader";
699 fn to_entity(&self) -> Self::Entity {
700 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
701 }
702 fn new_unchecked(slice: &'r [u8]) -> Self {
703 PingReader(slice)
704 }
705 fn as_slice(&self) -> &'r [u8] {
706 self.0
707 }
708 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
709 use molecule::verification_error as ve;
710 let slice_len = slice.len();
711 if slice_len < molecule::NUMBER_SIZE {
712 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
713 }
714 let total_size = molecule::unpack_number(slice) as usize;
715 if slice_len != total_size {
716 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
717 }
718 if slice_len < molecule::NUMBER_SIZE * 2 {
719 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
720 }
721 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
722 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
723 return ve!(Self, OffsetsNotMatch);
724 }
725 if slice_len < offset_first {
726 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
727 }
728 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
729 if field_count < Self::FIELD_COUNT {
730 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
731 } else if !compatible && field_count > Self::FIELD_COUNT {
732 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
733 };
734 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
735 .chunks_exact(molecule::NUMBER_SIZE)
736 .map(|x| molecule::unpack_number(x) as usize)
737 .collect();
738 offsets.push(total_size);
739 if offsets.windows(2).any(|i| i[0] > i[1]) {
740 return ve!(Self, OffsetsNotMatch);
741 }
742 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
743 Ok(())
744 }
745}
746#[derive(Clone, Debug, Default)]
747pub struct PingBuilder {
748 pub(crate) nonce: Uint32,
749}
750impl PingBuilder {
751 pub const FIELD_COUNT: usize = 1;
752 pub fn nonce<T>(mut self, v: T) -> Self
753 where
754 T: ::core::convert::Into<Uint32>,
755 {
756 self.nonce = v.into();
757 self
758 }
759}
760impl molecule::prelude::Builder for PingBuilder {
761 type Entity = Ping;
762 const NAME: &'static str = "PingBuilder";
763 fn expected_length(&self) -> usize {
764 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.nonce.as_slice().len()
765 }
766 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
767 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
768 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
769 offsets.push(total_size);
770 total_size += self.nonce.as_slice().len();
771 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
772 for offset in offsets.into_iter() {
773 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
774 }
775 writer.write_all(self.nonce.as_slice())?;
776 Ok(())
777 }
778 fn build(&self) -> Self::Entity {
779 let mut inner = Vec::with_capacity(self.expected_length());
780 self.write(&mut inner)
781 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
782 Ping::new_unchecked(inner.into())
783 }
784}
785#[derive(Clone)]
786pub struct Pong(molecule::bytes::Bytes);
787impl ::core::fmt::LowerHex for Pong {
788 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
789 use molecule::hex_string;
790 if f.alternate() {
791 write!(f, "0x")?;
792 }
793 write!(f, "{}", hex_string(self.as_slice()))
794 }
795}
796impl ::core::fmt::Debug for Pong {
797 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
798 write!(f, "{}({:#x})", Self::NAME, self)
799 }
800}
801impl ::core::fmt::Display for Pong {
802 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
803 write!(f, "{} {{ ", Self::NAME)?;
804 write!(f, "{}: {}", "nonce", self.nonce())?;
805 let extra_count = self.count_extra_fields();
806 if extra_count != 0 {
807 write!(f, ", .. ({} fields)", extra_count)?;
808 }
809 write!(f, " }}")
810 }
811}
812impl ::core::default::Default for Pong {
813 fn default() -> Self {
814 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
815 Pong::new_unchecked(v)
816 }
817}
818impl Pong {
819 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
820 pub const FIELD_COUNT: usize = 1;
821 pub fn total_size(&self) -> usize {
822 molecule::unpack_number(self.as_slice()) as usize
823 }
824 pub fn field_count(&self) -> usize {
825 if self.total_size() == molecule::NUMBER_SIZE {
826 0
827 } else {
828 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
829 }
830 }
831 pub fn count_extra_fields(&self) -> usize {
832 self.field_count() - Self::FIELD_COUNT
833 }
834 pub fn has_extra_fields(&self) -> bool {
835 Self::FIELD_COUNT != self.field_count()
836 }
837 pub fn nonce(&self) -> Uint32 {
838 let slice = self.as_slice();
839 let start = molecule::unpack_number(&slice[4..]) as usize;
840 if self.has_extra_fields() {
841 let end = molecule::unpack_number(&slice[8..]) as usize;
842 Uint32::new_unchecked(self.0.slice(start..end))
843 } else {
844 Uint32::new_unchecked(self.0.slice(start..))
845 }
846 }
847 pub fn as_reader<'r>(&'r self) -> PongReader<'r> {
848 PongReader::new_unchecked(self.as_slice())
849 }
850}
851impl molecule::prelude::Entity for Pong {
852 type Builder = PongBuilder;
853 const NAME: &'static str = "Pong";
854 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
855 Pong(data)
856 }
857 fn as_bytes(&self) -> molecule::bytes::Bytes {
858 self.0.clone()
859 }
860 fn as_slice(&self) -> &[u8] {
861 &self.0[..]
862 }
863 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
864 PongReader::from_slice(slice).map(|reader| reader.to_entity())
865 }
866 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
867 PongReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
868 }
869 fn new_builder() -> Self::Builder {
870 ::core::default::Default::default()
871 }
872 fn as_builder(self) -> Self::Builder {
873 Self::new_builder().nonce(self.nonce())
874 }
875}
876#[derive(Clone, Copy)]
877pub struct PongReader<'r>(&'r [u8]);
878impl<'r> ::core::fmt::LowerHex for PongReader<'r> {
879 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
880 use molecule::hex_string;
881 if f.alternate() {
882 write!(f, "0x")?;
883 }
884 write!(f, "{}", hex_string(self.as_slice()))
885 }
886}
887impl<'r> ::core::fmt::Debug for PongReader<'r> {
888 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
889 write!(f, "{}({:#x})", Self::NAME, self)
890 }
891}
892impl<'r> ::core::fmt::Display for PongReader<'r> {
893 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
894 write!(f, "{} {{ ", Self::NAME)?;
895 write!(f, "{}: {}", "nonce", self.nonce())?;
896 let extra_count = self.count_extra_fields();
897 if extra_count != 0 {
898 write!(f, ", .. ({} fields)", extra_count)?;
899 }
900 write!(f, " }}")
901 }
902}
903impl<'r> PongReader<'r> {
904 pub const FIELD_COUNT: usize = 1;
905 pub fn total_size(&self) -> usize {
906 molecule::unpack_number(self.as_slice()) as usize
907 }
908 pub fn field_count(&self) -> usize {
909 if self.total_size() == molecule::NUMBER_SIZE {
910 0
911 } else {
912 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
913 }
914 }
915 pub fn count_extra_fields(&self) -> usize {
916 self.field_count() - Self::FIELD_COUNT
917 }
918 pub fn has_extra_fields(&self) -> bool {
919 Self::FIELD_COUNT != self.field_count()
920 }
921 pub fn nonce(&self) -> Uint32Reader<'r> {
922 let slice = self.as_slice();
923 let start = molecule::unpack_number(&slice[4..]) as usize;
924 if self.has_extra_fields() {
925 let end = molecule::unpack_number(&slice[8..]) as usize;
926 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
927 } else {
928 Uint32Reader::new_unchecked(&self.as_slice()[start..])
929 }
930 }
931}
932impl<'r> molecule::prelude::Reader<'r> for PongReader<'r> {
933 type Entity = Pong;
934 const NAME: &'static str = "PongReader";
935 fn to_entity(&self) -> Self::Entity {
936 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
937 }
938 fn new_unchecked(slice: &'r [u8]) -> Self {
939 PongReader(slice)
940 }
941 fn as_slice(&self) -> &'r [u8] {
942 self.0
943 }
944 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
945 use molecule::verification_error as ve;
946 let slice_len = slice.len();
947 if slice_len < molecule::NUMBER_SIZE {
948 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
949 }
950 let total_size = molecule::unpack_number(slice) as usize;
951 if slice_len != total_size {
952 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
953 }
954 if slice_len < molecule::NUMBER_SIZE * 2 {
955 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
956 }
957 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
958 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
959 return ve!(Self, OffsetsNotMatch);
960 }
961 if slice_len < offset_first {
962 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
963 }
964 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
965 if field_count < Self::FIELD_COUNT {
966 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
967 } else if !compatible && field_count > Self::FIELD_COUNT {
968 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
969 };
970 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
971 .chunks_exact(molecule::NUMBER_SIZE)
972 .map(|x| molecule::unpack_number(x) as usize)
973 .collect();
974 offsets.push(total_size);
975 if offsets.windows(2).any(|i| i[0] > i[1]) {
976 return ve!(Self, OffsetsNotMatch);
977 }
978 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
979 Ok(())
980 }
981}
982#[derive(Clone, Debug, Default)]
983pub struct PongBuilder {
984 pub(crate) nonce: Uint32,
985}
986impl PongBuilder {
987 pub const FIELD_COUNT: usize = 1;
988 pub fn nonce<T>(mut self, v: T) -> Self
989 where
990 T: ::core::convert::Into<Uint32>,
991 {
992 self.nonce = v.into();
993 self
994 }
995}
996impl molecule::prelude::Builder for PongBuilder {
997 type Entity = Pong;
998 const NAME: &'static str = "PongBuilder";
999 fn expected_length(&self) -> usize {
1000 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.nonce.as_slice().len()
1001 }
1002 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1003 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
1004 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
1005 offsets.push(total_size);
1006 total_size += self.nonce.as_slice().len();
1007 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
1008 for offset in offsets.into_iter() {
1009 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
1010 }
1011 writer.write_all(self.nonce.as_slice())?;
1012 Ok(())
1013 }
1014 fn build(&self) -> Self::Entity {
1015 let mut inner = Vec::with_capacity(self.expected_length());
1016 self.write(&mut inner)
1017 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1018 Pong::new_unchecked(inner.into())
1019 }
1020}
1021#[derive(Clone)]
1022pub struct NodeVec(molecule::bytes::Bytes);
1023impl ::core::fmt::LowerHex for NodeVec {
1024 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1025 use molecule::hex_string;
1026 if f.alternate() {
1027 write!(f, "0x")?;
1028 }
1029 write!(f, "{}", hex_string(self.as_slice()))
1030 }
1031}
1032impl ::core::fmt::Debug for NodeVec {
1033 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1034 write!(f, "{}({:#x})", Self::NAME, self)
1035 }
1036}
1037impl ::core::fmt::Display for NodeVec {
1038 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1039 write!(f, "{} [", Self::NAME)?;
1040 for i in 0..self.len() {
1041 if i == 0 {
1042 write!(f, "{}", self.get_unchecked(i))?;
1043 } else {
1044 write!(f, ", {}", self.get_unchecked(i))?;
1045 }
1046 }
1047 write!(f, "]")
1048 }
1049}
1050impl ::core::default::Default for NodeVec {
1051 fn default() -> Self {
1052 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1053 NodeVec::new_unchecked(v)
1054 }
1055}
1056impl NodeVec {
1057 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
1058 pub fn total_size(&self) -> usize {
1059 molecule::unpack_number(self.as_slice()) as usize
1060 }
1061 pub fn item_count(&self) -> usize {
1062 if self.total_size() == molecule::NUMBER_SIZE {
1063 0
1064 } else {
1065 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1066 }
1067 }
1068 pub fn len(&self) -> usize {
1069 self.item_count()
1070 }
1071 pub fn is_empty(&self) -> bool {
1072 self.len() == 0
1073 }
1074 pub fn get(&self, idx: usize) -> Option<Node> {
1075 if idx >= self.len() {
1076 None
1077 } else {
1078 Some(self.get_unchecked(idx))
1079 }
1080 }
1081 pub fn get_unchecked(&self, idx: usize) -> Node {
1082 let slice = self.as_slice();
1083 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
1084 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
1085 if idx == self.len() - 1 {
1086 Node::new_unchecked(self.0.slice(start..))
1087 } else {
1088 let end_idx = start_idx + molecule::NUMBER_SIZE;
1089 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
1090 Node::new_unchecked(self.0.slice(start..end))
1091 }
1092 }
1093 pub fn as_reader<'r>(&'r self) -> NodeVecReader<'r> {
1094 NodeVecReader::new_unchecked(self.as_slice())
1095 }
1096}
1097impl molecule::prelude::Entity for NodeVec {
1098 type Builder = NodeVecBuilder;
1099 const NAME: &'static str = "NodeVec";
1100 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1101 NodeVec(data)
1102 }
1103 fn as_bytes(&self) -> molecule::bytes::Bytes {
1104 self.0.clone()
1105 }
1106 fn as_slice(&self) -> &[u8] {
1107 &self.0[..]
1108 }
1109 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1110 NodeVecReader::from_slice(slice).map(|reader| reader.to_entity())
1111 }
1112 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1113 NodeVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1114 }
1115 fn new_builder() -> Self::Builder {
1116 ::core::default::Default::default()
1117 }
1118 fn as_builder(self) -> Self::Builder {
1119 Self::new_builder().extend(self.into_iter())
1120 }
1121}
1122#[derive(Clone, Copy)]
1123pub struct NodeVecReader<'r>(&'r [u8]);
1124impl<'r> ::core::fmt::LowerHex for NodeVecReader<'r> {
1125 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1126 use molecule::hex_string;
1127 if f.alternate() {
1128 write!(f, "0x")?;
1129 }
1130 write!(f, "{}", hex_string(self.as_slice()))
1131 }
1132}
1133impl<'r> ::core::fmt::Debug for NodeVecReader<'r> {
1134 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1135 write!(f, "{}({:#x})", Self::NAME, self)
1136 }
1137}
1138impl<'r> ::core::fmt::Display for NodeVecReader<'r> {
1139 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1140 write!(f, "{} [", Self::NAME)?;
1141 for i in 0..self.len() {
1142 if i == 0 {
1143 write!(f, "{}", self.get_unchecked(i))?;
1144 } else {
1145 write!(f, ", {}", self.get_unchecked(i))?;
1146 }
1147 }
1148 write!(f, "]")
1149 }
1150}
1151impl<'r> NodeVecReader<'r> {
1152 pub fn total_size(&self) -> usize {
1153 molecule::unpack_number(self.as_slice()) as usize
1154 }
1155 pub fn item_count(&self) -> usize {
1156 if self.total_size() == molecule::NUMBER_SIZE {
1157 0
1158 } else {
1159 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1160 }
1161 }
1162 pub fn len(&self) -> usize {
1163 self.item_count()
1164 }
1165 pub fn is_empty(&self) -> bool {
1166 self.len() == 0
1167 }
1168 pub fn get(&self, idx: usize) -> Option<NodeReader<'r>> {
1169 if idx >= self.len() {
1170 None
1171 } else {
1172 Some(self.get_unchecked(idx))
1173 }
1174 }
1175 pub fn get_unchecked(&self, idx: usize) -> NodeReader<'r> {
1176 let slice = self.as_slice();
1177 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
1178 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
1179 if idx == self.len() - 1 {
1180 NodeReader::new_unchecked(&self.as_slice()[start..])
1181 } else {
1182 let end_idx = start_idx + molecule::NUMBER_SIZE;
1183 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
1184 NodeReader::new_unchecked(&self.as_slice()[start..end])
1185 }
1186 }
1187}
1188impl<'r> molecule::prelude::Reader<'r> for NodeVecReader<'r> {
1189 type Entity = NodeVec;
1190 const NAME: &'static str = "NodeVecReader";
1191 fn to_entity(&self) -> Self::Entity {
1192 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1193 }
1194 fn new_unchecked(slice: &'r [u8]) -> Self {
1195 NodeVecReader(slice)
1196 }
1197 fn as_slice(&self) -> &'r [u8] {
1198 self.0
1199 }
1200 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
1201 use molecule::verification_error as ve;
1202 let slice_len = slice.len();
1203 if slice_len < molecule::NUMBER_SIZE {
1204 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1205 }
1206 let total_size = molecule::unpack_number(slice) as usize;
1207 if slice_len != total_size {
1208 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1209 }
1210 if slice_len == molecule::NUMBER_SIZE {
1211 return Ok(());
1212 }
1213 if slice_len < molecule::NUMBER_SIZE * 2 {
1214 return ve!(
1215 Self,
1216 TotalSizeNotMatch,
1217 molecule::NUMBER_SIZE * 2,
1218 slice_len
1219 );
1220 }
1221 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
1222 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
1223 return ve!(Self, OffsetsNotMatch);
1224 }
1225 if slice_len < offset_first {
1226 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
1227 }
1228 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
1229 .chunks_exact(molecule::NUMBER_SIZE)
1230 .map(|x| molecule::unpack_number(x) as usize)
1231 .collect();
1232 offsets.push(total_size);
1233 if offsets.windows(2).any(|i| i[0] > i[1]) {
1234 return ve!(Self, OffsetsNotMatch);
1235 }
1236 for pair in offsets.windows(2) {
1237 let start = pair[0];
1238 let end = pair[1];
1239 NodeReader::verify(&slice[start..end], compatible)?;
1240 }
1241 Ok(())
1242 }
1243}
1244#[derive(Clone, Debug, Default)]
1245pub struct NodeVecBuilder(pub(crate) Vec<Node>);
1246impl NodeVecBuilder {
1247 pub fn set(mut self, v: Vec<Node>) -> Self {
1248 self.0 = v;
1249 self
1250 }
1251 pub fn push<T>(mut self, v: T) -> Self
1252 where
1253 T: ::core::convert::Into<Node>,
1254 {
1255 self.0.push(v.into());
1256 self
1257 }
1258 pub fn extend<T: ::core::iter::IntoIterator<Item = Node>>(mut self, iter: T) -> Self {
1259 self.0.extend(iter);
1260 self
1261 }
1262 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Node>
1263 where
1264 T: ::core::convert::Into<Node>,
1265 {
1266 self.0
1267 .get_mut(index)
1268 .map(|item| ::core::mem::replace(item, v.into()))
1269 }
1270}
1271impl molecule::prelude::Builder for NodeVecBuilder {
1272 type Entity = NodeVec;
1273 const NAME: &'static str = "NodeVecBuilder";
1274 fn expected_length(&self) -> usize {
1275 molecule::NUMBER_SIZE * (self.0.len() + 1)
1276 + self
1277 .0
1278 .iter()
1279 .map(|inner| inner.as_slice().len())
1280 .sum::<usize>()
1281 }
1282 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1283 let item_count = self.0.len();
1284 if item_count == 0 {
1285 writer.write_all(&molecule::pack_number(
1286 molecule::NUMBER_SIZE as molecule::Number,
1287 ))?;
1288 } else {
1289 let (total_size, offsets) = self.0.iter().fold(
1290 (
1291 molecule::NUMBER_SIZE * (item_count + 1),
1292 Vec::with_capacity(item_count),
1293 ),
1294 |(start, mut offsets), inner| {
1295 offsets.push(start);
1296 (start + inner.as_slice().len(), offsets)
1297 },
1298 );
1299 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
1300 for offset in offsets.into_iter() {
1301 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
1302 }
1303 for inner in self.0.iter() {
1304 writer.write_all(inner.as_slice())?;
1305 }
1306 }
1307 Ok(())
1308 }
1309 fn build(&self) -> Self::Entity {
1310 let mut inner = Vec::with_capacity(self.expected_length());
1311 self.write(&mut inner)
1312 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1313 NodeVec::new_unchecked(inner.into())
1314 }
1315}
1316pub struct NodeVecIterator(NodeVec, usize, usize);
1317impl ::core::iter::Iterator for NodeVecIterator {
1318 type Item = Node;
1319 fn next(&mut self) -> Option<Self::Item> {
1320 if self.1 >= self.2 {
1321 None
1322 } else {
1323 let ret = self.0.get_unchecked(self.1);
1324 self.1 += 1;
1325 Some(ret)
1326 }
1327 }
1328}
1329impl ::core::iter::ExactSizeIterator for NodeVecIterator {
1330 fn len(&self) -> usize {
1331 self.2 - self.1
1332 }
1333}
1334impl ::core::iter::IntoIterator for NodeVec {
1335 type Item = Node;
1336 type IntoIter = NodeVecIterator;
1337 fn into_iter(self) -> Self::IntoIter {
1338 let len = self.len();
1339 NodeVecIterator(self, 0, len)
1340 }
1341}
1342impl<'r> NodeVecReader<'r> {
1343 pub fn iter<'t>(&'t self) -> NodeVecReaderIterator<'t, 'r> {
1344 NodeVecReaderIterator(&self, 0, self.len())
1345 }
1346}
1347pub struct NodeVecReaderIterator<'t, 'r>(&'t NodeVecReader<'r>, usize, usize);
1348impl<'t: 'r, 'r> ::core::iter::Iterator for NodeVecReaderIterator<'t, 'r> {
1349 type Item = NodeReader<'t>;
1350 fn next(&mut self) -> Option<Self::Item> {
1351 if self.1 >= self.2 {
1352 None
1353 } else {
1354 let ret = self.0.get_unchecked(self.1);
1355 self.1 += 1;
1356 Some(ret)
1357 }
1358 }
1359}
1360impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for NodeVecReaderIterator<'t, 'r> {
1361 fn len(&self) -> usize {
1362 self.2 - self.1
1363 }
1364}
1365impl<T> ::core::iter::FromIterator<T> for NodeVec
1366where
1367 T: Into<Node>,
1368{
1369 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1370 Self::new_builder()
1371 .extend(iter.into_iter().map(Into::into))
1372 .build()
1373 }
1374}
1375impl<T> From<Vec<T>> for NodeVec
1376where
1377 T: Into<Node>,
1378{
1379 fn from(v: Vec<T>) -> Self {
1380 v.into_iter().collect()
1381 }
1382}
1383#[derive(Clone)]
1384pub struct Node2Vec(molecule::bytes::Bytes);
1385impl ::core::fmt::LowerHex for Node2Vec {
1386 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1387 use molecule::hex_string;
1388 if f.alternate() {
1389 write!(f, "0x")?;
1390 }
1391 write!(f, "{}", hex_string(self.as_slice()))
1392 }
1393}
1394impl ::core::fmt::Debug for Node2Vec {
1395 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1396 write!(f, "{}({:#x})", Self::NAME, self)
1397 }
1398}
1399impl ::core::fmt::Display for Node2Vec {
1400 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1401 write!(f, "{} [", Self::NAME)?;
1402 for i in 0..self.len() {
1403 if i == 0 {
1404 write!(f, "{}", self.get_unchecked(i))?;
1405 } else {
1406 write!(f, ", {}", self.get_unchecked(i))?;
1407 }
1408 }
1409 write!(f, "]")
1410 }
1411}
1412impl ::core::default::Default for Node2Vec {
1413 fn default() -> Self {
1414 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1415 Node2Vec::new_unchecked(v)
1416 }
1417}
1418impl Node2Vec {
1419 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
1420 pub fn total_size(&self) -> usize {
1421 molecule::unpack_number(self.as_slice()) as usize
1422 }
1423 pub fn item_count(&self) -> usize {
1424 if self.total_size() == molecule::NUMBER_SIZE {
1425 0
1426 } else {
1427 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1428 }
1429 }
1430 pub fn len(&self) -> usize {
1431 self.item_count()
1432 }
1433 pub fn is_empty(&self) -> bool {
1434 self.len() == 0
1435 }
1436 pub fn get(&self, idx: usize) -> Option<Node2> {
1437 if idx >= self.len() {
1438 None
1439 } else {
1440 Some(self.get_unchecked(idx))
1441 }
1442 }
1443 pub fn get_unchecked(&self, idx: usize) -> Node2 {
1444 let slice = self.as_slice();
1445 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
1446 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
1447 if idx == self.len() - 1 {
1448 Node2::new_unchecked(self.0.slice(start..))
1449 } else {
1450 let end_idx = start_idx + molecule::NUMBER_SIZE;
1451 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
1452 Node2::new_unchecked(self.0.slice(start..end))
1453 }
1454 }
1455 pub fn as_reader<'r>(&'r self) -> Node2VecReader<'r> {
1456 Node2VecReader::new_unchecked(self.as_slice())
1457 }
1458}
1459impl molecule::prelude::Entity for Node2Vec {
1460 type Builder = Node2VecBuilder;
1461 const NAME: &'static str = "Node2Vec";
1462 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1463 Node2Vec(data)
1464 }
1465 fn as_bytes(&self) -> molecule::bytes::Bytes {
1466 self.0.clone()
1467 }
1468 fn as_slice(&self) -> &[u8] {
1469 &self.0[..]
1470 }
1471 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1472 Node2VecReader::from_slice(slice).map(|reader| reader.to_entity())
1473 }
1474 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1475 Node2VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1476 }
1477 fn new_builder() -> Self::Builder {
1478 ::core::default::Default::default()
1479 }
1480 fn as_builder(self) -> Self::Builder {
1481 Self::new_builder().extend(self.into_iter())
1482 }
1483}
1484#[derive(Clone, Copy)]
1485pub struct Node2VecReader<'r>(&'r [u8]);
1486impl<'r> ::core::fmt::LowerHex for Node2VecReader<'r> {
1487 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1488 use molecule::hex_string;
1489 if f.alternate() {
1490 write!(f, "0x")?;
1491 }
1492 write!(f, "{}", hex_string(self.as_slice()))
1493 }
1494}
1495impl<'r> ::core::fmt::Debug for Node2VecReader<'r> {
1496 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1497 write!(f, "{}({:#x})", Self::NAME, self)
1498 }
1499}
1500impl<'r> ::core::fmt::Display for Node2VecReader<'r> {
1501 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1502 write!(f, "{} [", Self::NAME)?;
1503 for i in 0..self.len() {
1504 if i == 0 {
1505 write!(f, "{}", self.get_unchecked(i))?;
1506 } else {
1507 write!(f, ", {}", self.get_unchecked(i))?;
1508 }
1509 }
1510 write!(f, "]")
1511 }
1512}
1513impl<'r> Node2VecReader<'r> {
1514 pub fn total_size(&self) -> usize {
1515 molecule::unpack_number(self.as_slice()) as usize
1516 }
1517 pub fn item_count(&self) -> usize {
1518 if self.total_size() == molecule::NUMBER_SIZE {
1519 0
1520 } else {
1521 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1522 }
1523 }
1524 pub fn len(&self) -> usize {
1525 self.item_count()
1526 }
1527 pub fn is_empty(&self) -> bool {
1528 self.len() == 0
1529 }
1530 pub fn get(&self, idx: usize) -> Option<Node2Reader<'r>> {
1531 if idx >= self.len() {
1532 None
1533 } else {
1534 Some(self.get_unchecked(idx))
1535 }
1536 }
1537 pub fn get_unchecked(&self, idx: usize) -> Node2Reader<'r> {
1538 let slice = self.as_slice();
1539 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
1540 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
1541 if idx == self.len() - 1 {
1542 Node2Reader::new_unchecked(&self.as_slice()[start..])
1543 } else {
1544 let end_idx = start_idx + molecule::NUMBER_SIZE;
1545 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
1546 Node2Reader::new_unchecked(&self.as_slice()[start..end])
1547 }
1548 }
1549}
1550impl<'r> molecule::prelude::Reader<'r> for Node2VecReader<'r> {
1551 type Entity = Node2Vec;
1552 const NAME: &'static str = "Node2VecReader";
1553 fn to_entity(&self) -> Self::Entity {
1554 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1555 }
1556 fn new_unchecked(slice: &'r [u8]) -> Self {
1557 Node2VecReader(slice)
1558 }
1559 fn as_slice(&self) -> &'r [u8] {
1560 self.0
1561 }
1562 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
1563 use molecule::verification_error as ve;
1564 let slice_len = slice.len();
1565 if slice_len < molecule::NUMBER_SIZE {
1566 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1567 }
1568 let total_size = molecule::unpack_number(slice) as usize;
1569 if slice_len != total_size {
1570 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1571 }
1572 if slice_len == molecule::NUMBER_SIZE {
1573 return Ok(());
1574 }
1575 if slice_len < molecule::NUMBER_SIZE * 2 {
1576 return ve!(
1577 Self,
1578 TotalSizeNotMatch,
1579 molecule::NUMBER_SIZE * 2,
1580 slice_len
1581 );
1582 }
1583 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
1584 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
1585 return ve!(Self, OffsetsNotMatch);
1586 }
1587 if slice_len < offset_first {
1588 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
1589 }
1590 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
1591 .chunks_exact(molecule::NUMBER_SIZE)
1592 .map(|x| molecule::unpack_number(x) as usize)
1593 .collect();
1594 offsets.push(total_size);
1595 if offsets.windows(2).any(|i| i[0] > i[1]) {
1596 return ve!(Self, OffsetsNotMatch);
1597 }
1598 for pair in offsets.windows(2) {
1599 let start = pair[0];
1600 let end = pair[1];
1601 Node2Reader::verify(&slice[start..end], compatible)?;
1602 }
1603 Ok(())
1604 }
1605}
1606#[derive(Clone, Debug, Default)]
1607pub struct Node2VecBuilder(pub(crate) Vec<Node2>);
1608impl Node2VecBuilder {
1609 pub fn set(mut self, v: Vec<Node2>) -> Self {
1610 self.0 = v;
1611 self
1612 }
1613 pub fn push<T>(mut self, v: T) -> Self
1614 where
1615 T: ::core::convert::Into<Node2>,
1616 {
1617 self.0.push(v.into());
1618 self
1619 }
1620 pub fn extend<T: ::core::iter::IntoIterator<Item = Node2>>(mut self, iter: T) -> Self {
1621 self.0.extend(iter);
1622 self
1623 }
1624 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Node2>
1625 where
1626 T: ::core::convert::Into<Node2>,
1627 {
1628 self.0
1629 .get_mut(index)
1630 .map(|item| ::core::mem::replace(item, v.into()))
1631 }
1632}
1633impl molecule::prelude::Builder for Node2VecBuilder {
1634 type Entity = Node2Vec;
1635 const NAME: &'static str = "Node2VecBuilder";
1636 fn expected_length(&self) -> usize {
1637 molecule::NUMBER_SIZE * (self.0.len() + 1)
1638 + self
1639 .0
1640 .iter()
1641 .map(|inner| inner.as_slice().len())
1642 .sum::<usize>()
1643 }
1644 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1645 let item_count = self.0.len();
1646 if item_count == 0 {
1647 writer.write_all(&molecule::pack_number(
1648 molecule::NUMBER_SIZE as molecule::Number,
1649 ))?;
1650 } else {
1651 let (total_size, offsets) = self.0.iter().fold(
1652 (
1653 molecule::NUMBER_SIZE * (item_count + 1),
1654 Vec::with_capacity(item_count),
1655 ),
1656 |(start, mut offsets), inner| {
1657 offsets.push(start);
1658 (start + inner.as_slice().len(), offsets)
1659 },
1660 );
1661 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
1662 for offset in offsets.into_iter() {
1663 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
1664 }
1665 for inner in self.0.iter() {
1666 writer.write_all(inner.as_slice())?;
1667 }
1668 }
1669 Ok(())
1670 }
1671 fn build(&self) -> Self::Entity {
1672 let mut inner = Vec::with_capacity(self.expected_length());
1673 self.write(&mut inner)
1674 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1675 Node2Vec::new_unchecked(inner.into())
1676 }
1677}
1678pub struct Node2VecIterator(Node2Vec, usize, usize);
1679impl ::core::iter::Iterator for Node2VecIterator {
1680 type Item = Node2;
1681 fn next(&mut self) -> Option<Self::Item> {
1682 if self.1 >= self.2 {
1683 None
1684 } else {
1685 let ret = self.0.get_unchecked(self.1);
1686 self.1 += 1;
1687 Some(ret)
1688 }
1689 }
1690}
1691impl ::core::iter::ExactSizeIterator for Node2VecIterator {
1692 fn len(&self) -> usize {
1693 self.2 - self.1
1694 }
1695}
1696impl ::core::iter::IntoIterator for Node2Vec {
1697 type Item = Node2;
1698 type IntoIter = Node2VecIterator;
1699 fn into_iter(self) -> Self::IntoIter {
1700 let len = self.len();
1701 Node2VecIterator(self, 0, len)
1702 }
1703}
1704impl<'r> Node2VecReader<'r> {
1705 pub fn iter<'t>(&'t self) -> Node2VecReaderIterator<'t, 'r> {
1706 Node2VecReaderIterator(&self, 0, self.len())
1707 }
1708}
1709pub struct Node2VecReaderIterator<'t, 'r>(&'t Node2VecReader<'r>, usize, usize);
1710impl<'t: 'r, 'r> ::core::iter::Iterator for Node2VecReaderIterator<'t, 'r> {
1711 type Item = Node2Reader<'t>;
1712 fn next(&mut self) -> Option<Self::Item> {
1713 if self.1 >= self.2 {
1714 None
1715 } else {
1716 let ret = self.0.get_unchecked(self.1);
1717 self.1 += 1;
1718 Some(ret)
1719 }
1720 }
1721}
1722impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Node2VecReaderIterator<'t, 'r> {
1723 fn len(&self) -> usize {
1724 self.2 - self.1
1725 }
1726}
1727impl<T> ::core::iter::FromIterator<T> for Node2Vec
1728where
1729 T: Into<Node2>,
1730{
1731 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1732 Self::new_builder()
1733 .extend(iter.into_iter().map(Into::into))
1734 .build()
1735 }
1736}
1737impl<T> From<Vec<T>> for Node2Vec
1738where
1739 T: Into<Node2>,
1740{
1741 fn from(v: Vec<T>) -> Self {
1742 v.into_iter().collect()
1743 }
1744}
1745#[derive(Clone)]
1746pub struct Uint16(molecule::bytes::Bytes);
1747impl ::core::fmt::LowerHex for Uint16 {
1748 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1749 use molecule::hex_string;
1750 if f.alternate() {
1751 write!(f, "0x")?;
1752 }
1753 write!(f, "{}", hex_string(self.as_slice()))
1754 }
1755}
1756impl ::core::fmt::Debug for Uint16 {
1757 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1758 write!(f, "{}({:#x})", Self::NAME, self)
1759 }
1760}
1761impl ::core::fmt::Display for Uint16 {
1762 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1763 use molecule::hex_string;
1764 let raw_data = hex_string(&self.raw_data());
1765 write!(f, "{}(0x{})", Self::NAME, raw_data)
1766 }
1767}
1768impl ::core::default::Default for Uint16 {
1769 fn default() -> Self {
1770 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1771 Uint16::new_unchecked(v)
1772 }
1773}
1774impl Uint16 {
1775 const DEFAULT_VALUE: [u8; 2] = [0, 0];
1776 pub const TOTAL_SIZE: usize = 2;
1777 pub const ITEM_SIZE: usize = 1;
1778 pub const ITEM_COUNT: usize = 2;
1779 pub fn nth0(&self) -> Byte {
1780 Byte::new_unchecked(self.0.slice(0..1))
1781 }
1782 pub fn nth1(&self) -> Byte {
1783 Byte::new_unchecked(self.0.slice(1..2))
1784 }
1785 pub fn raw_data(&self) -> molecule::bytes::Bytes {
1786 self.as_bytes()
1787 }
1788 pub fn as_reader<'r>(&'r self) -> Uint16Reader<'r> {
1789 Uint16Reader::new_unchecked(self.as_slice())
1790 }
1791}
1792impl molecule::prelude::Entity for Uint16 {
1793 type Builder = Uint16Builder;
1794 const NAME: &'static str = "Uint16";
1795 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1796 Uint16(data)
1797 }
1798 fn as_bytes(&self) -> molecule::bytes::Bytes {
1799 self.0.clone()
1800 }
1801 fn as_slice(&self) -> &[u8] {
1802 &self.0[..]
1803 }
1804 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1805 Uint16Reader::from_slice(slice).map(|reader| reader.to_entity())
1806 }
1807 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1808 Uint16Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1809 }
1810 fn new_builder() -> Self::Builder {
1811 ::core::default::Default::default()
1812 }
1813 fn as_builder(self) -> Self::Builder {
1814 Self::new_builder().set([self.nth0(), self.nth1()])
1815 }
1816}
1817#[derive(Clone, Copy)]
1818pub struct Uint16Reader<'r>(&'r [u8]);
1819impl<'r> ::core::fmt::LowerHex for Uint16Reader<'r> {
1820 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1821 use molecule::hex_string;
1822 if f.alternate() {
1823 write!(f, "0x")?;
1824 }
1825 write!(f, "{}", hex_string(self.as_slice()))
1826 }
1827}
1828impl<'r> ::core::fmt::Debug for Uint16Reader<'r> {
1829 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1830 write!(f, "{}({:#x})", Self::NAME, self)
1831 }
1832}
1833impl<'r> ::core::fmt::Display for Uint16Reader<'r> {
1834 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1835 use molecule::hex_string;
1836 let raw_data = hex_string(&self.raw_data());
1837 write!(f, "{}(0x{})", Self::NAME, raw_data)
1838 }
1839}
1840impl<'r> Uint16Reader<'r> {
1841 pub const TOTAL_SIZE: usize = 2;
1842 pub const ITEM_SIZE: usize = 1;
1843 pub const ITEM_COUNT: usize = 2;
1844 pub fn nth0(&self) -> ByteReader<'r> {
1845 ByteReader::new_unchecked(&self.as_slice()[0..1])
1846 }
1847 pub fn nth1(&self) -> ByteReader<'r> {
1848 ByteReader::new_unchecked(&self.as_slice()[1..2])
1849 }
1850 pub fn raw_data(&self) -> &'r [u8] {
1851 self.as_slice()
1852 }
1853}
1854impl<'r> molecule::prelude::Reader<'r> for Uint16Reader<'r> {
1855 type Entity = Uint16;
1856 const NAME: &'static str = "Uint16Reader";
1857 fn to_entity(&self) -> Self::Entity {
1858 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1859 }
1860 fn new_unchecked(slice: &'r [u8]) -> Self {
1861 Uint16Reader(slice)
1862 }
1863 fn as_slice(&self) -> &'r [u8] {
1864 self.0
1865 }
1866 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1867 use molecule::verification_error as ve;
1868 let slice_len = slice.len();
1869 if slice_len != Self::TOTAL_SIZE {
1870 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
1871 }
1872 Ok(())
1873 }
1874}
1875#[derive(Clone)]
1876pub struct Uint16Builder(pub(crate) [Byte; 2]);
1877impl ::core::fmt::Debug for Uint16Builder {
1878 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1879 write!(f, "{}({:?})", Self::NAME, &self.0[..])
1880 }
1881}
1882impl ::core::default::Default for Uint16Builder {
1883 fn default() -> Self {
1884 Uint16Builder([Byte::default(), Byte::default()])
1885 }
1886}
1887impl Uint16Builder {
1888 pub const TOTAL_SIZE: usize = 2;
1889 pub const ITEM_SIZE: usize = 1;
1890 pub const ITEM_COUNT: usize = 2;
1891 pub fn set<T>(mut self, v: T) -> Self
1892 where
1893 T: ::core::convert::Into<[Byte; 2]>,
1894 {
1895 self.0 = v.into();
1896 self
1897 }
1898 pub fn nth0<T>(mut self, v: T) -> Self
1899 where
1900 T: ::core::convert::Into<Byte>,
1901 {
1902 self.0[0] = v.into();
1903 self
1904 }
1905 pub fn nth1<T>(mut self, v: T) -> Self
1906 where
1907 T: ::core::convert::Into<Byte>,
1908 {
1909 self.0[1] = v.into();
1910 self
1911 }
1912}
1913impl molecule::prelude::Builder for Uint16Builder {
1914 type Entity = Uint16;
1915 const NAME: &'static str = "Uint16Builder";
1916 fn expected_length(&self) -> usize {
1917 Self::TOTAL_SIZE
1918 }
1919 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1920 writer.write_all(self.0[0].as_slice())?;
1921 writer.write_all(self.0[1].as_slice())?;
1922 Ok(())
1923 }
1924 fn build(&self) -> Self::Entity {
1925 let mut inner = Vec::with_capacity(self.expected_length());
1926 self.write(&mut inner)
1927 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1928 Uint16::new_unchecked(inner.into())
1929 }
1930}
1931impl From<[Byte; 2usize]> for Uint16 {
1932 fn from(value: [Byte; 2usize]) -> Self {
1933 Self::new_builder().set(value).build()
1934 }
1935}
1936impl ::core::convert::TryFrom<&[Byte]> for Uint16 {
1937 type Error = ::core::array::TryFromSliceError;
1938 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
1939 Ok(Self::new_builder()
1940 .set(<&[Byte; 2usize]>::try_from(value)?.clone())
1941 .build())
1942 }
1943}
1944impl From<Uint16> for [Byte; 2usize] {
1945 #[track_caller]
1946 fn from(value: Uint16) -> Self {
1947 [value.nth0(), value.nth1()]
1948 }
1949}
1950impl From<[u8; 2usize]> for Uint16 {
1951 fn from(value: [u8; 2usize]) -> Self {
1952 Uint16Reader::new_unchecked(&value).to_entity()
1953 }
1954}
1955impl ::core::convert::TryFrom<&[u8]> for Uint16 {
1956 type Error = ::core::array::TryFromSliceError;
1957 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
1958 Ok(<[u8; 2usize]>::try_from(value)?.into())
1959 }
1960}
1961impl From<Uint16> for [u8; 2usize] {
1962 #[track_caller]
1963 fn from(value: Uint16) -> Self {
1964 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1965 }
1966}
1967impl<'a> From<Uint16Reader<'a>> for &'a [u8; 2usize] {
1968 #[track_caller]
1969 fn from(value: Uint16Reader<'a>) -> Self {
1970 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1971 }
1972}
1973impl<'a> From<&'a Uint16Reader<'a>> for &'a [u8; 2usize] {
1974 #[track_caller]
1975 fn from(value: &'a Uint16Reader<'a>) -> Self {
1976 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1977 }
1978}
1979#[derive(Clone)]
1980pub struct PortOpt(molecule::bytes::Bytes);
1981impl ::core::fmt::LowerHex for PortOpt {
1982 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1983 use molecule::hex_string;
1984 if f.alternate() {
1985 write!(f, "0x")?;
1986 }
1987 write!(f, "{}", hex_string(self.as_slice()))
1988 }
1989}
1990impl ::core::fmt::Debug for PortOpt {
1991 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1992 write!(f, "{}({:#x})", Self::NAME, self)
1993 }
1994}
1995impl ::core::fmt::Display for PortOpt {
1996 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1997 if let Some(v) = self.to_opt() {
1998 write!(f, "{}(Some({}))", Self::NAME, v)
1999 } else {
2000 write!(f, "{}(None)", Self::NAME)
2001 }
2002 }
2003}
2004impl ::core::default::Default for PortOpt {
2005 fn default() -> Self {
2006 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2007 PortOpt::new_unchecked(v)
2008 }
2009}
2010impl PortOpt {
2011 const DEFAULT_VALUE: [u8; 0] = [];
2012 pub fn is_none(&self) -> bool {
2013 self.0.is_empty()
2014 }
2015 pub fn is_some(&self) -> bool {
2016 !self.0.is_empty()
2017 }
2018 pub fn to_opt(&self) -> Option<Uint16> {
2019 if self.is_none() {
2020 None
2021 } else {
2022 Some(Uint16::new_unchecked(self.0.clone()))
2023 }
2024 }
2025 pub fn as_reader<'r>(&'r self) -> PortOptReader<'r> {
2026 PortOptReader::new_unchecked(self.as_slice())
2027 }
2028}
2029impl molecule::prelude::Entity for PortOpt {
2030 type Builder = PortOptBuilder;
2031 const NAME: &'static str = "PortOpt";
2032 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2033 PortOpt(data)
2034 }
2035 fn as_bytes(&self) -> molecule::bytes::Bytes {
2036 self.0.clone()
2037 }
2038 fn as_slice(&self) -> &[u8] {
2039 &self.0[..]
2040 }
2041 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2042 PortOptReader::from_slice(slice).map(|reader| reader.to_entity())
2043 }
2044 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2045 PortOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2046 }
2047 fn new_builder() -> Self::Builder {
2048 ::core::default::Default::default()
2049 }
2050 fn as_builder(self) -> Self::Builder {
2051 Self::new_builder().set(self.to_opt())
2052 }
2053}
2054#[derive(Clone, Copy)]
2055pub struct PortOptReader<'r>(&'r [u8]);
2056impl<'r> ::core::fmt::LowerHex for PortOptReader<'r> {
2057 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2058 use molecule::hex_string;
2059 if f.alternate() {
2060 write!(f, "0x")?;
2061 }
2062 write!(f, "{}", hex_string(self.as_slice()))
2063 }
2064}
2065impl<'r> ::core::fmt::Debug for PortOptReader<'r> {
2066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2067 write!(f, "{}({:#x})", Self::NAME, self)
2068 }
2069}
2070impl<'r> ::core::fmt::Display for PortOptReader<'r> {
2071 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2072 if let Some(v) = self.to_opt() {
2073 write!(f, "{}(Some({}))", Self::NAME, v)
2074 } else {
2075 write!(f, "{}(None)", Self::NAME)
2076 }
2077 }
2078}
2079impl<'r> PortOptReader<'r> {
2080 pub fn is_none(&self) -> bool {
2081 self.0.is_empty()
2082 }
2083 pub fn is_some(&self) -> bool {
2084 !self.0.is_empty()
2085 }
2086 pub fn to_opt(&self) -> Option<Uint16Reader<'r>> {
2087 if self.is_none() {
2088 None
2089 } else {
2090 Some(Uint16Reader::new_unchecked(self.as_slice()))
2091 }
2092 }
2093}
2094impl<'r> molecule::prelude::Reader<'r> for PortOptReader<'r> {
2095 type Entity = PortOpt;
2096 const NAME: &'static str = "PortOptReader";
2097 fn to_entity(&self) -> Self::Entity {
2098 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2099 }
2100 fn new_unchecked(slice: &'r [u8]) -> Self {
2101 PortOptReader(slice)
2102 }
2103 fn as_slice(&self) -> &'r [u8] {
2104 self.0
2105 }
2106 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2107 if !slice.is_empty() {
2108 Uint16Reader::verify(&slice[..], compatible)?;
2109 }
2110 Ok(())
2111 }
2112}
2113#[derive(Clone, Debug, Default)]
2114pub struct PortOptBuilder(pub(crate) Option<Uint16>);
2115impl PortOptBuilder {
2116 pub fn set<T>(mut self, v: T) -> Self
2117 where
2118 T: ::core::convert::Into<Option<Uint16>>,
2119 {
2120 self.0 = v.into();
2121 self
2122 }
2123}
2124impl molecule::prelude::Builder for PortOptBuilder {
2125 type Entity = PortOpt;
2126 const NAME: &'static str = "PortOptBuilder";
2127 fn expected_length(&self) -> usize {
2128 self.0
2129 .as_ref()
2130 .map(|ref inner| inner.as_slice().len())
2131 .unwrap_or(0)
2132 }
2133 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2134 self.0
2135 .as_ref()
2136 .map(|ref inner| writer.write_all(inner.as_slice()))
2137 .unwrap_or(Ok(()))
2138 }
2139 fn build(&self) -> Self::Entity {
2140 let mut inner = Vec::with_capacity(self.expected_length());
2141 self.write(&mut inner)
2142 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2143 PortOpt::new_unchecked(inner.into())
2144 }
2145}
2146impl From<Uint16> for PortOpt {
2147 fn from(value: Uint16) -> Self {
2148 Self::new_builder().set(Some(value)).build()
2149 }
2150}
2151#[derive(Clone)]
2152pub struct DiscoveryPayload(molecule::bytes::Bytes);
2153impl ::core::fmt::LowerHex for DiscoveryPayload {
2154 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2155 use molecule::hex_string;
2156 if f.alternate() {
2157 write!(f, "0x")?;
2158 }
2159 write!(f, "{}", hex_string(self.as_slice()))
2160 }
2161}
2162impl ::core::fmt::Debug for DiscoveryPayload {
2163 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2164 write!(f, "{}({:#x})", Self::NAME, self)
2165 }
2166}
2167impl ::core::fmt::Display for DiscoveryPayload {
2168 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2169 write!(f, "{}(", Self::NAME)?;
2170 self.to_enum().display_inner(f)?;
2171 write!(f, ")")
2172 }
2173}
2174impl ::core::default::Default for DiscoveryPayload {
2175 fn default() -> Self {
2176 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2177 DiscoveryPayload::new_unchecked(v)
2178 }
2179}
2180impl DiscoveryPayload {
2181 const DEFAULT_VALUE: [u8; 28] = [
2182 0, 0, 0, 0, 24, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2183 ];
2184 pub const ITEMS_COUNT: usize = 2;
2185 pub fn item_id(&self) -> molecule::Number {
2186 molecule::unpack_number(self.as_slice())
2187 }
2188 pub fn to_enum(&self) -> DiscoveryPayloadUnion {
2189 let inner = self.0.slice(molecule::NUMBER_SIZE..);
2190 match self.item_id() {
2191 0 => GetNodes::new_unchecked(inner).into(),
2192 1 => Nodes::new_unchecked(inner).into(),
2193 _ => panic!("{}: invalid data", Self::NAME),
2194 }
2195 }
2196 pub fn as_reader<'r>(&'r self) -> DiscoveryPayloadReader<'r> {
2197 DiscoveryPayloadReader::new_unchecked(self.as_slice())
2198 }
2199}
2200impl molecule::prelude::Entity for DiscoveryPayload {
2201 type Builder = DiscoveryPayloadBuilder;
2202 const NAME: &'static str = "DiscoveryPayload";
2203 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2204 DiscoveryPayload(data)
2205 }
2206 fn as_bytes(&self) -> molecule::bytes::Bytes {
2207 self.0.clone()
2208 }
2209 fn as_slice(&self) -> &[u8] {
2210 &self.0[..]
2211 }
2212 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2213 DiscoveryPayloadReader::from_slice(slice).map(|reader| reader.to_entity())
2214 }
2215 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2216 DiscoveryPayloadReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2217 }
2218 fn new_builder() -> Self::Builder {
2219 ::core::default::Default::default()
2220 }
2221 fn as_builder(self) -> Self::Builder {
2222 Self::new_builder().set(self.to_enum())
2223 }
2224}
2225#[derive(Clone, Copy)]
2226pub struct DiscoveryPayloadReader<'r>(&'r [u8]);
2227impl<'r> ::core::fmt::LowerHex for DiscoveryPayloadReader<'r> {
2228 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2229 use molecule::hex_string;
2230 if f.alternate() {
2231 write!(f, "0x")?;
2232 }
2233 write!(f, "{}", hex_string(self.as_slice()))
2234 }
2235}
2236impl<'r> ::core::fmt::Debug for DiscoveryPayloadReader<'r> {
2237 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2238 write!(f, "{}({:#x})", Self::NAME, self)
2239 }
2240}
2241impl<'r> ::core::fmt::Display for DiscoveryPayloadReader<'r> {
2242 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2243 write!(f, "{}(", Self::NAME)?;
2244 self.to_enum().display_inner(f)?;
2245 write!(f, ")")
2246 }
2247}
2248impl<'r> DiscoveryPayloadReader<'r> {
2249 pub const ITEMS_COUNT: usize = 2;
2250 pub fn item_id(&self) -> molecule::Number {
2251 molecule::unpack_number(self.as_slice())
2252 }
2253 pub fn to_enum(&self) -> DiscoveryPayloadUnionReader<'r> {
2254 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
2255 match self.item_id() {
2256 0 => GetNodesReader::new_unchecked(inner).into(),
2257 1 => NodesReader::new_unchecked(inner).into(),
2258 _ => panic!("{}: invalid data", Self::NAME),
2259 }
2260 }
2261}
2262impl<'r> molecule::prelude::Reader<'r> for DiscoveryPayloadReader<'r> {
2263 type Entity = DiscoveryPayload;
2264 const NAME: &'static str = "DiscoveryPayloadReader";
2265 fn to_entity(&self) -> Self::Entity {
2266 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2267 }
2268 fn new_unchecked(slice: &'r [u8]) -> Self {
2269 DiscoveryPayloadReader(slice)
2270 }
2271 fn as_slice(&self) -> &'r [u8] {
2272 self.0
2273 }
2274 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2275 use molecule::verification_error as ve;
2276 let slice_len = slice.len();
2277 if slice_len < molecule::NUMBER_SIZE {
2278 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2279 }
2280 let item_id = molecule::unpack_number(slice);
2281 let inner_slice = &slice[molecule::NUMBER_SIZE..];
2282 match item_id {
2283 0 => GetNodesReader::verify(inner_slice, compatible),
2284 1 => NodesReader::verify(inner_slice, compatible),
2285 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
2286 }?;
2287 Ok(())
2288 }
2289}
2290#[derive(Clone, Debug, Default)]
2291pub struct DiscoveryPayloadBuilder(pub(crate) DiscoveryPayloadUnion);
2292impl DiscoveryPayloadBuilder {
2293 pub const ITEMS_COUNT: usize = 2;
2294 pub fn set<I>(mut self, v: I) -> Self
2295 where
2296 I: ::core::convert::Into<DiscoveryPayloadUnion>,
2297 {
2298 self.0 = v.into();
2299 self
2300 }
2301}
2302impl molecule::prelude::Builder for DiscoveryPayloadBuilder {
2303 type Entity = DiscoveryPayload;
2304 const NAME: &'static str = "DiscoveryPayloadBuilder";
2305 fn expected_length(&self) -> usize {
2306 molecule::NUMBER_SIZE + self.0.as_slice().len()
2307 }
2308 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2309 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
2310 writer.write_all(self.0.as_slice())
2311 }
2312 fn build(&self) -> Self::Entity {
2313 let mut inner = Vec::with_capacity(self.expected_length());
2314 self.write(&mut inner)
2315 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2316 DiscoveryPayload::new_unchecked(inner.into())
2317 }
2318}
2319#[derive(Debug, Clone)]
2320pub enum DiscoveryPayloadUnion {
2321 GetNodes(GetNodes),
2322 Nodes(Nodes),
2323}
2324#[derive(Debug, Clone, Copy)]
2325pub enum DiscoveryPayloadUnionReader<'r> {
2326 GetNodes(GetNodesReader<'r>),
2327 Nodes(NodesReader<'r>),
2328}
2329impl ::core::default::Default for DiscoveryPayloadUnion {
2330 fn default() -> Self {
2331 DiscoveryPayloadUnion::GetNodes(::core::default::Default::default())
2332 }
2333}
2334impl ::core::fmt::Display for DiscoveryPayloadUnion {
2335 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2336 match self {
2337 DiscoveryPayloadUnion::GetNodes(ref item) => {
2338 write!(f, "{}::{}({})", Self::NAME, GetNodes::NAME, item)
2339 }
2340 DiscoveryPayloadUnion::Nodes(ref item) => {
2341 write!(f, "{}::{}({})", Self::NAME, Nodes::NAME, item)
2342 }
2343 }
2344 }
2345}
2346impl<'r> ::core::fmt::Display for DiscoveryPayloadUnionReader<'r> {
2347 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2348 match self {
2349 DiscoveryPayloadUnionReader::GetNodes(ref item) => {
2350 write!(f, "{}::{}({})", Self::NAME, GetNodes::NAME, item)
2351 }
2352 DiscoveryPayloadUnionReader::Nodes(ref item) => {
2353 write!(f, "{}::{}({})", Self::NAME, Nodes::NAME, item)
2354 }
2355 }
2356 }
2357}
2358impl DiscoveryPayloadUnion {
2359 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2360 match self {
2361 DiscoveryPayloadUnion::GetNodes(ref item) => write!(f, "{}", item),
2362 DiscoveryPayloadUnion::Nodes(ref item) => write!(f, "{}", item),
2363 }
2364 }
2365}
2366impl<'r> DiscoveryPayloadUnionReader<'r> {
2367 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2368 match self {
2369 DiscoveryPayloadUnionReader::GetNodes(ref item) => write!(f, "{}", item),
2370 DiscoveryPayloadUnionReader::Nodes(ref item) => write!(f, "{}", item),
2371 }
2372 }
2373}
2374impl ::core::convert::From<GetNodes> for DiscoveryPayloadUnion {
2375 fn from(item: GetNodes) -> Self {
2376 DiscoveryPayloadUnion::GetNodes(item)
2377 }
2378}
2379impl ::core::convert::From<Nodes> for DiscoveryPayloadUnion {
2380 fn from(item: Nodes) -> Self {
2381 DiscoveryPayloadUnion::Nodes(item)
2382 }
2383}
2384impl<'r> ::core::convert::From<GetNodesReader<'r>> for DiscoveryPayloadUnionReader<'r> {
2385 fn from(item: GetNodesReader<'r>) -> Self {
2386 DiscoveryPayloadUnionReader::GetNodes(item)
2387 }
2388}
2389impl<'r> ::core::convert::From<NodesReader<'r>> for DiscoveryPayloadUnionReader<'r> {
2390 fn from(item: NodesReader<'r>) -> Self {
2391 DiscoveryPayloadUnionReader::Nodes(item)
2392 }
2393}
2394impl DiscoveryPayloadUnion {
2395 pub const NAME: &'static str = "DiscoveryPayloadUnion";
2396 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
2397 match self {
2398 DiscoveryPayloadUnion::GetNodes(item) => item.as_bytes(),
2399 DiscoveryPayloadUnion::Nodes(item) => item.as_bytes(),
2400 }
2401 }
2402 pub fn as_slice(&self) -> &[u8] {
2403 match self {
2404 DiscoveryPayloadUnion::GetNodes(item) => item.as_slice(),
2405 DiscoveryPayloadUnion::Nodes(item) => item.as_slice(),
2406 }
2407 }
2408 pub fn item_id(&self) -> molecule::Number {
2409 match self {
2410 DiscoveryPayloadUnion::GetNodes(_) => 0,
2411 DiscoveryPayloadUnion::Nodes(_) => 1,
2412 }
2413 }
2414 pub fn item_name(&self) -> &str {
2415 match self {
2416 DiscoveryPayloadUnion::GetNodes(_) => "GetNodes",
2417 DiscoveryPayloadUnion::Nodes(_) => "Nodes",
2418 }
2419 }
2420 pub fn as_reader<'r>(&'r self) -> DiscoveryPayloadUnionReader<'r> {
2421 match self {
2422 DiscoveryPayloadUnion::GetNodes(item) => item.as_reader().into(),
2423 DiscoveryPayloadUnion::Nodes(item) => item.as_reader().into(),
2424 }
2425 }
2426}
2427impl<'r> DiscoveryPayloadUnionReader<'r> {
2428 pub const NAME: &'r str = "DiscoveryPayloadUnionReader";
2429 pub fn as_slice(&self) -> &'r [u8] {
2430 match self {
2431 DiscoveryPayloadUnionReader::GetNodes(item) => item.as_slice(),
2432 DiscoveryPayloadUnionReader::Nodes(item) => item.as_slice(),
2433 }
2434 }
2435 pub fn item_id(&self) -> molecule::Number {
2436 match self {
2437 DiscoveryPayloadUnionReader::GetNodes(_) => 0,
2438 DiscoveryPayloadUnionReader::Nodes(_) => 1,
2439 }
2440 }
2441 pub fn item_name(&self) -> &str {
2442 match self {
2443 DiscoveryPayloadUnionReader::GetNodes(_) => "GetNodes",
2444 DiscoveryPayloadUnionReader::Nodes(_) => "Nodes",
2445 }
2446 }
2447}
2448impl From<GetNodes> for DiscoveryPayload {
2449 fn from(value: GetNodes) -> Self {
2450 Self::new_builder().set(value).build()
2451 }
2452}
2453impl From<Nodes> for DiscoveryPayload {
2454 fn from(value: Nodes) -> Self {
2455 Self::new_builder().set(value).build()
2456 }
2457}
2458#[derive(Clone)]
2459pub struct DiscoveryMessage(molecule::bytes::Bytes);
2460impl ::core::fmt::LowerHex for DiscoveryMessage {
2461 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2462 use molecule::hex_string;
2463 if f.alternate() {
2464 write!(f, "0x")?;
2465 }
2466 write!(f, "{}", hex_string(self.as_slice()))
2467 }
2468}
2469impl ::core::fmt::Debug for DiscoveryMessage {
2470 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2471 write!(f, "{}({:#x})", Self::NAME, self)
2472 }
2473}
2474impl ::core::fmt::Display for DiscoveryMessage {
2475 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2476 write!(f, "{} {{ ", Self::NAME)?;
2477 write!(f, "{}: {}", "payload", self.payload())?;
2478 let extra_count = self.count_extra_fields();
2479 if extra_count != 0 {
2480 write!(f, ", .. ({} fields)", extra_count)?;
2481 }
2482 write!(f, " }}")
2483 }
2484}
2485impl ::core::default::Default for DiscoveryMessage {
2486 fn default() -> Self {
2487 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2488 DiscoveryMessage::new_unchecked(v)
2489 }
2490}
2491impl DiscoveryMessage {
2492 const DEFAULT_VALUE: [u8; 36] = [
2493 36, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0,
2494 0, 0, 0, 0, 0, 0, 0,
2495 ];
2496 pub const FIELD_COUNT: usize = 1;
2497 pub fn total_size(&self) -> usize {
2498 molecule::unpack_number(self.as_slice()) as usize
2499 }
2500 pub fn field_count(&self) -> usize {
2501 if self.total_size() == molecule::NUMBER_SIZE {
2502 0
2503 } else {
2504 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2505 }
2506 }
2507 pub fn count_extra_fields(&self) -> usize {
2508 self.field_count() - Self::FIELD_COUNT
2509 }
2510 pub fn has_extra_fields(&self) -> bool {
2511 Self::FIELD_COUNT != self.field_count()
2512 }
2513 pub fn payload(&self) -> DiscoveryPayload {
2514 let slice = self.as_slice();
2515 let start = molecule::unpack_number(&slice[4..]) as usize;
2516 if self.has_extra_fields() {
2517 let end = molecule::unpack_number(&slice[8..]) as usize;
2518 DiscoveryPayload::new_unchecked(self.0.slice(start..end))
2519 } else {
2520 DiscoveryPayload::new_unchecked(self.0.slice(start..))
2521 }
2522 }
2523 pub fn as_reader<'r>(&'r self) -> DiscoveryMessageReader<'r> {
2524 DiscoveryMessageReader::new_unchecked(self.as_slice())
2525 }
2526}
2527impl molecule::prelude::Entity for DiscoveryMessage {
2528 type Builder = DiscoveryMessageBuilder;
2529 const NAME: &'static str = "DiscoveryMessage";
2530 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2531 DiscoveryMessage(data)
2532 }
2533 fn as_bytes(&self) -> molecule::bytes::Bytes {
2534 self.0.clone()
2535 }
2536 fn as_slice(&self) -> &[u8] {
2537 &self.0[..]
2538 }
2539 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2540 DiscoveryMessageReader::from_slice(slice).map(|reader| reader.to_entity())
2541 }
2542 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2543 DiscoveryMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2544 }
2545 fn new_builder() -> Self::Builder {
2546 ::core::default::Default::default()
2547 }
2548 fn as_builder(self) -> Self::Builder {
2549 Self::new_builder().payload(self.payload())
2550 }
2551}
2552#[derive(Clone, Copy)]
2553pub struct DiscoveryMessageReader<'r>(&'r [u8]);
2554impl<'r> ::core::fmt::LowerHex for DiscoveryMessageReader<'r> {
2555 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2556 use molecule::hex_string;
2557 if f.alternate() {
2558 write!(f, "0x")?;
2559 }
2560 write!(f, "{}", hex_string(self.as_slice()))
2561 }
2562}
2563impl<'r> ::core::fmt::Debug for DiscoveryMessageReader<'r> {
2564 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2565 write!(f, "{}({:#x})", Self::NAME, self)
2566 }
2567}
2568impl<'r> ::core::fmt::Display for DiscoveryMessageReader<'r> {
2569 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2570 write!(f, "{} {{ ", Self::NAME)?;
2571 write!(f, "{}: {}", "payload", self.payload())?;
2572 let extra_count = self.count_extra_fields();
2573 if extra_count != 0 {
2574 write!(f, ", .. ({} fields)", extra_count)?;
2575 }
2576 write!(f, " }}")
2577 }
2578}
2579impl<'r> DiscoveryMessageReader<'r> {
2580 pub const FIELD_COUNT: usize = 1;
2581 pub fn total_size(&self) -> usize {
2582 molecule::unpack_number(self.as_slice()) as usize
2583 }
2584 pub fn field_count(&self) -> usize {
2585 if self.total_size() == molecule::NUMBER_SIZE {
2586 0
2587 } else {
2588 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2589 }
2590 }
2591 pub fn count_extra_fields(&self) -> usize {
2592 self.field_count() - Self::FIELD_COUNT
2593 }
2594 pub fn has_extra_fields(&self) -> bool {
2595 Self::FIELD_COUNT != self.field_count()
2596 }
2597 pub fn payload(&self) -> DiscoveryPayloadReader<'r> {
2598 let slice = self.as_slice();
2599 let start = molecule::unpack_number(&slice[4..]) as usize;
2600 if self.has_extra_fields() {
2601 let end = molecule::unpack_number(&slice[8..]) as usize;
2602 DiscoveryPayloadReader::new_unchecked(&self.as_slice()[start..end])
2603 } else {
2604 DiscoveryPayloadReader::new_unchecked(&self.as_slice()[start..])
2605 }
2606 }
2607}
2608impl<'r> molecule::prelude::Reader<'r> for DiscoveryMessageReader<'r> {
2609 type Entity = DiscoveryMessage;
2610 const NAME: &'static str = "DiscoveryMessageReader";
2611 fn to_entity(&self) -> Self::Entity {
2612 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2613 }
2614 fn new_unchecked(slice: &'r [u8]) -> Self {
2615 DiscoveryMessageReader(slice)
2616 }
2617 fn as_slice(&self) -> &'r [u8] {
2618 self.0
2619 }
2620 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2621 use molecule::verification_error as ve;
2622 let slice_len = slice.len();
2623 if slice_len < molecule::NUMBER_SIZE {
2624 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2625 }
2626 let total_size = molecule::unpack_number(slice) as usize;
2627 if slice_len != total_size {
2628 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2629 }
2630 if slice_len < molecule::NUMBER_SIZE * 2 {
2631 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
2632 }
2633 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
2634 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
2635 return ve!(Self, OffsetsNotMatch);
2636 }
2637 if slice_len < offset_first {
2638 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
2639 }
2640 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
2641 if field_count < Self::FIELD_COUNT {
2642 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2643 } else if !compatible && field_count > Self::FIELD_COUNT {
2644 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2645 };
2646 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
2647 .chunks_exact(molecule::NUMBER_SIZE)
2648 .map(|x| molecule::unpack_number(x) as usize)
2649 .collect();
2650 offsets.push(total_size);
2651 if offsets.windows(2).any(|i| i[0] > i[1]) {
2652 return ve!(Self, OffsetsNotMatch);
2653 }
2654 DiscoveryPayloadReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
2655 Ok(())
2656 }
2657}
2658#[derive(Clone, Debug, Default)]
2659pub struct DiscoveryMessageBuilder {
2660 pub(crate) payload: DiscoveryPayload,
2661}
2662impl DiscoveryMessageBuilder {
2663 pub const FIELD_COUNT: usize = 1;
2664 pub fn payload<T>(mut self, v: T) -> Self
2665 where
2666 T: ::core::convert::Into<DiscoveryPayload>,
2667 {
2668 self.payload = v.into();
2669 self
2670 }
2671}
2672impl molecule::prelude::Builder for DiscoveryMessageBuilder {
2673 type Entity = DiscoveryMessage;
2674 const NAME: &'static str = "DiscoveryMessageBuilder";
2675 fn expected_length(&self) -> usize {
2676 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.payload.as_slice().len()
2677 }
2678 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2679 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
2680 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
2681 offsets.push(total_size);
2682 total_size += self.payload.as_slice().len();
2683 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
2684 for offset in offsets.into_iter() {
2685 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
2686 }
2687 writer.write_all(self.payload.as_slice())?;
2688 Ok(())
2689 }
2690 fn build(&self) -> Self::Entity {
2691 let mut inner = Vec::with_capacity(self.expected_length());
2692 self.write(&mut inner)
2693 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2694 DiscoveryMessage::new_unchecked(inner.into())
2695 }
2696}
2697#[derive(Clone)]
2698pub struct GetNodes(molecule::bytes::Bytes);
2699impl ::core::fmt::LowerHex for GetNodes {
2700 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2701 use molecule::hex_string;
2702 if f.alternate() {
2703 write!(f, "0x")?;
2704 }
2705 write!(f, "{}", hex_string(self.as_slice()))
2706 }
2707}
2708impl ::core::fmt::Debug for GetNodes {
2709 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2710 write!(f, "{}({:#x})", Self::NAME, self)
2711 }
2712}
2713impl ::core::fmt::Display for GetNodes {
2714 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2715 write!(f, "{} {{ ", Self::NAME)?;
2716 write!(f, "{}: {}", "version", self.version())?;
2717 write!(f, ", {}: {}", "count", self.count())?;
2718 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
2719 let extra_count = self.count_extra_fields();
2720 if extra_count != 0 {
2721 write!(f, ", .. ({} fields)", extra_count)?;
2722 }
2723 write!(f, " }}")
2724 }
2725}
2726impl ::core::default::Default for GetNodes {
2727 fn default() -> Self {
2728 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2729 GetNodes::new_unchecked(v)
2730 }
2731}
2732impl GetNodes {
2733 const DEFAULT_VALUE: [u8; 24] = [
2734 24, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2735 ];
2736 pub const FIELD_COUNT: usize = 3;
2737 pub fn total_size(&self) -> usize {
2738 molecule::unpack_number(self.as_slice()) as usize
2739 }
2740 pub fn field_count(&self) -> usize {
2741 if self.total_size() == molecule::NUMBER_SIZE {
2742 0
2743 } else {
2744 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2745 }
2746 }
2747 pub fn count_extra_fields(&self) -> usize {
2748 self.field_count() - Self::FIELD_COUNT
2749 }
2750 pub fn has_extra_fields(&self) -> bool {
2751 Self::FIELD_COUNT != self.field_count()
2752 }
2753 pub fn version(&self) -> Uint32 {
2754 let slice = self.as_slice();
2755 let start = molecule::unpack_number(&slice[4..]) as usize;
2756 let end = molecule::unpack_number(&slice[8..]) as usize;
2757 Uint32::new_unchecked(self.0.slice(start..end))
2758 }
2759 pub fn count(&self) -> Uint32 {
2760 let slice = self.as_slice();
2761 let start = molecule::unpack_number(&slice[8..]) as usize;
2762 let end = molecule::unpack_number(&slice[12..]) as usize;
2763 Uint32::new_unchecked(self.0.slice(start..end))
2764 }
2765 pub fn listen_port(&self) -> PortOpt {
2766 let slice = self.as_slice();
2767 let start = molecule::unpack_number(&slice[12..]) as usize;
2768 if self.has_extra_fields() {
2769 let end = molecule::unpack_number(&slice[16..]) as usize;
2770 PortOpt::new_unchecked(self.0.slice(start..end))
2771 } else {
2772 PortOpt::new_unchecked(self.0.slice(start..))
2773 }
2774 }
2775 pub fn as_reader<'r>(&'r self) -> GetNodesReader<'r> {
2776 GetNodesReader::new_unchecked(self.as_slice())
2777 }
2778}
2779impl molecule::prelude::Entity for GetNodes {
2780 type Builder = GetNodesBuilder;
2781 const NAME: &'static str = "GetNodes";
2782 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2783 GetNodes(data)
2784 }
2785 fn as_bytes(&self) -> molecule::bytes::Bytes {
2786 self.0.clone()
2787 }
2788 fn as_slice(&self) -> &[u8] {
2789 &self.0[..]
2790 }
2791 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2792 GetNodesReader::from_slice(slice).map(|reader| reader.to_entity())
2793 }
2794 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2795 GetNodesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2796 }
2797 fn new_builder() -> Self::Builder {
2798 ::core::default::Default::default()
2799 }
2800 fn as_builder(self) -> Self::Builder {
2801 Self::new_builder()
2802 .version(self.version())
2803 .count(self.count())
2804 .listen_port(self.listen_port())
2805 }
2806}
2807#[derive(Clone, Copy)]
2808pub struct GetNodesReader<'r>(&'r [u8]);
2809impl<'r> ::core::fmt::LowerHex for GetNodesReader<'r> {
2810 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2811 use molecule::hex_string;
2812 if f.alternate() {
2813 write!(f, "0x")?;
2814 }
2815 write!(f, "{}", hex_string(self.as_slice()))
2816 }
2817}
2818impl<'r> ::core::fmt::Debug for GetNodesReader<'r> {
2819 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2820 write!(f, "{}({:#x})", Self::NAME, self)
2821 }
2822}
2823impl<'r> ::core::fmt::Display for GetNodesReader<'r> {
2824 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2825 write!(f, "{} {{ ", Self::NAME)?;
2826 write!(f, "{}: {}", "version", self.version())?;
2827 write!(f, ", {}: {}", "count", self.count())?;
2828 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
2829 let extra_count = self.count_extra_fields();
2830 if extra_count != 0 {
2831 write!(f, ", .. ({} fields)", extra_count)?;
2832 }
2833 write!(f, " }}")
2834 }
2835}
2836impl<'r> GetNodesReader<'r> {
2837 pub const FIELD_COUNT: usize = 3;
2838 pub fn total_size(&self) -> usize {
2839 molecule::unpack_number(self.as_slice()) as usize
2840 }
2841 pub fn field_count(&self) -> usize {
2842 if self.total_size() == molecule::NUMBER_SIZE {
2843 0
2844 } else {
2845 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2846 }
2847 }
2848 pub fn count_extra_fields(&self) -> usize {
2849 self.field_count() - Self::FIELD_COUNT
2850 }
2851 pub fn has_extra_fields(&self) -> bool {
2852 Self::FIELD_COUNT != self.field_count()
2853 }
2854 pub fn version(&self) -> Uint32Reader<'r> {
2855 let slice = self.as_slice();
2856 let start = molecule::unpack_number(&slice[4..]) as usize;
2857 let end = molecule::unpack_number(&slice[8..]) as usize;
2858 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
2859 }
2860 pub fn count(&self) -> Uint32Reader<'r> {
2861 let slice = self.as_slice();
2862 let start = molecule::unpack_number(&slice[8..]) as usize;
2863 let end = molecule::unpack_number(&slice[12..]) as usize;
2864 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
2865 }
2866 pub fn listen_port(&self) -> PortOptReader<'r> {
2867 let slice = self.as_slice();
2868 let start = molecule::unpack_number(&slice[12..]) as usize;
2869 if self.has_extra_fields() {
2870 let end = molecule::unpack_number(&slice[16..]) as usize;
2871 PortOptReader::new_unchecked(&self.as_slice()[start..end])
2872 } else {
2873 PortOptReader::new_unchecked(&self.as_slice()[start..])
2874 }
2875 }
2876}
2877impl<'r> molecule::prelude::Reader<'r> for GetNodesReader<'r> {
2878 type Entity = GetNodes;
2879 const NAME: &'static str = "GetNodesReader";
2880 fn to_entity(&self) -> Self::Entity {
2881 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2882 }
2883 fn new_unchecked(slice: &'r [u8]) -> Self {
2884 GetNodesReader(slice)
2885 }
2886 fn as_slice(&self) -> &'r [u8] {
2887 self.0
2888 }
2889 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2890 use molecule::verification_error as ve;
2891 let slice_len = slice.len();
2892 if slice_len < molecule::NUMBER_SIZE {
2893 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2894 }
2895 let total_size = molecule::unpack_number(slice) as usize;
2896 if slice_len != total_size {
2897 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2898 }
2899 if slice_len < molecule::NUMBER_SIZE * 2 {
2900 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
2901 }
2902 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
2903 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
2904 return ve!(Self, OffsetsNotMatch);
2905 }
2906 if slice_len < offset_first {
2907 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
2908 }
2909 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
2910 if field_count < Self::FIELD_COUNT {
2911 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2912 } else if !compatible && field_count > Self::FIELD_COUNT {
2913 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2914 };
2915 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
2916 .chunks_exact(molecule::NUMBER_SIZE)
2917 .map(|x| molecule::unpack_number(x) as usize)
2918 .collect();
2919 offsets.push(total_size);
2920 if offsets.windows(2).any(|i| i[0] > i[1]) {
2921 return ve!(Self, OffsetsNotMatch);
2922 }
2923 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
2924 Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
2925 PortOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
2926 Ok(())
2927 }
2928}
2929#[derive(Clone, Debug, Default)]
2930pub struct GetNodesBuilder {
2931 pub(crate) version: Uint32,
2932 pub(crate) count: Uint32,
2933 pub(crate) listen_port: PortOpt,
2934}
2935impl GetNodesBuilder {
2936 pub const FIELD_COUNT: usize = 3;
2937 pub fn version<T>(mut self, v: T) -> Self
2938 where
2939 T: ::core::convert::Into<Uint32>,
2940 {
2941 self.version = v.into();
2942 self
2943 }
2944 pub fn count<T>(mut self, v: T) -> Self
2945 where
2946 T: ::core::convert::Into<Uint32>,
2947 {
2948 self.count = v.into();
2949 self
2950 }
2951 pub fn listen_port<T>(mut self, v: T) -> Self
2952 where
2953 T: ::core::convert::Into<PortOpt>,
2954 {
2955 self.listen_port = v.into();
2956 self
2957 }
2958}
2959impl molecule::prelude::Builder for GetNodesBuilder {
2960 type Entity = GetNodes;
2961 const NAME: &'static str = "GetNodesBuilder";
2962 fn expected_length(&self) -> usize {
2963 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
2964 + self.version.as_slice().len()
2965 + self.count.as_slice().len()
2966 + self.listen_port.as_slice().len()
2967 }
2968 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2969 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
2970 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
2971 offsets.push(total_size);
2972 total_size += self.version.as_slice().len();
2973 offsets.push(total_size);
2974 total_size += self.count.as_slice().len();
2975 offsets.push(total_size);
2976 total_size += self.listen_port.as_slice().len();
2977 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
2978 for offset in offsets.into_iter() {
2979 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
2980 }
2981 writer.write_all(self.version.as_slice())?;
2982 writer.write_all(self.count.as_slice())?;
2983 writer.write_all(self.listen_port.as_slice())?;
2984 Ok(())
2985 }
2986 fn build(&self) -> Self::Entity {
2987 let mut inner = Vec::with_capacity(self.expected_length());
2988 self.write(&mut inner)
2989 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2990 GetNodes::new_unchecked(inner.into())
2991 }
2992}
2993#[derive(Clone)]
2994pub struct GetNodes2(molecule::bytes::Bytes);
2995impl ::core::fmt::LowerHex for GetNodes2 {
2996 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2997 use molecule::hex_string;
2998 if f.alternate() {
2999 write!(f, "0x")?;
3000 }
3001 write!(f, "{}", hex_string(self.as_slice()))
3002 }
3003}
3004impl ::core::fmt::Debug for GetNodes2 {
3005 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3006 write!(f, "{}({:#x})", Self::NAME, self)
3007 }
3008}
3009impl ::core::fmt::Display for GetNodes2 {
3010 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3011 write!(f, "{} {{ ", Self::NAME)?;
3012 write!(f, "{}: {}", "version", self.version())?;
3013 write!(f, ", {}: {}", "count", self.count())?;
3014 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
3015 write!(f, ", {}: {}", "required_flags", self.required_flags())?;
3016 let extra_count = self.count_extra_fields();
3017 if extra_count != 0 {
3018 write!(f, ", .. ({} fields)", extra_count)?;
3019 }
3020 write!(f, " }}")
3021 }
3022}
3023impl ::core::default::Default for GetNodes2 {
3024 fn default() -> Self {
3025 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3026 GetNodes2::new_unchecked(v)
3027 }
3028}
3029impl GetNodes2 {
3030 const DEFAULT_VALUE: [u8; 36] = [
3031 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3032 0, 0, 0, 0, 0, 0, 0,
3033 ];
3034 pub const FIELD_COUNT: usize = 4;
3035 pub fn total_size(&self) -> usize {
3036 molecule::unpack_number(self.as_slice()) as usize
3037 }
3038 pub fn field_count(&self) -> usize {
3039 if self.total_size() == molecule::NUMBER_SIZE {
3040 0
3041 } else {
3042 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3043 }
3044 }
3045 pub fn count_extra_fields(&self) -> usize {
3046 self.field_count() - Self::FIELD_COUNT
3047 }
3048 pub fn has_extra_fields(&self) -> bool {
3049 Self::FIELD_COUNT != self.field_count()
3050 }
3051 pub fn version(&self) -> Uint32 {
3052 let slice = self.as_slice();
3053 let start = molecule::unpack_number(&slice[4..]) as usize;
3054 let end = molecule::unpack_number(&slice[8..]) as usize;
3055 Uint32::new_unchecked(self.0.slice(start..end))
3056 }
3057 pub fn count(&self) -> Uint32 {
3058 let slice = self.as_slice();
3059 let start = molecule::unpack_number(&slice[8..]) as usize;
3060 let end = molecule::unpack_number(&slice[12..]) as usize;
3061 Uint32::new_unchecked(self.0.slice(start..end))
3062 }
3063 pub fn listen_port(&self) -> PortOpt {
3064 let slice = self.as_slice();
3065 let start = molecule::unpack_number(&slice[12..]) as usize;
3066 let end = molecule::unpack_number(&slice[16..]) as usize;
3067 PortOpt::new_unchecked(self.0.slice(start..end))
3068 }
3069 pub fn required_flags(&self) -> Uint64 {
3070 let slice = self.as_slice();
3071 let start = molecule::unpack_number(&slice[16..]) as usize;
3072 if self.has_extra_fields() {
3073 let end = molecule::unpack_number(&slice[20..]) as usize;
3074 Uint64::new_unchecked(self.0.slice(start..end))
3075 } else {
3076 Uint64::new_unchecked(self.0.slice(start..))
3077 }
3078 }
3079 pub fn as_reader<'r>(&'r self) -> GetNodes2Reader<'r> {
3080 GetNodes2Reader::new_unchecked(self.as_slice())
3081 }
3082}
3083impl molecule::prelude::Entity for GetNodes2 {
3084 type Builder = GetNodes2Builder;
3085 const NAME: &'static str = "GetNodes2";
3086 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3087 GetNodes2(data)
3088 }
3089 fn as_bytes(&self) -> molecule::bytes::Bytes {
3090 self.0.clone()
3091 }
3092 fn as_slice(&self) -> &[u8] {
3093 &self.0[..]
3094 }
3095 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3096 GetNodes2Reader::from_slice(slice).map(|reader| reader.to_entity())
3097 }
3098 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3099 GetNodes2Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3100 }
3101 fn new_builder() -> Self::Builder {
3102 ::core::default::Default::default()
3103 }
3104 fn as_builder(self) -> Self::Builder {
3105 Self::new_builder()
3106 .version(self.version())
3107 .count(self.count())
3108 .listen_port(self.listen_port())
3109 .required_flags(self.required_flags())
3110 }
3111}
3112#[derive(Clone, Copy)]
3113pub struct GetNodes2Reader<'r>(&'r [u8]);
3114impl<'r> ::core::fmt::LowerHex for GetNodes2Reader<'r> {
3115 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3116 use molecule::hex_string;
3117 if f.alternate() {
3118 write!(f, "0x")?;
3119 }
3120 write!(f, "{}", hex_string(self.as_slice()))
3121 }
3122}
3123impl<'r> ::core::fmt::Debug for GetNodes2Reader<'r> {
3124 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3125 write!(f, "{}({:#x})", Self::NAME, self)
3126 }
3127}
3128impl<'r> ::core::fmt::Display for GetNodes2Reader<'r> {
3129 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3130 write!(f, "{} {{ ", Self::NAME)?;
3131 write!(f, "{}: {}", "version", self.version())?;
3132 write!(f, ", {}: {}", "count", self.count())?;
3133 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
3134 write!(f, ", {}: {}", "required_flags", self.required_flags())?;
3135 let extra_count = self.count_extra_fields();
3136 if extra_count != 0 {
3137 write!(f, ", .. ({} fields)", extra_count)?;
3138 }
3139 write!(f, " }}")
3140 }
3141}
3142impl<'r> GetNodes2Reader<'r> {
3143 pub const FIELD_COUNT: usize = 4;
3144 pub fn total_size(&self) -> usize {
3145 molecule::unpack_number(self.as_slice()) as usize
3146 }
3147 pub fn field_count(&self) -> usize {
3148 if self.total_size() == molecule::NUMBER_SIZE {
3149 0
3150 } else {
3151 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3152 }
3153 }
3154 pub fn count_extra_fields(&self) -> usize {
3155 self.field_count() - Self::FIELD_COUNT
3156 }
3157 pub fn has_extra_fields(&self) -> bool {
3158 Self::FIELD_COUNT != self.field_count()
3159 }
3160 pub fn version(&self) -> Uint32Reader<'r> {
3161 let slice = self.as_slice();
3162 let start = molecule::unpack_number(&slice[4..]) as usize;
3163 let end = molecule::unpack_number(&slice[8..]) as usize;
3164 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
3165 }
3166 pub fn count(&self) -> Uint32Reader<'r> {
3167 let slice = self.as_slice();
3168 let start = molecule::unpack_number(&slice[8..]) as usize;
3169 let end = molecule::unpack_number(&slice[12..]) as usize;
3170 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
3171 }
3172 pub fn listen_port(&self) -> PortOptReader<'r> {
3173 let slice = self.as_slice();
3174 let start = molecule::unpack_number(&slice[12..]) as usize;
3175 let end = molecule::unpack_number(&slice[16..]) as usize;
3176 PortOptReader::new_unchecked(&self.as_slice()[start..end])
3177 }
3178 pub fn required_flags(&self) -> Uint64Reader<'r> {
3179 let slice = self.as_slice();
3180 let start = molecule::unpack_number(&slice[16..]) as usize;
3181 if self.has_extra_fields() {
3182 let end = molecule::unpack_number(&slice[20..]) as usize;
3183 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
3184 } else {
3185 Uint64Reader::new_unchecked(&self.as_slice()[start..])
3186 }
3187 }
3188}
3189impl<'r> molecule::prelude::Reader<'r> for GetNodes2Reader<'r> {
3190 type Entity = GetNodes2;
3191 const NAME: &'static str = "GetNodes2Reader";
3192 fn to_entity(&self) -> Self::Entity {
3193 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3194 }
3195 fn new_unchecked(slice: &'r [u8]) -> Self {
3196 GetNodes2Reader(slice)
3197 }
3198 fn as_slice(&self) -> &'r [u8] {
3199 self.0
3200 }
3201 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3202 use molecule::verification_error as ve;
3203 let slice_len = slice.len();
3204 if slice_len < molecule::NUMBER_SIZE {
3205 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3206 }
3207 let total_size = molecule::unpack_number(slice) as usize;
3208 if slice_len != total_size {
3209 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3210 }
3211 if slice_len < molecule::NUMBER_SIZE * 2 {
3212 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3213 }
3214 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3215 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3216 return ve!(Self, OffsetsNotMatch);
3217 }
3218 if slice_len < offset_first {
3219 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3220 }
3221 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3222 if field_count < Self::FIELD_COUNT {
3223 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3224 } else if !compatible && field_count > Self::FIELD_COUNT {
3225 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3226 };
3227 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3228 .chunks_exact(molecule::NUMBER_SIZE)
3229 .map(|x| molecule::unpack_number(x) as usize)
3230 .collect();
3231 offsets.push(total_size);
3232 if offsets.windows(2).any(|i| i[0] > i[1]) {
3233 return ve!(Self, OffsetsNotMatch);
3234 }
3235 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3236 Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3237 PortOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
3238 Uint64Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
3239 Ok(())
3240 }
3241}
3242#[derive(Clone, Debug, Default)]
3243pub struct GetNodes2Builder {
3244 pub(crate) version: Uint32,
3245 pub(crate) count: Uint32,
3246 pub(crate) listen_port: PortOpt,
3247 pub(crate) required_flags: Uint64,
3248}
3249impl GetNodes2Builder {
3250 pub const FIELD_COUNT: usize = 4;
3251 pub fn version<T>(mut self, v: T) -> Self
3252 where
3253 T: ::core::convert::Into<Uint32>,
3254 {
3255 self.version = v.into();
3256 self
3257 }
3258 pub fn count<T>(mut self, v: T) -> Self
3259 where
3260 T: ::core::convert::Into<Uint32>,
3261 {
3262 self.count = v.into();
3263 self
3264 }
3265 pub fn listen_port<T>(mut self, v: T) -> Self
3266 where
3267 T: ::core::convert::Into<PortOpt>,
3268 {
3269 self.listen_port = v.into();
3270 self
3271 }
3272 pub fn required_flags<T>(mut self, v: T) -> Self
3273 where
3274 T: ::core::convert::Into<Uint64>,
3275 {
3276 self.required_flags = v.into();
3277 self
3278 }
3279}
3280impl molecule::prelude::Builder for GetNodes2Builder {
3281 type Entity = GetNodes2;
3282 const NAME: &'static str = "GetNodes2Builder";
3283 fn expected_length(&self) -> usize {
3284 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3285 + self.version.as_slice().len()
3286 + self.count.as_slice().len()
3287 + self.listen_port.as_slice().len()
3288 + self.required_flags.as_slice().len()
3289 }
3290 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3291 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3292 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3293 offsets.push(total_size);
3294 total_size += self.version.as_slice().len();
3295 offsets.push(total_size);
3296 total_size += self.count.as_slice().len();
3297 offsets.push(total_size);
3298 total_size += self.listen_port.as_slice().len();
3299 offsets.push(total_size);
3300 total_size += self.required_flags.as_slice().len();
3301 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3302 for offset in offsets.into_iter() {
3303 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3304 }
3305 writer.write_all(self.version.as_slice())?;
3306 writer.write_all(self.count.as_slice())?;
3307 writer.write_all(self.listen_port.as_slice())?;
3308 writer.write_all(self.required_flags.as_slice())?;
3309 Ok(())
3310 }
3311 fn build(&self) -> Self::Entity {
3312 let mut inner = Vec::with_capacity(self.expected_length());
3313 self.write(&mut inner)
3314 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3315 GetNodes2::new_unchecked(inner.into())
3316 }
3317}
3318#[derive(Clone)]
3319pub struct Nodes(molecule::bytes::Bytes);
3320impl ::core::fmt::LowerHex for Nodes {
3321 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3322 use molecule::hex_string;
3323 if f.alternate() {
3324 write!(f, "0x")?;
3325 }
3326 write!(f, "{}", hex_string(self.as_slice()))
3327 }
3328}
3329impl ::core::fmt::Debug for Nodes {
3330 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3331 write!(f, "{}({:#x})", Self::NAME, self)
3332 }
3333}
3334impl ::core::fmt::Display for Nodes {
3335 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3336 write!(f, "{} {{ ", Self::NAME)?;
3337 write!(f, "{}: {}", "announce", self.announce())?;
3338 write!(f, ", {}: {}", "items", self.items())?;
3339 let extra_count = self.count_extra_fields();
3340 if extra_count != 0 {
3341 write!(f, ", .. ({} fields)", extra_count)?;
3342 }
3343 write!(f, " }}")
3344 }
3345}
3346impl ::core::default::Default for Nodes {
3347 fn default() -> Self {
3348 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3349 Nodes::new_unchecked(v)
3350 }
3351}
3352impl Nodes {
3353 const DEFAULT_VALUE: [u8; 17] = [17, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 0, 4, 0, 0, 0];
3354 pub const FIELD_COUNT: usize = 2;
3355 pub fn total_size(&self) -> usize {
3356 molecule::unpack_number(self.as_slice()) as usize
3357 }
3358 pub fn field_count(&self) -> usize {
3359 if self.total_size() == molecule::NUMBER_SIZE {
3360 0
3361 } else {
3362 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3363 }
3364 }
3365 pub fn count_extra_fields(&self) -> usize {
3366 self.field_count() - Self::FIELD_COUNT
3367 }
3368 pub fn has_extra_fields(&self) -> bool {
3369 Self::FIELD_COUNT != self.field_count()
3370 }
3371 pub fn announce(&self) -> Bool {
3372 let slice = self.as_slice();
3373 let start = molecule::unpack_number(&slice[4..]) as usize;
3374 let end = molecule::unpack_number(&slice[8..]) as usize;
3375 Bool::new_unchecked(self.0.slice(start..end))
3376 }
3377 pub fn items(&self) -> NodeVec {
3378 let slice = self.as_slice();
3379 let start = molecule::unpack_number(&slice[8..]) as usize;
3380 if self.has_extra_fields() {
3381 let end = molecule::unpack_number(&slice[12..]) as usize;
3382 NodeVec::new_unchecked(self.0.slice(start..end))
3383 } else {
3384 NodeVec::new_unchecked(self.0.slice(start..))
3385 }
3386 }
3387 pub fn as_reader<'r>(&'r self) -> NodesReader<'r> {
3388 NodesReader::new_unchecked(self.as_slice())
3389 }
3390}
3391impl molecule::prelude::Entity for Nodes {
3392 type Builder = NodesBuilder;
3393 const NAME: &'static str = "Nodes";
3394 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3395 Nodes(data)
3396 }
3397 fn as_bytes(&self) -> molecule::bytes::Bytes {
3398 self.0.clone()
3399 }
3400 fn as_slice(&self) -> &[u8] {
3401 &self.0[..]
3402 }
3403 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3404 NodesReader::from_slice(slice).map(|reader| reader.to_entity())
3405 }
3406 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3407 NodesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3408 }
3409 fn new_builder() -> Self::Builder {
3410 ::core::default::Default::default()
3411 }
3412 fn as_builder(self) -> Self::Builder {
3413 Self::new_builder()
3414 .announce(self.announce())
3415 .items(self.items())
3416 }
3417}
3418#[derive(Clone, Copy)]
3419pub struct NodesReader<'r>(&'r [u8]);
3420impl<'r> ::core::fmt::LowerHex for NodesReader<'r> {
3421 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3422 use molecule::hex_string;
3423 if f.alternate() {
3424 write!(f, "0x")?;
3425 }
3426 write!(f, "{}", hex_string(self.as_slice()))
3427 }
3428}
3429impl<'r> ::core::fmt::Debug for NodesReader<'r> {
3430 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3431 write!(f, "{}({:#x})", Self::NAME, self)
3432 }
3433}
3434impl<'r> ::core::fmt::Display for NodesReader<'r> {
3435 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3436 write!(f, "{} {{ ", Self::NAME)?;
3437 write!(f, "{}: {}", "announce", self.announce())?;
3438 write!(f, ", {}: {}", "items", self.items())?;
3439 let extra_count = self.count_extra_fields();
3440 if extra_count != 0 {
3441 write!(f, ", .. ({} fields)", extra_count)?;
3442 }
3443 write!(f, " }}")
3444 }
3445}
3446impl<'r> NodesReader<'r> {
3447 pub const FIELD_COUNT: usize = 2;
3448 pub fn total_size(&self) -> usize {
3449 molecule::unpack_number(self.as_slice()) as usize
3450 }
3451 pub fn field_count(&self) -> usize {
3452 if self.total_size() == molecule::NUMBER_SIZE {
3453 0
3454 } else {
3455 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3456 }
3457 }
3458 pub fn count_extra_fields(&self) -> usize {
3459 self.field_count() - Self::FIELD_COUNT
3460 }
3461 pub fn has_extra_fields(&self) -> bool {
3462 Self::FIELD_COUNT != self.field_count()
3463 }
3464 pub fn announce(&self) -> BoolReader<'r> {
3465 let slice = self.as_slice();
3466 let start = molecule::unpack_number(&slice[4..]) as usize;
3467 let end = molecule::unpack_number(&slice[8..]) as usize;
3468 BoolReader::new_unchecked(&self.as_slice()[start..end])
3469 }
3470 pub fn items(&self) -> NodeVecReader<'r> {
3471 let slice = self.as_slice();
3472 let start = molecule::unpack_number(&slice[8..]) as usize;
3473 if self.has_extra_fields() {
3474 let end = molecule::unpack_number(&slice[12..]) as usize;
3475 NodeVecReader::new_unchecked(&self.as_slice()[start..end])
3476 } else {
3477 NodeVecReader::new_unchecked(&self.as_slice()[start..])
3478 }
3479 }
3480}
3481impl<'r> molecule::prelude::Reader<'r> for NodesReader<'r> {
3482 type Entity = Nodes;
3483 const NAME: &'static str = "NodesReader";
3484 fn to_entity(&self) -> Self::Entity {
3485 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3486 }
3487 fn new_unchecked(slice: &'r [u8]) -> Self {
3488 NodesReader(slice)
3489 }
3490 fn as_slice(&self) -> &'r [u8] {
3491 self.0
3492 }
3493 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3494 use molecule::verification_error as ve;
3495 let slice_len = slice.len();
3496 if slice_len < molecule::NUMBER_SIZE {
3497 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3498 }
3499 let total_size = molecule::unpack_number(slice) as usize;
3500 if slice_len != total_size {
3501 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3502 }
3503 if slice_len < molecule::NUMBER_SIZE * 2 {
3504 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3505 }
3506 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3507 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3508 return ve!(Self, OffsetsNotMatch);
3509 }
3510 if slice_len < offset_first {
3511 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3512 }
3513 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3514 if field_count < Self::FIELD_COUNT {
3515 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3516 } else if !compatible && field_count > Self::FIELD_COUNT {
3517 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3518 };
3519 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3520 .chunks_exact(molecule::NUMBER_SIZE)
3521 .map(|x| molecule::unpack_number(x) as usize)
3522 .collect();
3523 offsets.push(total_size);
3524 if offsets.windows(2).any(|i| i[0] > i[1]) {
3525 return ve!(Self, OffsetsNotMatch);
3526 }
3527 BoolReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3528 NodeVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3529 Ok(())
3530 }
3531}
3532#[derive(Clone, Debug, Default)]
3533pub struct NodesBuilder {
3534 pub(crate) announce: Bool,
3535 pub(crate) items: NodeVec,
3536}
3537impl NodesBuilder {
3538 pub const FIELD_COUNT: usize = 2;
3539 pub fn announce<T>(mut self, v: T) -> Self
3540 where
3541 T: ::core::convert::Into<Bool>,
3542 {
3543 self.announce = v.into();
3544 self
3545 }
3546 pub fn items<T>(mut self, v: T) -> Self
3547 where
3548 T: ::core::convert::Into<NodeVec>,
3549 {
3550 self.items = v.into();
3551 self
3552 }
3553}
3554impl molecule::prelude::Builder for NodesBuilder {
3555 type Entity = Nodes;
3556 const NAME: &'static str = "NodesBuilder";
3557 fn expected_length(&self) -> usize {
3558 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3559 + self.announce.as_slice().len()
3560 + self.items.as_slice().len()
3561 }
3562 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3563 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3564 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3565 offsets.push(total_size);
3566 total_size += self.announce.as_slice().len();
3567 offsets.push(total_size);
3568 total_size += self.items.as_slice().len();
3569 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3570 for offset in offsets.into_iter() {
3571 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3572 }
3573 writer.write_all(self.announce.as_slice())?;
3574 writer.write_all(self.items.as_slice())?;
3575 Ok(())
3576 }
3577 fn build(&self) -> Self::Entity {
3578 let mut inner = Vec::with_capacity(self.expected_length());
3579 self.write(&mut inner)
3580 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3581 Nodes::new_unchecked(inner.into())
3582 }
3583}
3584#[derive(Clone)]
3585pub struct Nodes2(molecule::bytes::Bytes);
3586impl ::core::fmt::LowerHex for Nodes2 {
3587 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3588 use molecule::hex_string;
3589 if f.alternate() {
3590 write!(f, "0x")?;
3591 }
3592 write!(f, "{}", hex_string(self.as_slice()))
3593 }
3594}
3595impl ::core::fmt::Debug for Nodes2 {
3596 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3597 write!(f, "{}({:#x})", Self::NAME, self)
3598 }
3599}
3600impl ::core::fmt::Display for Nodes2 {
3601 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3602 write!(f, "{} {{ ", Self::NAME)?;
3603 write!(f, "{}: {}", "announce", self.announce())?;
3604 write!(f, ", {}: {}", "items", self.items())?;
3605 let extra_count = self.count_extra_fields();
3606 if extra_count != 0 {
3607 write!(f, ", .. ({} fields)", extra_count)?;
3608 }
3609 write!(f, " }}")
3610 }
3611}
3612impl ::core::default::Default for Nodes2 {
3613 fn default() -> Self {
3614 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3615 Nodes2::new_unchecked(v)
3616 }
3617}
3618impl Nodes2 {
3619 const DEFAULT_VALUE: [u8; 17] = [17, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 0, 4, 0, 0, 0];
3620 pub const FIELD_COUNT: usize = 2;
3621 pub fn total_size(&self) -> usize {
3622 molecule::unpack_number(self.as_slice()) as usize
3623 }
3624 pub fn field_count(&self) -> usize {
3625 if self.total_size() == molecule::NUMBER_SIZE {
3626 0
3627 } else {
3628 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3629 }
3630 }
3631 pub fn count_extra_fields(&self) -> usize {
3632 self.field_count() - Self::FIELD_COUNT
3633 }
3634 pub fn has_extra_fields(&self) -> bool {
3635 Self::FIELD_COUNT != self.field_count()
3636 }
3637 pub fn announce(&self) -> Bool {
3638 let slice = self.as_slice();
3639 let start = molecule::unpack_number(&slice[4..]) as usize;
3640 let end = molecule::unpack_number(&slice[8..]) as usize;
3641 Bool::new_unchecked(self.0.slice(start..end))
3642 }
3643 pub fn items(&self) -> Node2Vec {
3644 let slice = self.as_slice();
3645 let start = molecule::unpack_number(&slice[8..]) as usize;
3646 if self.has_extra_fields() {
3647 let end = molecule::unpack_number(&slice[12..]) as usize;
3648 Node2Vec::new_unchecked(self.0.slice(start..end))
3649 } else {
3650 Node2Vec::new_unchecked(self.0.slice(start..))
3651 }
3652 }
3653 pub fn as_reader<'r>(&'r self) -> Nodes2Reader<'r> {
3654 Nodes2Reader::new_unchecked(self.as_slice())
3655 }
3656}
3657impl molecule::prelude::Entity for Nodes2 {
3658 type Builder = Nodes2Builder;
3659 const NAME: &'static str = "Nodes2";
3660 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3661 Nodes2(data)
3662 }
3663 fn as_bytes(&self) -> molecule::bytes::Bytes {
3664 self.0.clone()
3665 }
3666 fn as_slice(&self) -> &[u8] {
3667 &self.0[..]
3668 }
3669 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3670 Nodes2Reader::from_slice(slice).map(|reader| reader.to_entity())
3671 }
3672 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3673 Nodes2Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3674 }
3675 fn new_builder() -> Self::Builder {
3676 ::core::default::Default::default()
3677 }
3678 fn as_builder(self) -> Self::Builder {
3679 Self::new_builder()
3680 .announce(self.announce())
3681 .items(self.items())
3682 }
3683}
3684#[derive(Clone, Copy)]
3685pub struct Nodes2Reader<'r>(&'r [u8]);
3686impl<'r> ::core::fmt::LowerHex for Nodes2Reader<'r> {
3687 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3688 use molecule::hex_string;
3689 if f.alternate() {
3690 write!(f, "0x")?;
3691 }
3692 write!(f, "{}", hex_string(self.as_slice()))
3693 }
3694}
3695impl<'r> ::core::fmt::Debug for Nodes2Reader<'r> {
3696 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3697 write!(f, "{}({:#x})", Self::NAME, self)
3698 }
3699}
3700impl<'r> ::core::fmt::Display for Nodes2Reader<'r> {
3701 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3702 write!(f, "{} {{ ", Self::NAME)?;
3703 write!(f, "{}: {}", "announce", self.announce())?;
3704 write!(f, ", {}: {}", "items", self.items())?;
3705 let extra_count = self.count_extra_fields();
3706 if extra_count != 0 {
3707 write!(f, ", .. ({} fields)", extra_count)?;
3708 }
3709 write!(f, " }}")
3710 }
3711}
3712impl<'r> Nodes2Reader<'r> {
3713 pub const FIELD_COUNT: usize = 2;
3714 pub fn total_size(&self) -> usize {
3715 molecule::unpack_number(self.as_slice()) as usize
3716 }
3717 pub fn field_count(&self) -> usize {
3718 if self.total_size() == molecule::NUMBER_SIZE {
3719 0
3720 } else {
3721 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3722 }
3723 }
3724 pub fn count_extra_fields(&self) -> usize {
3725 self.field_count() - Self::FIELD_COUNT
3726 }
3727 pub fn has_extra_fields(&self) -> bool {
3728 Self::FIELD_COUNT != self.field_count()
3729 }
3730 pub fn announce(&self) -> BoolReader<'r> {
3731 let slice = self.as_slice();
3732 let start = molecule::unpack_number(&slice[4..]) as usize;
3733 let end = molecule::unpack_number(&slice[8..]) as usize;
3734 BoolReader::new_unchecked(&self.as_slice()[start..end])
3735 }
3736 pub fn items(&self) -> Node2VecReader<'r> {
3737 let slice = self.as_slice();
3738 let start = molecule::unpack_number(&slice[8..]) as usize;
3739 if self.has_extra_fields() {
3740 let end = molecule::unpack_number(&slice[12..]) as usize;
3741 Node2VecReader::new_unchecked(&self.as_slice()[start..end])
3742 } else {
3743 Node2VecReader::new_unchecked(&self.as_slice()[start..])
3744 }
3745 }
3746}
3747impl<'r> molecule::prelude::Reader<'r> for Nodes2Reader<'r> {
3748 type Entity = Nodes2;
3749 const NAME: &'static str = "Nodes2Reader";
3750 fn to_entity(&self) -> Self::Entity {
3751 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3752 }
3753 fn new_unchecked(slice: &'r [u8]) -> Self {
3754 Nodes2Reader(slice)
3755 }
3756 fn as_slice(&self) -> &'r [u8] {
3757 self.0
3758 }
3759 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3760 use molecule::verification_error as ve;
3761 let slice_len = slice.len();
3762 if slice_len < molecule::NUMBER_SIZE {
3763 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3764 }
3765 let total_size = molecule::unpack_number(slice) as usize;
3766 if slice_len != total_size {
3767 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3768 }
3769 if slice_len < molecule::NUMBER_SIZE * 2 {
3770 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3771 }
3772 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3773 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3774 return ve!(Self, OffsetsNotMatch);
3775 }
3776 if slice_len < offset_first {
3777 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3778 }
3779 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3780 if field_count < Self::FIELD_COUNT {
3781 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3782 } else if !compatible && field_count > Self::FIELD_COUNT {
3783 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3784 };
3785 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3786 .chunks_exact(molecule::NUMBER_SIZE)
3787 .map(|x| molecule::unpack_number(x) as usize)
3788 .collect();
3789 offsets.push(total_size);
3790 if offsets.windows(2).any(|i| i[0] > i[1]) {
3791 return ve!(Self, OffsetsNotMatch);
3792 }
3793 BoolReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3794 Node2VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3795 Ok(())
3796 }
3797}
3798#[derive(Clone, Debug, Default)]
3799pub struct Nodes2Builder {
3800 pub(crate) announce: Bool,
3801 pub(crate) items: Node2Vec,
3802}
3803impl Nodes2Builder {
3804 pub const FIELD_COUNT: usize = 2;
3805 pub fn announce<T>(mut self, v: T) -> Self
3806 where
3807 T: ::core::convert::Into<Bool>,
3808 {
3809 self.announce = v.into();
3810 self
3811 }
3812 pub fn items<T>(mut self, v: T) -> Self
3813 where
3814 T: ::core::convert::Into<Node2Vec>,
3815 {
3816 self.items = v.into();
3817 self
3818 }
3819}
3820impl molecule::prelude::Builder for Nodes2Builder {
3821 type Entity = Nodes2;
3822 const NAME: &'static str = "Nodes2Builder";
3823 fn expected_length(&self) -> usize {
3824 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3825 + self.announce.as_slice().len()
3826 + self.items.as_slice().len()
3827 }
3828 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3829 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3830 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3831 offsets.push(total_size);
3832 total_size += self.announce.as_slice().len();
3833 offsets.push(total_size);
3834 total_size += self.items.as_slice().len();
3835 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3836 for offset in offsets.into_iter() {
3837 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3838 }
3839 writer.write_all(self.announce.as_slice())?;
3840 writer.write_all(self.items.as_slice())?;
3841 Ok(())
3842 }
3843 fn build(&self) -> Self::Entity {
3844 let mut inner = Vec::with_capacity(self.expected_length());
3845 self.write(&mut inner)
3846 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3847 Nodes2::new_unchecked(inner.into())
3848 }
3849}
3850#[derive(Clone)]
3851pub struct Node(molecule::bytes::Bytes);
3852impl ::core::fmt::LowerHex for Node {
3853 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3854 use molecule::hex_string;
3855 if f.alternate() {
3856 write!(f, "0x")?;
3857 }
3858 write!(f, "{}", hex_string(self.as_slice()))
3859 }
3860}
3861impl ::core::fmt::Debug for Node {
3862 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3863 write!(f, "{}({:#x})", Self::NAME, self)
3864 }
3865}
3866impl ::core::fmt::Display for Node {
3867 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3868 write!(f, "{} {{ ", Self::NAME)?;
3869 write!(f, "{}: {}", "addresses", self.addresses())?;
3870 let extra_count = self.count_extra_fields();
3871 if extra_count != 0 {
3872 write!(f, ", .. ({} fields)", extra_count)?;
3873 }
3874 write!(f, " }}")
3875 }
3876}
3877impl ::core::default::Default for Node {
3878 fn default() -> Self {
3879 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3880 Node::new_unchecked(v)
3881 }
3882}
3883impl Node {
3884 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0];
3885 pub const FIELD_COUNT: usize = 1;
3886 pub fn total_size(&self) -> usize {
3887 molecule::unpack_number(self.as_slice()) as usize
3888 }
3889 pub fn field_count(&self) -> usize {
3890 if self.total_size() == molecule::NUMBER_SIZE {
3891 0
3892 } else {
3893 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3894 }
3895 }
3896 pub fn count_extra_fields(&self) -> usize {
3897 self.field_count() - Self::FIELD_COUNT
3898 }
3899 pub fn has_extra_fields(&self) -> bool {
3900 Self::FIELD_COUNT != self.field_count()
3901 }
3902 pub fn addresses(&self) -> BytesVec {
3903 let slice = self.as_slice();
3904 let start = molecule::unpack_number(&slice[4..]) as usize;
3905 if self.has_extra_fields() {
3906 let end = molecule::unpack_number(&slice[8..]) as usize;
3907 BytesVec::new_unchecked(self.0.slice(start..end))
3908 } else {
3909 BytesVec::new_unchecked(self.0.slice(start..))
3910 }
3911 }
3912 pub fn as_reader<'r>(&'r self) -> NodeReader<'r> {
3913 NodeReader::new_unchecked(self.as_slice())
3914 }
3915}
3916impl molecule::prelude::Entity for Node {
3917 type Builder = NodeBuilder;
3918 const NAME: &'static str = "Node";
3919 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3920 Node(data)
3921 }
3922 fn as_bytes(&self) -> molecule::bytes::Bytes {
3923 self.0.clone()
3924 }
3925 fn as_slice(&self) -> &[u8] {
3926 &self.0[..]
3927 }
3928 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3929 NodeReader::from_slice(slice).map(|reader| reader.to_entity())
3930 }
3931 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3932 NodeReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3933 }
3934 fn new_builder() -> Self::Builder {
3935 ::core::default::Default::default()
3936 }
3937 fn as_builder(self) -> Self::Builder {
3938 Self::new_builder().addresses(self.addresses())
3939 }
3940}
3941#[derive(Clone, Copy)]
3942pub struct NodeReader<'r>(&'r [u8]);
3943impl<'r> ::core::fmt::LowerHex for NodeReader<'r> {
3944 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3945 use molecule::hex_string;
3946 if f.alternate() {
3947 write!(f, "0x")?;
3948 }
3949 write!(f, "{}", hex_string(self.as_slice()))
3950 }
3951}
3952impl<'r> ::core::fmt::Debug for NodeReader<'r> {
3953 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3954 write!(f, "{}({:#x})", Self::NAME, self)
3955 }
3956}
3957impl<'r> ::core::fmt::Display for NodeReader<'r> {
3958 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3959 write!(f, "{} {{ ", Self::NAME)?;
3960 write!(f, "{}: {}", "addresses", self.addresses())?;
3961 let extra_count = self.count_extra_fields();
3962 if extra_count != 0 {
3963 write!(f, ", .. ({} fields)", extra_count)?;
3964 }
3965 write!(f, " }}")
3966 }
3967}
3968impl<'r> NodeReader<'r> {
3969 pub const FIELD_COUNT: usize = 1;
3970 pub fn total_size(&self) -> usize {
3971 molecule::unpack_number(self.as_slice()) as usize
3972 }
3973 pub fn field_count(&self) -> usize {
3974 if self.total_size() == molecule::NUMBER_SIZE {
3975 0
3976 } else {
3977 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3978 }
3979 }
3980 pub fn count_extra_fields(&self) -> usize {
3981 self.field_count() - Self::FIELD_COUNT
3982 }
3983 pub fn has_extra_fields(&self) -> bool {
3984 Self::FIELD_COUNT != self.field_count()
3985 }
3986 pub fn addresses(&self) -> BytesVecReader<'r> {
3987 let slice = self.as_slice();
3988 let start = molecule::unpack_number(&slice[4..]) as usize;
3989 if self.has_extra_fields() {
3990 let end = molecule::unpack_number(&slice[8..]) as usize;
3991 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
3992 } else {
3993 BytesVecReader::new_unchecked(&self.as_slice()[start..])
3994 }
3995 }
3996}
3997impl<'r> molecule::prelude::Reader<'r> for NodeReader<'r> {
3998 type Entity = Node;
3999 const NAME: &'static str = "NodeReader";
4000 fn to_entity(&self) -> Self::Entity {
4001 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4002 }
4003 fn new_unchecked(slice: &'r [u8]) -> Self {
4004 NodeReader(slice)
4005 }
4006 fn as_slice(&self) -> &'r [u8] {
4007 self.0
4008 }
4009 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4010 use molecule::verification_error as ve;
4011 let slice_len = slice.len();
4012 if slice_len < molecule::NUMBER_SIZE {
4013 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4014 }
4015 let total_size = molecule::unpack_number(slice) as usize;
4016 if slice_len != total_size {
4017 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4018 }
4019 if slice_len < molecule::NUMBER_SIZE * 2 {
4020 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4021 }
4022 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4023 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4024 return ve!(Self, OffsetsNotMatch);
4025 }
4026 if slice_len < offset_first {
4027 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4028 }
4029 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4030 if field_count < Self::FIELD_COUNT {
4031 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4032 } else if !compatible && field_count > Self::FIELD_COUNT {
4033 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4034 };
4035 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4036 .chunks_exact(molecule::NUMBER_SIZE)
4037 .map(|x| molecule::unpack_number(x) as usize)
4038 .collect();
4039 offsets.push(total_size);
4040 if offsets.windows(2).any(|i| i[0] > i[1]) {
4041 return ve!(Self, OffsetsNotMatch);
4042 }
4043 BytesVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4044 Ok(())
4045 }
4046}
4047#[derive(Clone, Debug, Default)]
4048pub struct NodeBuilder {
4049 pub(crate) addresses: BytesVec,
4050}
4051impl NodeBuilder {
4052 pub const FIELD_COUNT: usize = 1;
4053 pub fn addresses<T>(mut self, v: T) -> Self
4054 where
4055 T: ::core::convert::Into<BytesVec>,
4056 {
4057 self.addresses = v.into();
4058 self
4059 }
4060}
4061impl molecule::prelude::Builder for NodeBuilder {
4062 type Entity = Node;
4063 const NAME: &'static str = "NodeBuilder";
4064 fn expected_length(&self) -> usize {
4065 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.addresses.as_slice().len()
4066 }
4067 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4068 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4069 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4070 offsets.push(total_size);
4071 total_size += self.addresses.as_slice().len();
4072 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4073 for offset in offsets.into_iter() {
4074 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4075 }
4076 writer.write_all(self.addresses.as_slice())?;
4077 Ok(())
4078 }
4079 fn build(&self) -> Self::Entity {
4080 let mut inner = Vec::with_capacity(self.expected_length());
4081 self.write(&mut inner)
4082 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4083 Node::new_unchecked(inner.into())
4084 }
4085}
4086#[derive(Clone)]
4087pub struct Node2(molecule::bytes::Bytes);
4088impl ::core::fmt::LowerHex for Node2 {
4089 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4090 use molecule::hex_string;
4091 if f.alternate() {
4092 write!(f, "0x")?;
4093 }
4094 write!(f, "{}", hex_string(self.as_slice()))
4095 }
4096}
4097impl ::core::fmt::Debug for Node2 {
4098 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4099 write!(f, "{}({:#x})", Self::NAME, self)
4100 }
4101}
4102impl ::core::fmt::Display for Node2 {
4103 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4104 write!(f, "{} {{ ", Self::NAME)?;
4105 write!(f, "{}: {}", "addresses", self.addresses())?;
4106 write!(f, ", {}: {}", "flags", self.flags())?;
4107 let extra_count = self.count_extra_fields();
4108 if extra_count != 0 {
4109 write!(f, ", .. ({} fields)", extra_count)?;
4110 }
4111 write!(f, " }}")
4112 }
4113}
4114impl ::core::default::Default for Node2 {
4115 fn default() -> Self {
4116 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4117 Node2::new_unchecked(v)
4118 }
4119}
4120impl Node2 {
4121 const DEFAULT_VALUE: [u8; 24] = [
4122 24, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4123 ];
4124 pub const FIELD_COUNT: usize = 2;
4125 pub fn total_size(&self) -> usize {
4126 molecule::unpack_number(self.as_slice()) as usize
4127 }
4128 pub fn field_count(&self) -> usize {
4129 if self.total_size() == molecule::NUMBER_SIZE {
4130 0
4131 } else {
4132 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4133 }
4134 }
4135 pub fn count_extra_fields(&self) -> usize {
4136 self.field_count() - Self::FIELD_COUNT
4137 }
4138 pub fn has_extra_fields(&self) -> bool {
4139 Self::FIELD_COUNT != self.field_count()
4140 }
4141 pub fn addresses(&self) -> BytesVec {
4142 let slice = self.as_slice();
4143 let start = molecule::unpack_number(&slice[4..]) as usize;
4144 let end = molecule::unpack_number(&slice[8..]) as usize;
4145 BytesVec::new_unchecked(self.0.slice(start..end))
4146 }
4147 pub fn flags(&self) -> Uint64 {
4148 let slice = self.as_slice();
4149 let start = molecule::unpack_number(&slice[8..]) as usize;
4150 if self.has_extra_fields() {
4151 let end = molecule::unpack_number(&slice[12..]) as usize;
4152 Uint64::new_unchecked(self.0.slice(start..end))
4153 } else {
4154 Uint64::new_unchecked(self.0.slice(start..))
4155 }
4156 }
4157 pub fn as_reader<'r>(&'r self) -> Node2Reader<'r> {
4158 Node2Reader::new_unchecked(self.as_slice())
4159 }
4160}
4161impl molecule::prelude::Entity for Node2 {
4162 type Builder = Node2Builder;
4163 const NAME: &'static str = "Node2";
4164 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4165 Node2(data)
4166 }
4167 fn as_bytes(&self) -> molecule::bytes::Bytes {
4168 self.0.clone()
4169 }
4170 fn as_slice(&self) -> &[u8] {
4171 &self.0[..]
4172 }
4173 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4174 Node2Reader::from_slice(slice).map(|reader| reader.to_entity())
4175 }
4176 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4177 Node2Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4178 }
4179 fn new_builder() -> Self::Builder {
4180 ::core::default::Default::default()
4181 }
4182 fn as_builder(self) -> Self::Builder {
4183 Self::new_builder()
4184 .addresses(self.addresses())
4185 .flags(self.flags())
4186 }
4187}
4188#[derive(Clone, Copy)]
4189pub struct Node2Reader<'r>(&'r [u8]);
4190impl<'r> ::core::fmt::LowerHex for Node2Reader<'r> {
4191 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4192 use molecule::hex_string;
4193 if f.alternate() {
4194 write!(f, "0x")?;
4195 }
4196 write!(f, "{}", hex_string(self.as_slice()))
4197 }
4198}
4199impl<'r> ::core::fmt::Debug for Node2Reader<'r> {
4200 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4201 write!(f, "{}({:#x})", Self::NAME, self)
4202 }
4203}
4204impl<'r> ::core::fmt::Display for Node2Reader<'r> {
4205 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4206 write!(f, "{} {{ ", Self::NAME)?;
4207 write!(f, "{}: {}", "addresses", self.addresses())?;
4208 write!(f, ", {}: {}", "flags", self.flags())?;
4209 let extra_count = self.count_extra_fields();
4210 if extra_count != 0 {
4211 write!(f, ", .. ({} fields)", extra_count)?;
4212 }
4213 write!(f, " }}")
4214 }
4215}
4216impl<'r> Node2Reader<'r> {
4217 pub const FIELD_COUNT: usize = 2;
4218 pub fn total_size(&self) -> usize {
4219 molecule::unpack_number(self.as_slice()) as usize
4220 }
4221 pub fn field_count(&self) -> usize {
4222 if self.total_size() == molecule::NUMBER_SIZE {
4223 0
4224 } else {
4225 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4226 }
4227 }
4228 pub fn count_extra_fields(&self) -> usize {
4229 self.field_count() - Self::FIELD_COUNT
4230 }
4231 pub fn has_extra_fields(&self) -> bool {
4232 Self::FIELD_COUNT != self.field_count()
4233 }
4234 pub fn addresses(&self) -> BytesVecReader<'r> {
4235 let slice = self.as_slice();
4236 let start = molecule::unpack_number(&slice[4..]) as usize;
4237 let end = molecule::unpack_number(&slice[8..]) as usize;
4238 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
4239 }
4240 pub fn flags(&self) -> Uint64Reader<'r> {
4241 let slice = self.as_slice();
4242 let start = molecule::unpack_number(&slice[8..]) as usize;
4243 if self.has_extra_fields() {
4244 let end = molecule::unpack_number(&slice[12..]) as usize;
4245 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4246 } else {
4247 Uint64Reader::new_unchecked(&self.as_slice()[start..])
4248 }
4249 }
4250}
4251impl<'r> molecule::prelude::Reader<'r> for Node2Reader<'r> {
4252 type Entity = Node2;
4253 const NAME: &'static str = "Node2Reader";
4254 fn to_entity(&self) -> Self::Entity {
4255 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4256 }
4257 fn new_unchecked(slice: &'r [u8]) -> Self {
4258 Node2Reader(slice)
4259 }
4260 fn as_slice(&self) -> &'r [u8] {
4261 self.0
4262 }
4263 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4264 use molecule::verification_error as ve;
4265 let slice_len = slice.len();
4266 if slice_len < molecule::NUMBER_SIZE {
4267 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4268 }
4269 let total_size = molecule::unpack_number(slice) as usize;
4270 if slice_len != total_size {
4271 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4272 }
4273 if slice_len < molecule::NUMBER_SIZE * 2 {
4274 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4275 }
4276 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4277 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4278 return ve!(Self, OffsetsNotMatch);
4279 }
4280 if slice_len < offset_first {
4281 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4282 }
4283 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4284 if field_count < Self::FIELD_COUNT {
4285 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4286 } else if !compatible && field_count > Self::FIELD_COUNT {
4287 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4288 };
4289 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4290 .chunks_exact(molecule::NUMBER_SIZE)
4291 .map(|x| molecule::unpack_number(x) as usize)
4292 .collect();
4293 offsets.push(total_size);
4294 if offsets.windows(2).any(|i| i[0] > i[1]) {
4295 return ve!(Self, OffsetsNotMatch);
4296 }
4297 BytesVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4298 Uint64Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
4299 Ok(())
4300 }
4301}
4302#[derive(Clone, Debug, Default)]
4303pub struct Node2Builder {
4304 pub(crate) addresses: BytesVec,
4305 pub(crate) flags: Uint64,
4306}
4307impl Node2Builder {
4308 pub const FIELD_COUNT: usize = 2;
4309 pub fn addresses<T>(mut self, v: T) -> Self
4310 where
4311 T: ::core::convert::Into<BytesVec>,
4312 {
4313 self.addresses = v.into();
4314 self
4315 }
4316 pub fn flags<T>(mut self, v: T) -> Self
4317 where
4318 T: ::core::convert::Into<Uint64>,
4319 {
4320 self.flags = v.into();
4321 self
4322 }
4323}
4324impl molecule::prelude::Builder for Node2Builder {
4325 type Entity = Node2;
4326 const NAME: &'static str = "Node2Builder";
4327 fn expected_length(&self) -> usize {
4328 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
4329 + self.addresses.as_slice().len()
4330 + self.flags.as_slice().len()
4331 }
4332 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4333 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4334 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4335 offsets.push(total_size);
4336 total_size += self.addresses.as_slice().len();
4337 offsets.push(total_size);
4338 total_size += self.flags.as_slice().len();
4339 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4340 for offset in offsets.into_iter() {
4341 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4342 }
4343 writer.write_all(self.addresses.as_slice())?;
4344 writer.write_all(self.flags.as_slice())?;
4345 Ok(())
4346 }
4347 fn build(&self) -> Self::Entity {
4348 let mut inner = Vec::with_capacity(self.expected_length());
4349 self.write(&mut inner)
4350 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4351 Node2::new_unchecked(inner.into())
4352 }
4353}
4354#[derive(Clone)]
4355pub struct AddressVec(molecule::bytes::Bytes);
4356impl ::core::fmt::LowerHex for AddressVec {
4357 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4358 use molecule::hex_string;
4359 if f.alternate() {
4360 write!(f, "0x")?;
4361 }
4362 write!(f, "{}", hex_string(self.as_slice()))
4363 }
4364}
4365impl ::core::fmt::Debug for AddressVec {
4366 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4367 write!(f, "{}({:#x})", Self::NAME, self)
4368 }
4369}
4370impl ::core::fmt::Display for AddressVec {
4371 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4372 write!(f, "{} [", Self::NAME)?;
4373 for i in 0..self.len() {
4374 if i == 0 {
4375 write!(f, "{}", self.get_unchecked(i))?;
4376 } else {
4377 write!(f, ", {}", self.get_unchecked(i))?;
4378 }
4379 }
4380 write!(f, "]")
4381 }
4382}
4383impl ::core::default::Default for AddressVec {
4384 fn default() -> Self {
4385 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4386 AddressVec::new_unchecked(v)
4387 }
4388}
4389impl AddressVec {
4390 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
4391 pub fn total_size(&self) -> usize {
4392 molecule::unpack_number(self.as_slice()) as usize
4393 }
4394 pub fn item_count(&self) -> usize {
4395 if self.total_size() == molecule::NUMBER_SIZE {
4396 0
4397 } else {
4398 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4399 }
4400 }
4401 pub fn len(&self) -> usize {
4402 self.item_count()
4403 }
4404 pub fn is_empty(&self) -> bool {
4405 self.len() == 0
4406 }
4407 pub fn get(&self, idx: usize) -> Option<Address> {
4408 if idx >= self.len() {
4409 None
4410 } else {
4411 Some(self.get_unchecked(idx))
4412 }
4413 }
4414 pub fn get_unchecked(&self, idx: usize) -> Address {
4415 let slice = self.as_slice();
4416 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4417 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4418 if idx == self.len() - 1 {
4419 Address::new_unchecked(self.0.slice(start..))
4420 } else {
4421 let end_idx = start_idx + molecule::NUMBER_SIZE;
4422 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4423 Address::new_unchecked(self.0.slice(start..end))
4424 }
4425 }
4426 pub fn as_reader<'r>(&'r self) -> AddressVecReader<'r> {
4427 AddressVecReader::new_unchecked(self.as_slice())
4428 }
4429}
4430impl molecule::prelude::Entity for AddressVec {
4431 type Builder = AddressVecBuilder;
4432 const NAME: &'static str = "AddressVec";
4433 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4434 AddressVec(data)
4435 }
4436 fn as_bytes(&self) -> molecule::bytes::Bytes {
4437 self.0.clone()
4438 }
4439 fn as_slice(&self) -> &[u8] {
4440 &self.0[..]
4441 }
4442 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4443 AddressVecReader::from_slice(slice).map(|reader| reader.to_entity())
4444 }
4445 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4446 AddressVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4447 }
4448 fn new_builder() -> Self::Builder {
4449 ::core::default::Default::default()
4450 }
4451 fn as_builder(self) -> Self::Builder {
4452 Self::new_builder().extend(self.into_iter())
4453 }
4454}
4455#[derive(Clone, Copy)]
4456pub struct AddressVecReader<'r>(&'r [u8]);
4457impl<'r> ::core::fmt::LowerHex for AddressVecReader<'r> {
4458 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4459 use molecule::hex_string;
4460 if f.alternate() {
4461 write!(f, "0x")?;
4462 }
4463 write!(f, "{}", hex_string(self.as_slice()))
4464 }
4465}
4466impl<'r> ::core::fmt::Debug for AddressVecReader<'r> {
4467 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4468 write!(f, "{}({:#x})", Self::NAME, self)
4469 }
4470}
4471impl<'r> ::core::fmt::Display for AddressVecReader<'r> {
4472 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4473 write!(f, "{} [", Self::NAME)?;
4474 for i in 0..self.len() {
4475 if i == 0 {
4476 write!(f, "{}", self.get_unchecked(i))?;
4477 } else {
4478 write!(f, ", {}", self.get_unchecked(i))?;
4479 }
4480 }
4481 write!(f, "]")
4482 }
4483}
4484impl<'r> AddressVecReader<'r> {
4485 pub fn total_size(&self) -> usize {
4486 molecule::unpack_number(self.as_slice()) as usize
4487 }
4488 pub fn item_count(&self) -> usize {
4489 if self.total_size() == molecule::NUMBER_SIZE {
4490 0
4491 } else {
4492 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4493 }
4494 }
4495 pub fn len(&self) -> usize {
4496 self.item_count()
4497 }
4498 pub fn is_empty(&self) -> bool {
4499 self.len() == 0
4500 }
4501 pub fn get(&self, idx: usize) -> Option<AddressReader<'r>> {
4502 if idx >= self.len() {
4503 None
4504 } else {
4505 Some(self.get_unchecked(idx))
4506 }
4507 }
4508 pub fn get_unchecked(&self, idx: usize) -> AddressReader<'r> {
4509 let slice = self.as_slice();
4510 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4511 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4512 if idx == self.len() - 1 {
4513 AddressReader::new_unchecked(&self.as_slice()[start..])
4514 } else {
4515 let end_idx = start_idx + molecule::NUMBER_SIZE;
4516 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4517 AddressReader::new_unchecked(&self.as_slice()[start..end])
4518 }
4519 }
4520}
4521impl<'r> molecule::prelude::Reader<'r> for AddressVecReader<'r> {
4522 type Entity = AddressVec;
4523 const NAME: &'static str = "AddressVecReader";
4524 fn to_entity(&self) -> Self::Entity {
4525 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4526 }
4527 fn new_unchecked(slice: &'r [u8]) -> Self {
4528 AddressVecReader(slice)
4529 }
4530 fn as_slice(&self) -> &'r [u8] {
4531 self.0
4532 }
4533 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4534 use molecule::verification_error as ve;
4535 let slice_len = slice.len();
4536 if slice_len < molecule::NUMBER_SIZE {
4537 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4538 }
4539 let total_size = molecule::unpack_number(slice) as usize;
4540 if slice_len != total_size {
4541 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4542 }
4543 if slice_len == molecule::NUMBER_SIZE {
4544 return Ok(());
4545 }
4546 if slice_len < molecule::NUMBER_SIZE * 2 {
4547 return ve!(
4548 Self,
4549 TotalSizeNotMatch,
4550 molecule::NUMBER_SIZE * 2,
4551 slice_len
4552 );
4553 }
4554 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4555 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4556 return ve!(Self, OffsetsNotMatch);
4557 }
4558 if slice_len < offset_first {
4559 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4560 }
4561 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4562 .chunks_exact(molecule::NUMBER_SIZE)
4563 .map(|x| molecule::unpack_number(x) as usize)
4564 .collect();
4565 offsets.push(total_size);
4566 if offsets.windows(2).any(|i| i[0] > i[1]) {
4567 return ve!(Self, OffsetsNotMatch);
4568 }
4569 for pair in offsets.windows(2) {
4570 let start = pair[0];
4571 let end = pair[1];
4572 AddressReader::verify(&slice[start..end], compatible)?;
4573 }
4574 Ok(())
4575 }
4576}
4577#[derive(Clone, Debug, Default)]
4578pub struct AddressVecBuilder(pub(crate) Vec<Address>);
4579impl AddressVecBuilder {
4580 pub fn set(mut self, v: Vec<Address>) -> Self {
4581 self.0 = v;
4582 self
4583 }
4584 pub fn push<T>(mut self, v: T) -> Self
4585 where
4586 T: ::core::convert::Into<Address>,
4587 {
4588 self.0.push(v.into());
4589 self
4590 }
4591 pub fn extend<T: ::core::iter::IntoIterator<Item = Address>>(mut self, iter: T) -> Self {
4592 self.0.extend(iter);
4593 self
4594 }
4595 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Address>
4596 where
4597 T: ::core::convert::Into<Address>,
4598 {
4599 self.0
4600 .get_mut(index)
4601 .map(|item| ::core::mem::replace(item, v.into()))
4602 }
4603}
4604impl molecule::prelude::Builder for AddressVecBuilder {
4605 type Entity = AddressVec;
4606 const NAME: &'static str = "AddressVecBuilder";
4607 fn expected_length(&self) -> usize {
4608 molecule::NUMBER_SIZE * (self.0.len() + 1)
4609 + self
4610 .0
4611 .iter()
4612 .map(|inner| inner.as_slice().len())
4613 .sum::<usize>()
4614 }
4615 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4616 let item_count = self.0.len();
4617 if item_count == 0 {
4618 writer.write_all(&molecule::pack_number(
4619 molecule::NUMBER_SIZE as molecule::Number,
4620 ))?;
4621 } else {
4622 let (total_size, offsets) = self.0.iter().fold(
4623 (
4624 molecule::NUMBER_SIZE * (item_count + 1),
4625 Vec::with_capacity(item_count),
4626 ),
4627 |(start, mut offsets), inner| {
4628 offsets.push(start);
4629 (start + inner.as_slice().len(), offsets)
4630 },
4631 );
4632 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4633 for offset in offsets.into_iter() {
4634 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4635 }
4636 for inner in self.0.iter() {
4637 writer.write_all(inner.as_slice())?;
4638 }
4639 }
4640 Ok(())
4641 }
4642 fn build(&self) -> Self::Entity {
4643 let mut inner = Vec::with_capacity(self.expected_length());
4644 self.write(&mut inner)
4645 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4646 AddressVec::new_unchecked(inner.into())
4647 }
4648}
4649pub struct AddressVecIterator(AddressVec, usize, usize);
4650impl ::core::iter::Iterator for AddressVecIterator {
4651 type Item = Address;
4652 fn next(&mut self) -> Option<Self::Item> {
4653 if self.1 >= self.2 {
4654 None
4655 } else {
4656 let ret = self.0.get_unchecked(self.1);
4657 self.1 += 1;
4658 Some(ret)
4659 }
4660 }
4661}
4662impl ::core::iter::ExactSizeIterator for AddressVecIterator {
4663 fn len(&self) -> usize {
4664 self.2 - self.1
4665 }
4666}
4667impl ::core::iter::IntoIterator for AddressVec {
4668 type Item = Address;
4669 type IntoIter = AddressVecIterator;
4670 fn into_iter(self) -> Self::IntoIter {
4671 let len = self.len();
4672 AddressVecIterator(self, 0, len)
4673 }
4674}
4675impl<'r> AddressVecReader<'r> {
4676 pub fn iter<'t>(&'t self) -> AddressVecReaderIterator<'t, 'r> {
4677 AddressVecReaderIterator(&self, 0, self.len())
4678 }
4679}
4680pub struct AddressVecReaderIterator<'t, 'r>(&'t AddressVecReader<'r>, usize, usize);
4681impl<'t: 'r, 'r> ::core::iter::Iterator for AddressVecReaderIterator<'t, 'r> {
4682 type Item = AddressReader<'t>;
4683 fn next(&mut self) -> Option<Self::Item> {
4684 if self.1 >= self.2 {
4685 None
4686 } else {
4687 let ret = self.0.get_unchecked(self.1);
4688 self.1 += 1;
4689 Some(ret)
4690 }
4691 }
4692}
4693impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for AddressVecReaderIterator<'t, 'r> {
4694 fn len(&self) -> usize {
4695 self.2 - self.1
4696 }
4697}
4698impl<T> ::core::iter::FromIterator<T> for AddressVec
4699where
4700 T: Into<Address>,
4701{
4702 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
4703 Self::new_builder()
4704 .extend(iter.into_iter().map(Into::into))
4705 .build()
4706 }
4707}
4708impl<T> From<Vec<T>> for AddressVec
4709where
4710 T: Into<Address>,
4711{
4712 fn from(v: Vec<T>) -> Self {
4713 v.into_iter().collect()
4714 }
4715}
4716#[derive(Clone)]
4717pub struct Address(molecule::bytes::Bytes);
4718impl ::core::fmt::LowerHex for Address {
4719 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4720 use molecule::hex_string;
4721 if f.alternate() {
4722 write!(f, "0x")?;
4723 }
4724 write!(f, "{}", hex_string(self.as_slice()))
4725 }
4726}
4727impl ::core::fmt::Debug for Address {
4728 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4729 write!(f, "{}({:#x})", Self::NAME, self)
4730 }
4731}
4732impl ::core::fmt::Display for Address {
4733 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4734 write!(f, "{} {{ ", Self::NAME)?;
4735 write!(f, "{}: {}", "bytes", self.bytes())?;
4736 let extra_count = self.count_extra_fields();
4737 if extra_count != 0 {
4738 write!(f, ", .. ({} fields)", extra_count)?;
4739 }
4740 write!(f, " }}")
4741 }
4742}
4743impl ::core::default::Default for Address {
4744 fn default() -> Self {
4745 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4746 Address::new_unchecked(v)
4747 }
4748}
4749impl Address {
4750 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
4751 pub const FIELD_COUNT: usize = 1;
4752 pub fn total_size(&self) -> usize {
4753 molecule::unpack_number(self.as_slice()) as usize
4754 }
4755 pub fn field_count(&self) -> usize {
4756 if self.total_size() == molecule::NUMBER_SIZE {
4757 0
4758 } else {
4759 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4760 }
4761 }
4762 pub fn count_extra_fields(&self) -> usize {
4763 self.field_count() - Self::FIELD_COUNT
4764 }
4765 pub fn has_extra_fields(&self) -> bool {
4766 Self::FIELD_COUNT != self.field_count()
4767 }
4768 pub fn bytes(&self) -> Bytes {
4769 let slice = self.as_slice();
4770 let start = molecule::unpack_number(&slice[4..]) as usize;
4771 if self.has_extra_fields() {
4772 let end = molecule::unpack_number(&slice[8..]) as usize;
4773 Bytes::new_unchecked(self.0.slice(start..end))
4774 } else {
4775 Bytes::new_unchecked(self.0.slice(start..))
4776 }
4777 }
4778 pub fn as_reader<'r>(&'r self) -> AddressReader<'r> {
4779 AddressReader::new_unchecked(self.as_slice())
4780 }
4781}
4782impl molecule::prelude::Entity for Address {
4783 type Builder = AddressBuilder;
4784 const NAME: &'static str = "Address";
4785 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4786 Address(data)
4787 }
4788 fn as_bytes(&self) -> molecule::bytes::Bytes {
4789 self.0.clone()
4790 }
4791 fn as_slice(&self) -> &[u8] {
4792 &self.0[..]
4793 }
4794 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4795 AddressReader::from_slice(slice).map(|reader| reader.to_entity())
4796 }
4797 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4798 AddressReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4799 }
4800 fn new_builder() -> Self::Builder {
4801 ::core::default::Default::default()
4802 }
4803 fn as_builder(self) -> Self::Builder {
4804 Self::new_builder().bytes(self.bytes())
4805 }
4806}
4807#[derive(Clone, Copy)]
4808pub struct AddressReader<'r>(&'r [u8]);
4809impl<'r> ::core::fmt::LowerHex for AddressReader<'r> {
4810 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4811 use molecule::hex_string;
4812 if f.alternate() {
4813 write!(f, "0x")?;
4814 }
4815 write!(f, "{}", hex_string(self.as_slice()))
4816 }
4817}
4818impl<'r> ::core::fmt::Debug for AddressReader<'r> {
4819 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4820 write!(f, "{}({:#x})", Self::NAME, self)
4821 }
4822}
4823impl<'r> ::core::fmt::Display for AddressReader<'r> {
4824 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4825 write!(f, "{} {{ ", Self::NAME)?;
4826 write!(f, "{}: {}", "bytes", self.bytes())?;
4827 let extra_count = self.count_extra_fields();
4828 if extra_count != 0 {
4829 write!(f, ", .. ({} fields)", extra_count)?;
4830 }
4831 write!(f, " }}")
4832 }
4833}
4834impl<'r> AddressReader<'r> {
4835 pub const FIELD_COUNT: usize = 1;
4836 pub fn total_size(&self) -> usize {
4837 molecule::unpack_number(self.as_slice()) as usize
4838 }
4839 pub fn field_count(&self) -> usize {
4840 if self.total_size() == molecule::NUMBER_SIZE {
4841 0
4842 } else {
4843 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4844 }
4845 }
4846 pub fn count_extra_fields(&self) -> usize {
4847 self.field_count() - Self::FIELD_COUNT
4848 }
4849 pub fn has_extra_fields(&self) -> bool {
4850 Self::FIELD_COUNT != self.field_count()
4851 }
4852 pub fn bytes(&self) -> BytesReader<'r> {
4853 let slice = self.as_slice();
4854 let start = molecule::unpack_number(&slice[4..]) as usize;
4855 if self.has_extra_fields() {
4856 let end = molecule::unpack_number(&slice[8..]) as usize;
4857 BytesReader::new_unchecked(&self.as_slice()[start..end])
4858 } else {
4859 BytesReader::new_unchecked(&self.as_slice()[start..])
4860 }
4861 }
4862}
4863impl<'r> molecule::prelude::Reader<'r> for AddressReader<'r> {
4864 type Entity = Address;
4865 const NAME: &'static str = "AddressReader";
4866 fn to_entity(&self) -> Self::Entity {
4867 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4868 }
4869 fn new_unchecked(slice: &'r [u8]) -> Self {
4870 AddressReader(slice)
4871 }
4872 fn as_slice(&self) -> &'r [u8] {
4873 self.0
4874 }
4875 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4876 use molecule::verification_error as ve;
4877 let slice_len = slice.len();
4878 if slice_len < molecule::NUMBER_SIZE {
4879 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4880 }
4881 let total_size = molecule::unpack_number(slice) as usize;
4882 if slice_len != total_size {
4883 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4884 }
4885 if slice_len < molecule::NUMBER_SIZE * 2 {
4886 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4887 }
4888 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4889 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4890 return ve!(Self, OffsetsNotMatch);
4891 }
4892 if slice_len < offset_first {
4893 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4894 }
4895 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4896 if field_count < Self::FIELD_COUNT {
4897 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4898 } else if !compatible && field_count > Self::FIELD_COUNT {
4899 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4900 };
4901 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4902 .chunks_exact(molecule::NUMBER_SIZE)
4903 .map(|x| molecule::unpack_number(x) as usize)
4904 .collect();
4905 offsets.push(total_size);
4906 if offsets.windows(2).any(|i| i[0] > i[1]) {
4907 return ve!(Self, OffsetsNotMatch);
4908 }
4909 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4910 Ok(())
4911 }
4912}
4913#[derive(Clone, Debug, Default)]
4914pub struct AddressBuilder {
4915 pub(crate) bytes: Bytes,
4916}
4917impl AddressBuilder {
4918 pub const FIELD_COUNT: usize = 1;
4919 pub fn bytes<T>(mut self, v: T) -> Self
4920 where
4921 T: ::core::convert::Into<Bytes>,
4922 {
4923 self.bytes = v.into();
4924 self
4925 }
4926}
4927impl molecule::prelude::Builder for AddressBuilder {
4928 type Entity = Address;
4929 const NAME: &'static str = "AddressBuilder";
4930 fn expected_length(&self) -> usize {
4931 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.bytes.as_slice().len()
4932 }
4933 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4934 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4935 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4936 offsets.push(total_size);
4937 total_size += self.bytes.as_slice().len();
4938 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4939 for offset in offsets.into_iter() {
4940 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4941 }
4942 writer.write_all(self.bytes.as_slice())?;
4943 Ok(())
4944 }
4945 fn build(&self) -> Self::Entity {
4946 let mut inner = Vec::with_capacity(self.expected_length());
4947 self.write(&mut inner)
4948 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4949 Address::new_unchecked(inner.into())
4950 }
4951}
4952#[derive(Clone)]
4953pub struct IdentifyMessage(molecule::bytes::Bytes);
4954impl ::core::fmt::LowerHex for IdentifyMessage {
4955 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4956 use molecule::hex_string;
4957 if f.alternate() {
4958 write!(f, "0x")?;
4959 }
4960 write!(f, "{}", hex_string(self.as_slice()))
4961 }
4962}
4963impl ::core::fmt::Debug for IdentifyMessage {
4964 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4965 write!(f, "{}({:#x})", Self::NAME, self)
4966 }
4967}
4968impl ::core::fmt::Display for IdentifyMessage {
4969 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4970 write!(f, "{} {{ ", Self::NAME)?;
4971 write!(f, "{}: {}", "listen_addrs", self.listen_addrs())?;
4972 write!(f, ", {}: {}", "observed_addr", self.observed_addr())?;
4973 write!(f, ", {}: {}", "identify", self.identify())?;
4974 let extra_count = self.count_extra_fields();
4975 if extra_count != 0 {
4976 write!(f, ", .. ({} fields)", extra_count)?;
4977 }
4978 write!(f, " }}")
4979 }
4980}
4981impl ::core::default::Default for IdentifyMessage {
4982 fn default() -> Self {
4983 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4984 IdentifyMessage::new_unchecked(v)
4985 }
4986}
4987impl IdentifyMessage {
4988 const DEFAULT_VALUE: [u8; 36] = [
4989 36, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 32, 0, 0, 0, 4, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, 0, 0,
4990 0, 0, 0, 0, 0, 0, 0,
4991 ];
4992 pub const FIELD_COUNT: usize = 3;
4993 pub fn total_size(&self) -> usize {
4994 molecule::unpack_number(self.as_slice()) as usize
4995 }
4996 pub fn field_count(&self) -> usize {
4997 if self.total_size() == molecule::NUMBER_SIZE {
4998 0
4999 } else {
5000 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5001 }
5002 }
5003 pub fn count_extra_fields(&self) -> usize {
5004 self.field_count() - Self::FIELD_COUNT
5005 }
5006 pub fn has_extra_fields(&self) -> bool {
5007 Self::FIELD_COUNT != self.field_count()
5008 }
5009 pub fn listen_addrs(&self) -> AddressVec {
5010 let slice = self.as_slice();
5011 let start = molecule::unpack_number(&slice[4..]) as usize;
5012 let end = molecule::unpack_number(&slice[8..]) as usize;
5013 AddressVec::new_unchecked(self.0.slice(start..end))
5014 }
5015 pub fn observed_addr(&self) -> Address {
5016 let slice = self.as_slice();
5017 let start = molecule::unpack_number(&slice[8..]) as usize;
5018 let end = molecule::unpack_number(&slice[12..]) as usize;
5019 Address::new_unchecked(self.0.slice(start..end))
5020 }
5021 pub fn identify(&self) -> Bytes {
5022 let slice = self.as_slice();
5023 let start = molecule::unpack_number(&slice[12..]) as usize;
5024 if self.has_extra_fields() {
5025 let end = molecule::unpack_number(&slice[16..]) as usize;
5026 Bytes::new_unchecked(self.0.slice(start..end))
5027 } else {
5028 Bytes::new_unchecked(self.0.slice(start..))
5029 }
5030 }
5031 pub fn as_reader<'r>(&'r self) -> IdentifyMessageReader<'r> {
5032 IdentifyMessageReader::new_unchecked(self.as_slice())
5033 }
5034}
5035impl molecule::prelude::Entity for IdentifyMessage {
5036 type Builder = IdentifyMessageBuilder;
5037 const NAME: &'static str = "IdentifyMessage";
5038 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5039 IdentifyMessage(data)
5040 }
5041 fn as_bytes(&self) -> molecule::bytes::Bytes {
5042 self.0.clone()
5043 }
5044 fn as_slice(&self) -> &[u8] {
5045 &self.0[..]
5046 }
5047 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5048 IdentifyMessageReader::from_slice(slice).map(|reader| reader.to_entity())
5049 }
5050 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5051 IdentifyMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5052 }
5053 fn new_builder() -> Self::Builder {
5054 ::core::default::Default::default()
5055 }
5056 fn as_builder(self) -> Self::Builder {
5057 Self::new_builder()
5058 .listen_addrs(self.listen_addrs())
5059 .observed_addr(self.observed_addr())
5060 .identify(self.identify())
5061 }
5062}
5063#[derive(Clone, Copy)]
5064pub struct IdentifyMessageReader<'r>(&'r [u8]);
5065impl<'r> ::core::fmt::LowerHex for IdentifyMessageReader<'r> {
5066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5067 use molecule::hex_string;
5068 if f.alternate() {
5069 write!(f, "0x")?;
5070 }
5071 write!(f, "{}", hex_string(self.as_slice()))
5072 }
5073}
5074impl<'r> ::core::fmt::Debug for IdentifyMessageReader<'r> {
5075 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5076 write!(f, "{}({:#x})", Self::NAME, self)
5077 }
5078}
5079impl<'r> ::core::fmt::Display for IdentifyMessageReader<'r> {
5080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5081 write!(f, "{} {{ ", Self::NAME)?;
5082 write!(f, "{}: {}", "listen_addrs", self.listen_addrs())?;
5083 write!(f, ", {}: {}", "observed_addr", self.observed_addr())?;
5084 write!(f, ", {}: {}", "identify", self.identify())?;
5085 let extra_count = self.count_extra_fields();
5086 if extra_count != 0 {
5087 write!(f, ", .. ({} fields)", extra_count)?;
5088 }
5089 write!(f, " }}")
5090 }
5091}
5092impl<'r> IdentifyMessageReader<'r> {
5093 pub const FIELD_COUNT: usize = 3;
5094 pub fn total_size(&self) -> usize {
5095 molecule::unpack_number(self.as_slice()) as usize
5096 }
5097 pub fn field_count(&self) -> usize {
5098 if self.total_size() == molecule::NUMBER_SIZE {
5099 0
5100 } else {
5101 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5102 }
5103 }
5104 pub fn count_extra_fields(&self) -> usize {
5105 self.field_count() - Self::FIELD_COUNT
5106 }
5107 pub fn has_extra_fields(&self) -> bool {
5108 Self::FIELD_COUNT != self.field_count()
5109 }
5110 pub fn listen_addrs(&self) -> AddressVecReader<'r> {
5111 let slice = self.as_slice();
5112 let start = molecule::unpack_number(&slice[4..]) as usize;
5113 let end = molecule::unpack_number(&slice[8..]) as usize;
5114 AddressVecReader::new_unchecked(&self.as_slice()[start..end])
5115 }
5116 pub fn observed_addr(&self) -> AddressReader<'r> {
5117 let slice = self.as_slice();
5118 let start = molecule::unpack_number(&slice[8..]) as usize;
5119 let end = molecule::unpack_number(&slice[12..]) as usize;
5120 AddressReader::new_unchecked(&self.as_slice()[start..end])
5121 }
5122 pub fn identify(&self) -> BytesReader<'r> {
5123 let slice = self.as_slice();
5124 let start = molecule::unpack_number(&slice[12..]) as usize;
5125 if self.has_extra_fields() {
5126 let end = molecule::unpack_number(&slice[16..]) as usize;
5127 BytesReader::new_unchecked(&self.as_slice()[start..end])
5128 } else {
5129 BytesReader::new_unchecked(&self.as_slice()[start..])
5130 }
5131 }
5132}
5133impl<'r> molecule::prelude::Reader<'r> for IdentifyMessageReader<'r> {
5134 type Entity = IdentifyMessage;
5135 const NAME: &'static str = "IdentifyMessageReader";
5136 fn to_entity(&self) -> Self::Entity {
5137 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5138 }
5139 fn new_unchecked(slice: &'r [u8]) -> Self {
5140 IdentifyMessageReader(slice)
5141 }
5142 fn as_slice(&self) -> &'r [u8] {
5143 self.0
5144 }
5145 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5146 use molecule::verification_error as ve;
5147 let slice_len = slice.len();
5148 if slice_len < molecule::NUMBER_SIZE {
5149 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5150 }
5151 let total_size = molecule::unpack_number(slice) as usize;
5152 if slice_len != total_size {
5153 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5154 }
5155 if slice_len < molecule::NUMBER_SIZE * 2 {
5156 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5157 }
5158 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5159 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5160 return ve!(Self, OffsetsNotMatch);
5161 }
5162 if slice_len < offset_first {
5163 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5164 }
5165 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5166 if field_count < Self::FIELD_COUNT {
5167 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5168 } else if !compatible && field_count > Self::FIELD_COUNT {
5169 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5170 };
5171 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5172 .chunks_exact(molecule::NUMBER_SIZE)
5173 .map(|x| molecule::unpack_number(x) as usize)
5174 .collect();
5175 offsets.push(total_size);
5176 if offsets.windows(2).any(|i| i[0] > i[1]) {
5177 return ve!(Self, OffsetsNotMatch);
5178 }
5179 AddressVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5180 AddressReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5181 BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5182 Ok(())
5183 }
5184}
5185#[derive(Clone, Debug, Default)]
5186pub struct IdentifyMessageBuilder {
5187 pub(crate) listen_addrs: AddressVec,
5188 pub(crate) observed_addr: Address,
5189 pub(crate) identify: Bytes,
5190}
5191impl IdentifyMessageBuilder {
5192 pub const FIELD_COUNT: usize = 3;
5193 pub fn listen_addrs<T>(mut self, v: T) -> Self
5194 where
5195 T: ::core::convert::Into<AddressVec>,
5196 {
5197 self.listen_addrs = v.into();
5198 self
5199 }
5200 pub fn observed_addr<T>(mut self, v: T) -> Self
5201 where
5202 T: ::core::convert::Into<Address>,
5203 {
5204 self.observed_addr = v.into();
5205 self
5206 }
5207 pub fn identify<T>(mut self, v: T) -> Self
5208 where
5209 T: ::core::convert::Into<Bytes>,
5210 {
5211 self.identify = v.into();
5212 self
5213 }
5214}
5215impl molecule::prelude::Builder for IdentifyMessageBuilder {
5216 type Entity = IdentifyMessage;
5217 const NAME: &'static str = "IdentifyMessageBuilder";
5218 fn expected_length(&self) -> usize {
5219 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5220 + self.listen_addrs.as_slice().len()
5221 + self.observed_addr.as_slice().len()
5222 + self.identify.as_slice().len()
5223 }
5224 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5225 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5226 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5227 offsets.push(total_size);
5228 total_size += self.listen_addrs.as_slice().len();
5229 offsets.push(total_size);
5230 total_size += self.observed_addr.as_slice().len();
5231 offsets.push(total_size);
5232 total_size += self.identify.as_slice().len();
5233 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5234 for offset in offsets.into_iter() {
5235 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5236 }
5237 writer.write_all(self.listen_addrs.as_slice())?;
5238 writer.write_all(self.observed_addr.as_slice())?;
5239 writer.write_all(self.identify.as_slice())?;
5240 Ok(())
5241 }
5242 fn build(&self) -> Self::Entity {
5243 let mut inner = Vec::with_capacity(self.expected_length());
5244 self.write(&mut inner)
5245 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5246 IdentifyMessage::new_unchecked(inner.into())
5247 }
5248}
5249#[derive(Clone)]
5250pub struct HolePunchingMessage(molecule::bytes::Bytes);
5251impl ::core::fmt::LowerHex for HolePunchingMessage {
5252 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5253 use molecule::hex_string;
5254 if f.alternate() {
5255 write!(f, "0x")?;
5256 }
5257 write!(f, "{}", hex_string(self.as_slice()))
5258 }
5259}
5260impl ::core::fmt::Debug for HolePunchingMessage {
5261 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5262 write!(f, "{}({:#x})", Self::NAME, self)
5263 }
5264}
5265impl ::core::fmt::Display for HolePunchingMessage {
5266 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5267 write!(f, "{}(", Self::NAME)?;
5268 self.to_enum().display_inner(f)?;
5269 write!(f, ")")
5270 }
5271}
5272impl ::core::default::Default for HolePunchingMessage {
5273 fn default() -> Self {
5274 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5275 HolePunchingMessage::new_unchecked(v)
5276 }
5277}
5278impl HolePunchingMessage {
5279 const DEFAULT_VALUE: [u8; 45] = [
5280 0, 0, 0, 0, 41, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 33, 0, 0, 0, 37, 0, 0, 0,
5281 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
5282 ];
5283 pub const ITEMS_COUNT: usize = 3;
5284 pub fn item_id(&self) -> molecule::Number {
5285 molecule::unpack_number(self.as_slice())
5286 }
5287 pub fn to_enum(&self) -> HolePunchingMessageUnion {
5288 let inner = self.0.slice(molecule::NUMBER_SIZE..);
5289 match self.item_id() {
5290 0 => ConnectionRequest::new_unchecked(inner).into(),
5291 1 => ConnectionRequestDelivered::new_unchecked(inner).into(),
5292 2 => ConnectionSync::new_unchecked(inner).into(),
5293 _ => panic!("{}: invalid data", Self::NAME),
5294 }
5295 }
5296 pub fn as_reader<'r>(&'r self) -> HolePunchingMessageReader<'r> {
5297 HolePunchingMessageReader::new_unchecked(self.as_slice())
5298 }
5299}
5300impl molecule::prelude::Entity for HolePunchingMessage {
5301 type Builder = HolePunchingMessageBuilder;
5302 const NAME: &'static str = "HolePunchingMessage";
5303 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5304 HolePunchingMessage(data)
5305 }
5306 fn as_bytes(&self) -> molecule::bytes::Bytes {
5307 self.0.clone()
5308 }
5309 fn as_slice(&self) -> &[u8] {
5310 &self.0[..]
5311 }
5312 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5313 HolePunchingMessageReader::from_slice(slice).map(|reader| reader.to_entity())
5314 }
5315 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5316 HolePunchingMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5317 }
5318 fn new_builder() -> Self::Builder {
5319 ::core::default::Default::default()
5320 }
5321 fn as_builder(self) -> Self::Builder {
5322 Self::new_builder().set(self.to_enum())
5323 }
5324}
5325#[derive(Clone, Copy)]
5326pub struct HolePunchingMessageReader<'r>(&'r [u8]);
5327impl<'r> ::core::fmt::LowerHex for HolePunchingMessageReader<'r> {
5328 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5329 use molecule::hex_string;
5330 if f.alternate() {
5331 write!(f, "0x")?;
5332 }
5333 write!(f, "{}", hex_string(self.as_slice()))
5334 }
5335}
5336impl<'r> ::core::fmt::Debug for HolePunchingMessageReader<'r> {
5337 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5338 write!(f, "{}({:#x})", Self::NAME, self)
5339 }
5340}
5341impl<'r> ::core::fmt::Display for HolePunchingMessageReader<'r> {
5342 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5343 write!(f, "{}(", Self::NAME)?;
5344 self.to_enum().display_inner(f)?;
5345 write!(f, ")")
5346 }
5347}
5348impl<'r> HolePunchingMessageReader<'r> {
5349 pub const ITEMS_COUNT: usize = 3;
5350 pub fn item_id(&self) -> molecule::Number {
5351 molecule::unpack_number(self.as_slice())
5352 }
5353 pub fn to_enum(&self) -> HolePunchingMessageUnionReader<'r> {
5354 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
5355 match self.item_id() {
5356 0 => ConnectionRequestReader::new_unchecked(inner).into(),
5357 1 => ConnectionRequestDeliveredReader::new_unchecked(inner).into(),
5358 2 => ConnectionSyncReader::new_unchecked(inner).into(),
5359 _ => panic!("{}: invalid data", Self::NAME),
5360 }
5361 }
5362}
5363impl<'r> molecule::prelude::Reader<'r> for HolePunchingMessageReader<'r> {
5364 type Entity = HolePunchingMessage;
5365 const NAME: &'static str = "HolePunchingMessageReader";
5366 fn to_entity(&self) -> Self::Entity {
5367 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5368 }
5369 fn new_unchecked(slice: &'r [u8]) -> Self {
5370 HolePunchingMessageReader(slice)
5371 }
5372 fn as_slice(&self) -> &'r [u8] {
5373 self.0
5374 }
5375 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5376 use molecule::verification_error as ve;
5377 let slice_len = slice.len();
5378 if slice_len < molecule::NUMBER_SIZE {
5379 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5380 }
5381 let item_id = molecule::unpack_number(slice);
5382 let inner_slice = &slice[molecule::NUMBER_SIZE..];
5383 match item_id {
5384 0 => ConnectionRequestReader::verify(inner_slice, compatible),
5385 1 => ConnectionRequestDeliveredReader::verify(inner_slice, compatible),
5386 2 => ConnectionSyncReader::verify(inner_slice, compatible),
5387 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
5388 }?;
5389 Ok(())
5390 }
5391}
5392#[derive(Clone, Debug, Default)]
5393pub struct HolePunchingMessageBuilder(pub(crate) HolePunchingMessageUnion);
5394impl HolePunchingMessageBuilder {
5395 pub const ITEMS_COUNT: usize = 3;
5396 pub fn set<I>(mut self, v: I) -> Self
5397 where
5398 I: ::core::convert::Into<HolePunchingMessageUnion>,
5399 {
5400 self.0 = v.into();
5401 self
5402 }
5403}
5404impl molecule::prelude::Builder for HolePunchingMessageBuilder {
5405 type Entity = HolePunchingMessage;
5406 const NAME: &'static str = "HolePunchingMessageBuilder";
5407 fn expected_length(&self) -> usize {
5408 molecule::NUMBER_SIZE + self.0.as_slice().len()
5409 }
5410 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5411 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
5412 writer.write_all(self.0.as_slice())
5413 }
5414 fn build(&self) -> Self::Entity {
5415 let mut inner = Vec::with_capacity(self.expected_length());
5416 self.write(&mut inner)
5417 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5418 HolePunchingMessage::new_unchecked(inner.into())
5419 }
5420}
5421#[derive(Debug, Clone)]
5422pub enum HolePunchingMessageUnion {
5423 ConnectionRequest(ConnectionRequest),
5424 ConnectionRequestDelivered(ConnectionRequestDelivered),
5425 ConnectionSync(ConnectionSync),
5426}
5427#[derive(Debug, Clone, Copy)]
5428pub enum HolePunchingMessageUnionReader<'r> {
5429 ConnectionRequest(ConnectionRequestReader<'r>),
5430 ConnectionRequestDelivered(ConnectionRequestDeliveredReader<'r>),
5431 ConnectionSync(ConnectionSyncReader<'r>),
5432}
5433impl ::core::default::Default for HolePunchingMessageUnion {
5434 fn default() -> Self {
5435 HolePunchingMessageUnion::ConnectionRequest(::core::default::Default::default())
5436 }
5437}
5438impl ::core::fmt::Display for HolePunchingMessageUnion {
5439 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5440 match self {
5441 HolePunchingMessageUnion::ConnectionRequest(ref item) => {
5442 write!(f, "{}::{}({})", Self::NAME, ConnectionRequest::NAME, item)
5443 }
5444 HolePunchingMessageUnion::ConnectionRequestDelivered(ref item) => {
5445 write!(
5446 f,
5447 "{}::{}({})",
5448 Self::NAME,
5449 ConnectionRequestDelivered::NAME,
5450 item
5451 )
5452 }
5453 HolePunchingMessageUnion::ConnectionSync(ref item) => {
5454 write!(f, "{}::{}({})", Self::NAME, ConnectionSync::NAME, item)
5455 }
5456 }
5457 }
5458}
5459impl<'r> ::core::fmt::Display for HolePunchingMessageUnionReader<'r> {
5460 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5461 match self {
5462 HolePunchingMessageUnionReader::ConnectionRequest(ref item) => {
5463 write!(f, "{}::{}({})", Self::NAME, ConnectionRequest::NAME, item)
5464 }
5465 HolePunchingMessageUnionReader::ConnectionRequestDelivered(ref item) => {
5466 write!(
5467 f,
5468 "{}::{}({})",
5469 Self::NAME,
5470 ConnectionRequestDelivered::NAME,
5471 item
5472 )
5473 }
5474 HolePunchingMessageUnionReader::ConnectionSync(ref item) => {
5475 write!(f, "{}::{}({})", Self::NAME, ConnectionSync::NAME, item)
5476 }
5477 }
5478 }
5479}
5480impl HolePunchingMessageUnion {
5481 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5482 match self {
5483 HolePunchingMessageUnion::ConnectionRequest(ref item) => write!(f, "{}", item),
5484 HolePunchingMessageUnion::ConnectionRequestDelivered(ref item) => write!(f, "{}", item),
5485 HolePunchingMessageUnion::ConnectionSync(ref item) => write!(f, "{}", item),
5486 }
5487 }
5488}
5489impl<'r> HolePunchingMessageUnionReader<'r> {
5490 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5491 match self {
5492 HolePunchingMessageUnionReader::ConnectionRequest(ref item) => write!(f, "{}", item),
5493 HolePunchingMessageUnionReader::ConnectionRequestDelivered(ref item) => {
5494 write!(f, "{}", item)
5495 }
5496 HolePunchingMessageUnionReader::ConnectionSync(ref item) => write!(f, "{}", item),
5497 }
5498 }
5499}
5500impl ::core::convert::From<ConnectionRequest> for HolePunchingMessageUnion {
5501 fn from(item: ConnectionRequest) -> Self {
5502 HolePunchingMessageUnion::ConnectionRequest(item)
5503 }
5504}
5505impl ::core::convert::From<ConnectionRequestDelivered> for HolePunchingMessageUnion {
5506 fn from(item: ConnectionRequestDelivered) -> Self {
5507 HolePunchingMessageUnion::ConnectionRequestDelivered(item)
5508 }
5509}
5510impl ::core::convert::From<ConnectionSync> for HolePunchingMessageUnion {
5511 fn from(item: ConnectionSync) -> Self {
5512 HolePunchingMessageUnion::ConnectionSync(item)
5513 }
5514}
5515impl<'r> ::core::convert::From<ConnectionRequestReader<'r>> for HolePunchingMessageUnionReader<'r> {
5516 fn from(item: ConnectionRequestReader<'r>) -> Self {
5517 HolePunchingMessageUnionReader::ConnectionRequest(item)
5518 }
5519}
5520impl<'r> ::core::convert::From<ConnectionRequestDeliveredReader<'r>>
5521 for HolePunchingMessageUnionReader<'r>
5522{
5523 fn from(item: ConnectionRequestDeliveredReader<'r>) -> Self {
5524 HolePunchingMessageUnionReader::ConnectionRequestDelivered(item)
5525 }
5526}
5527impl<'r> ::core::convert::From<ConnectionSyncReader<'r>> for HolePunchingMessageUnionReader<'r> {
5528 fn from(item: ConnectionSyncReader<'r>) -> Self {
5529 HolePunchingMessageUnionReader::ConnectionSync(item)
5530 }
5531}
5532impl HolePunchingMessageUnion {
5533 pub const NAME: &'static str = "HolePunchingMessageUnion";
5534 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
5535 match self {
5536 HolePunchingMessageUnion::ConnectionRequest(item) => item.as_bytes(),
5537 HolePunchingMessageUnion::ConnectionRequestDelivered(item) => item.as_bytes(),
5538 HolePunchingMessageUnion::ConnectionSync(item) => item.as_bytes(),
5539 }
5540 }
5541 pub fn as_slice(&self) -> &[u8] {
5542 match self {
5543 HolePunchingMessageUnion::ConnectionRequest(item) => item.as_slice(),
5544 HolePunchingMessageUnion::ConnectionRequestDelivered(item) => item.as_slice(),
5545 HolePunchingMessageUnion::ConnectionSync(item) => item.as_slice(),
5546 }
5547 }
5548 pub fn item_id(&self) -> molecule::Number {
5549 match self {
5550 HolePunchingMessageUnion::ConnectionRequest(_) => 0,
5551 HolePunchingMessageUnion::ConnectionRequestDelivered(_) => 1,
5552 HolePunchingMessageUnion::ConnectionSync(_) => 2,
5553 }
5554 }
5555 pub fn item_name(&self) -> &str {
5556 match self {
5557 HolePunchingMessageUnion::ConnectionRequest(_) => "ConnectionRequest",
5558 HolePunchingMessageUnion::ConnectionRequestDelivered(_) => "ConnectionRequestDelivered",
5559 HolePunchingMessageUnion::ConnectionSync(_) => "ConnectionSync",
5560 }
5561 }
5562 pub fn as_reader<'r>(&'r self) -> HolePunchingMessageUnionReader<'r> {
5563 match self {
5564 HolePunchingMessageUnion::ConnectionRequest(item) => item.as_reader().into(),
5565 HolePunchingMessageUnion::ConnectionRequestDelivered(item) => item.as_reader().into(),
5566 HolePunchingMessageUnion::ConnectionSync(item) => item.as_reader().into(),
5567 }
5568 }
5569}
5570impl<'r> HolePunchingMessageUnionReader<'r> {
5571 pub const NAME: &'r str = "HolePunchingMessageUnionReader";
5572 pub fn as_slice(&self) -> &'r [u8] {
5573 match self {
5574 HolePunchingMessageUnionReader::ConnectionRequest(item) => item.as_slice(),
5575 HolePunchingMessageUnionReader::ConnectionRequestDelivered(item) => item.as_slice(),
5576 HolePunchingMessageUnionReader::ConnectionSync(item) => item.as_slice(),
5577 }
5578 }
5579 pub fn item_id(&self) -> molecule::Number {
5580 match self {
5581 HolePunchingMessageUnionReader::ConnectionRequest(_) => 0,
5582 HolePunchingMessageUnionReader::ConnectionRequestDelivered(_) => 1,
5583 HolePunchingMessageUnionReader::ConnectionSync(_) => 2,
5584 }
5585 }
5586 pub fn item_name(&self) -> &str {
5587 match self {
5588 HolePunchingMessageUnionReader::ConnectionRequest(_) => "ConnectionRequest",
5589 HolePunchingMessageUnionReader::ConnectionRequestDelivered(_) => {
5590 "ConnectionRequestDelivered"
5591 }
5592 HolePunchingMessageUnionReader::ConnectionSync(_) => "ConnectionSync",
5593 }
5594 }
5595}
5596impl From<ConnectionRequest> for HolePunchingMessage {
5597 fn from(value: ConnectionRequest) -> Self {
5598 Self::new_builder().set(value).build()
5599 }
5600}
5601impl From<ConnectionRequestDelivered> for HolePunchingMessage {
5602 fn from(value: ConnectionRequestDelivered) -> Self {
5603 Self::new_builder().set(value).build()
5604 }
5605}
5606impl From<ConnectionSync> for HolePunchingMessage {
5607 fn from(value: ConnectionSync) -> Self {
5608 Self::new_builder().set(value).build()
5609 }
5610}
5611#[derive(Clone)]
5612pub struct ConnectionRequest(molecule::bytes::Bytes);
5613impl ::core::fmt::LowerHex for ConnectionRequest {
5614 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5615 use molecule::hex_string;
5616 if f.alternate() {
5617 write!(f, "0x")?;
5618 }
5619 write!(f, "{}", hex_string(self.as_slice()))
5620 }
5621}
5622impl ::core::fmt::Debug for ConnectionRequest {
5623 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5624 write!(f, "{}({:#x})", Self::NAME, self)
5625 }
5626}
5627impl ::core::fmt::Display for ConnectionRequest {
5628 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5629 write!(f, "{} {{ ", Self::NAME)?;
5630 write!(f, "{}: {}", "from", self.from())?;
5631 write!(f, ", {}: {}", "to", self.to())?;
5632 write!(f, ", {}: {}", "max_hops", self.max_hops())?;
5633 write!(f, ", {}: {}", "route", self.route())?;
5634 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
5635 let extra_count = self.count_extra_fields();
5636 if extra_count != 0 {
5637 write!(f, ", .. ({} fields)", extra_count)?;
5638 }
5639 write!(f, " }}")
5640 }
5641}
5642impl ::core::default::Default for ConnectionRequest {
5643 fn default() -> Self {
5644 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5645 ConnectionRequest::new_unchecked(v)
5646 }
5647}
5648impl ConnectionRequest {
5649 const DEFAULT_VALUE: [u8; 41] = [
5650 41, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 33, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0,
5651 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
5652 ];
5653 pub const FIELD_COUNT: usize = 5;
5654 pub fn total_size(&self) -> usize {
5655 molecule::unpack_number(self.as_slice()) as usize
5656 }
5657 pub fn field_count(&self) -> usize {
5658 if self.total_size() == molecule::NUMBER_SIZE {
5659 0
5660 } else {
5661 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5662 }
5663 }
5664 pub fn count_extra_fields(&self) -> usize {
5665 self.field_count() - Self::FIELD_COUNT
5666 }
5667 pub fn has_extra_fields(&self) -> bool {
5668 Self::FIELD_COUNT != self.field_count()
5669 }
5670 pub fn from(&self) -> Bytes {
5671 let slice = self.as_slice();
5672 let start = molecule::unpack_number(&slice[4..]) as usize;
5673 let end = molecule::unpack_number(&slice[8..]) as usize;
5674 Bytes::new_unchecked(self.0.slice(start..end))
5675 }
5676 pub fn to(&self) -> Bytes {
5677 let slice = self.as_slice();
5678 let start = molecule::unpack_number(&slice[8..]) as usize;
5679 let end = molecule::unpack_number(&slice[12..]) as usize;
5680 Bytes::new_unchecked(self.0.slice(start..end))
5681 }
5682 pub fn max_hops(&self) -> Byte {
5683 let slice = self.as_slice();
5684 let start = molecule::unpack_number(&slice[12..]) as usize;
5685 let end = molecule::unpack_number(&slice[16..]) as usize;
5686 Byte::new_unchecked(self.0.slice(start..end))
5687 }
5688 pub fn route(&self) -> BytesVec {
5689 let slice = self.as_slice();
5690 let start = molecule::unpack_number(&slice[16..]) as usize;
5691 let end = molecule::unpack_number(&slice[20..]) as usize;
5692 BytesVec::new_unchecked(self.0.slice(start..end))
5693 }
5694 pub fn listen_addrs(&self) -> AddressVec {
5695 let slice = self.as_slice();
5696 let start = molecule::unpack_number(&slice[20..]) as usize;
5697 if self.has_extra_fields() {
5698 let end = molecule::unpack_number(&slice[24..]) as usize;
5699 AddressVec::new_unchecked(self.0.slice(start..end))
5700 } else {
5701 AddressVec::new_unchecked(self.0.slice(start..))
5702 }
5703 }
5704 pub fn as_reader<'r>(&'r self) -> ConnectionRequestReader<'r> {
5705 ConnectionRequestReader::new_unchecked(self.as_slice())
5706 }
5707}
5708impl molecule::prelude::Entity for ConnectionRequest {
5709 type Builder = ConnectionRequestBuilder;
5710 const NAME: &'static str = "ConnectionRequest";
5711 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5712 ConnectionRequest(data)
5713 }
5714 fn as_bytes(&self) -> molecule::bytes::Bytes {
5715 self.0.clone()
5716 }
5717 fn as_slice(&self) -> &[u8] {
5718 &self.0[..]
5719 }
5720 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5721 ConnectionRequestReader::from_slice(slice).map(|reader| reader.to_entity())
5722 }
5723 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5724 ConnectionRequestReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5725 }
5726 fn new_builder() -> Self::Builder {
5727 ::core::default::Default::default()
5728 }
5729 fn as_builder(self) -> Self::Builder {
5730 Self::new_builder()
5731 .from(self.from())
5732 .to(self.to())
5733 .max_hops(self.max_hops())
5734 .route(self.route())
5735 .listen_addrs(self.listen_addrs())
5736 }
5737}
5738#[derive(Clone, Copy)]
5739pub struct ConnectionRequestReader<'r>(&'r [u8]);
5740impl<'r> ::core::fmt::LowerHex for ConnectionRequestReader<'r> {
5741 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5742 use molecule::hex_string;
5743 if f.alternate() {
5744 write!(f, "0x")?;
5745 }
5746 write!(f, "{}", hex_string(self.as_slice()))
5747 }
5748}
5749impl<'r> ::core::fmt::Debug for ConnectionRequestReader<'r> {
5750 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5751 write!(f, "{}({:#x})", Self::NAME, self)
5752 }
5753}
5754impl<'r> ::core::fmt::Display for ConnectionRequestReader<'r> {
5755 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5756 write!(f, "{} {{ ", Self::NAME)?;
5757 write!(f, "{}: {}", "from", self.from())?;
5758 write!(f, ", {}: {}", "to", self.to())?;
5759 write!(f, ", {}: {}", "max_hops", self.max_hops())?;
5760 write!(f, ", {}: {}", "route", self.route())?;
5761 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
5762 let extra_count = self.count_extra_fields();
5763 if extra_count != 0 {
5764 write!(f, ", .. ({} fields)", extra_count)?;
5765 }
5766 write!(f, " }}")
5767 }
5768}
5769impl<'r> ConnectionRequestReader<'r> {
5770 pub const FIELD_COUNT: usize = 5;
5771 pub fn total_size(&self) -> usize {
5772 molecule::unpack_number(self.as_slice()) as usize
5773 }
5774 pub fn field_count(&self) -> usize {
5775 if self.total_size() == molecule::NUMBER_SIZE {
5776 0
5777 } else {
5778 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5779 }
5780 }
5781 pub fn count_extra_fields(&self) -> usize {
5782 self.field_count() - Self::FIELD_COUNT
5783 }
5784 pub fn has_extra_fields(&self) -> bool {
5785 Self::FIELD_COUNT != self.field_count()
5786 }
5787 pub fn from(&self) -> BytesReader<'r> {
5788 let slice = self.as_slice();
5789 let start = molecule::unpack_number(&slice[4..]) as usize;
5790 let end = molecule::unpack_number(&slice[8..]) as usize;
5791 BytesReader::new_unchecked(&self.as_slice()[start..end])
5792 }
5793 pub fn to(&self) -> BytesReader<'r> {
5794 let slice = self.as_slice();
5795 let start = molecule::unpack_number(&slice[8..]) as usize;
5796 let end = molecule::unpack_number(&slice[12..]) as usize;
5797 BytesReader::new_unchecked(&self.as_slice()[start..end])
5798 }
5799 pub fn max_hops(&self) -> ByteReader<'r> {
5800 let slice = self.as_slice();
5801 let start = molecule::unpack_number(&slice[12..]) as usize;
5802 let end = molecule::unpack_number(&slice[16..]) as usize;
5803 ByteReader::new_unchecked(&self.as_slice()[start..end])
5804 }
5805 pub fn route(&self) -> BytesVecReader<'r> {
5806 let slice = self.as_slice();
5807 let start = molecule::unpack_number(&slice[16..]) as usize;
5808 let end = molecule::unpack_number(&slice[20..]) as usize;
5809 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
5810 }
5811 pub fn listen_addrs(&self) -> AddressVecReader<'r> {
5812 let slice = self.as_slice();
5813 let start = molecule::unpack_number(&slice[20..]) as usize;
5814 if self.has_extra_fields() {
5815 let end = molecule::unpack_number(&slice[24..]) as usize;
5816 AddressVecReader::new_unchecked(&self.as_slice()[start..end])
5817 } else {
5818 AddressVecReader::new_unchecked(&self.as_slice()[start..])
5819 }
5820 }
5821}
5822impl<'r> molecule::prelude::Reader<'r> for ConnectionRequestReader<'r> {
5823 type Entity = ConnectionRequest;
5824 const NAME: &'static str = "ConnectionRequestReader";
5825 fn to_entity(&self) -> Self::Entity {
5826 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5827 }
5828 fn new_unchecked(slice: &'r [u8]) -> Self {
5829 ConnectionRequestReader(slice)
5830 }
5831 fn as_slice(&self) -> &'r [u8] {
5832 self.0
5833 }
5834 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5835 use molecule::verification_error as ve;
5836 let slice_len = slice.len();
5837 if slice_len < molecule::NUMBER_SIZE {
5838 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5839 }
5840 let total_size = molecule::unpack_number(slice) as usize;
5841 if slice_len != total_size {
5842 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5843 }
5844 if slice_len < molecule::NUMBER_SIZE * 2 {
5845 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5846 }
5847 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5848 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5849 return ve!(Self, OffsetsNotMatch);
5850 }
5851 if slice_len < offset_first {
5852 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5853 }
5854 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5855 if field_count < Self::FIELD_COUNT {
5856 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5857 } else if !compatible && field_count > Self::FIELD_COUNT {
5858 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5859 };
5860 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5861 .chunks_exact(molecule::NUMBER_SIZE)
5862 .map(|x| molecule::unpack_number(x) as usize)
5863 .collect();
5864 offsets.push(total_size);
5865 if offsets.windows(2).any(|i| i[0] > i[1]) {
5866 return ve!(Self, OffsetsNotMatch);
5867 }
5868 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5869 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5870 ByteReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5871 BytesVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
5872 AddressVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
5873 Ok(())
5874 }
5875}
5876#[derive(Clone, Debug, Default)]
5877pub struct ConnectionRequestBuilder {
5878 pub(crate) from: Bytes,
5879 pub(crate) to: Bytes,
5880 pub(crate) max_hops: Byte,
5881 pub(crate) route: BytesVec,
5882 pub(crate) listen_addrs: AddressVec,
5883}
5884impl ConnectionRequestBuilder {
5885 pub const FIELD_COUNT: usize = 5;
5886 pub fn from<T>(mut self, v: T) -> Self
5887 where
5888 T: ::core::convert::Into<Bytes>,
5889 {
5890 self.from = v.into();
5891 self
5892 }
5893 pub fn to<T>(mut self, v: T) -> Self
5894 where
5895 T: ::core::convert::Into<Bytes>,
5896 {
5897 self.to = v.into();
5898 self
5899 }
5900 pub fn max_hops<T>(mut self, v: T) -> Self
5901 where
5902 T: ::core::convert::Into<Byte>,
5903 {
5904 self.max_hops = v.into();
5905 self
5906 }
5907 pub fn route<T>(mut self, v: T) -> Self
5908 where
5909 T: ::core::convert::Into<BytesVec>,
5910 {
5911 self.route = v.into();
5912 self
5913 }
5914 pub fn listen_addrs<T>(mut self, v: T) -> Self
5915 where
5916 T: ::core::convert::Into<AddressVec>,
5917 {
5918 self.listen_addrs = v.into();
5919 self
5920 }
5921}
5922impl molecule::prelude::Builder for ConnectionRequestBuilder {
5923 type Entity = ConnectionRequest;
5924 const NAME: &'static str = "ConnectionRequestBuilder";
5925 fn expected_length(&self) -> usize {
5926 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5927 + self.from.as_slice().len()
5928 + self.to.as_slice().len()
5929 + self.max_hops.as_slice().len()
5930 + self.route.as_slice().len()
5931 + self.listen_addrs.as_slice().len()
5932 }
5933 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5934 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5935 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5936 offsets.push(total_size);
5937 total_size += self.from.as_slice().len();
5938 offsets.push(total_size);
5939 total_size += self.to.as_slice().len();
5940 offsets.push(total_size);
5941 total_size += self.max_hops.as_slice().len();
5942 offsets.push(total_size);
5943 total_size += self.route.as_slice().len();
5944 offsets.push(total_size);
5945 total_size += self.listen_addrs.as_slice().len();
5946 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5947 for offset in offsets.into_iter() {
5948 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5949 }
5950 writer.write_all(self.from.as_slice())?;
5951 writer.write_all(self.to.as_slice())?;
5952 writer.write_all(self.max_hops.as_slice())?;
5953 writer.write_all(self.route.as_slice())?;
5954 writer.write_all(self.listen_addrs.as_slice())?;
5955 Ok(())
5956 }
5957 fn build(&self) -> Self::Entity {
5958 let mut inner = Vec::with_capacity(self.expected_length());
5959 self.write(&mut inner)
5960 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5961 ConnectionRequest::new_unchecked(inner.into())
5962 }
5963}
5964#[derive(Clone)]
5965pub struct ConnectionRequestDelivered(molecule::bytes::Bytes);
5966impl ::core::fmt::LowerHex for ConnectionRequestDelivered {
5967 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5968 use molecule::hex_string;
5969 if f.alternate() {
5970 write!(f, "0x")?;
5971 }
5972 write!(f, "{}", hex_string(self.as_slice()))
5973 }
5974}
5975impl ::core::fmt::Debug for ConnectionRequestDelivered {
5976 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5977 write!(f, "{}({:#x})", Self::NAME, self)
5978 }
5979}
5980impl ::core::fmt::Display for ConnectionRequestDelivered {
5981 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5982 write!(f, "{} {{ ", Self::NAME)?;
5983 write!(f, "{}: {}", "from", self.from())?;
5984 write!(f, ", {}: {}", "to", self.to())?;
5985 write!(f, ", {}: {}", "route", self.route())?;
5986 write!(f, ", {}: {}", "sync_route", self.sync_route())?;
5987 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
5988 let extra_count = self.count_extra_fields();
5989 if extra_count != 0 {
5990 write!(f, ", .. ({} fields)", extra_count)?;
5991 }
5992 write!(f, " }}")
5993 }
5994}
5995impl ::core::default::Default for ConnectionRequestDelivered {
5996 fn default() -> Self {
5997 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5998 ConnectionRequestDelivered::new_unchecked(v)
5999 }
6000}
6001impl ConnectionRequestDelivered {
6002 const DEFAULT_VALUE: [u8; 44] = [
6003 44, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0,
6004 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
6005 ];
6006 pub const FIELD_COUNT: usize = 5;
6007 pub fn total_size(&self) -> usize {
6008 molecule::unpack_number(self.as_slice()) as usize
6009 }
6010 pub fn field_count(&self) -> usize {
6011 if self.total_size() == molecule::NUMBER_SIZE {
6012 0
6013 } else {
6014 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6015 }
6016 }
6017 pub fn count_extra_fields(&self) -> usize {
6018 self.field_count() - Self::FIELD_COUNT
6019 }
6020 pub fn has_extra_fields(&self) -> bool {
6021 Self::FIELD_COUNT != self.field_count()
6022 }
6023 pub fn from(&self) -> Bytes {
6024 let slice = self.as_slice();
6025 let start = molecule::unpack_number(&slice[4..]) as usize;
6026 let end = molecule::unpack_number(&slice[8..]) as usize;
6027 Bytes::new_unchecked(self.0.slice(start..end))
6028 }
6029 pub fn to(&self) -> Bytes {
6030 let slice = self.as_slice();
6031 let start = molecule::unpack_number(&slice[8..]) as usize;
6032 let end = molecule::unpack_number(&slice[12..]) as usize;
6033 Bytes::new_unchecked(self.0.slice(start..end))
6034 }
6035 pub fn route(&self) -> BytesVec {
6036 let slice = self.as_slice();
6037 let start = molecule::unpack_number(&slice[12..]) as usize;
6038 let end = molecule::unpack_number(&slice[16..]) as usize;
6039 BytesVec::new_unchecked(self.0.slice(start..end))
6040 }
6041 pub fn sync_route(&self) -> BytesVec {
6042 let slice = self.as_slice();
6043 let start = molecule::unpack_number(&slice[16..]) as usize;
6044 let end = molecule::unpack_number(&slice[20..]) as usize;
6045 BytesVec::new_unchecked(self.0.slice(start..end))
6046 }
6047 pub fn listen_addrs(&self) -> AddressVec {
6048 let slice = self.as_slice();
6049 let start = molecule::unpack_number(&slice[20..]) as usize;
6050 if self.has_extra_fields() {
6051 let end = molecule::unpack_number(&slice[24..]) as usize;
6052 AddressVec::new_unchecked(self.0.slice(start..end))
6053 } else {
6054 AddressVec::new_unchecked(self.0.slice(start..))
6055 }
6056 }
6057 pub fn as_reader<'r>(&'r self) -> ConnectionRequestDeliveredReader<'r> {
6058 ConnectionRequestDeliveredReader::new_unchecked(self.as_slice())
6059 }
6060}
6061impl molecule::prelude::Entity for ConnectionRequestDelivered {
6062 type Builder = ConnectionRequestDeliveredBuilder;
6063 const NAME: &'static str = "ConnectionRequestDelivered";
6064 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6065 ConnectionRequestDelivered(data)
6066 }
6067 fn as_bytes(&self) -> molecule::bytes::Bytes {
6068 self.0.clone()
6069 }
6070 fn as_slice(&self) -> &[u8] {
6071 &self.0[..]
6072 }
6073 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6074 ConnectionRequestDeliveredReader::from_slice(slice).map(|reader| reader.to_entity())
6075 }
6076 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6077 ConnectionRequestDeliveredReader::from_compatible_slice(slice)
6078 .map(|reader| reader.to_entity())
6079 }
6080 fn new_builder() -> Self::Builder {
6081 ::core::default::Default::default()
6082 }
6083 fn as_builder(self) -> Self::Builder {
6084 Self::new_builder()
6085 .from(self.from())
6086 .to(self.to())
6087 .route(self.route())
6088 .sync_route(self.sync_route())
6089 .listen_addrs(self.listen_addrs())
6090 }
6091}
6092#[derive(Clone, Copy)]
6093pub struct ConnectionRequestDeliveredReader<'r>(&'r [u8]);
6094impl<'r> ::core::fmt::LowerHex for ConnectionRequestDeliveredReader<'r> {
6095 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6096 use molecule::hex_string;
6097 if f.alternate() {
6098 write!(f, "0x")?;
6099 }
6100 write!(f, "{}", hex_string(self.as_slice()))
6101 }
6102}
6103impl<'r> ::core::fmt::Debug for ConnectionRequestDeliveredReader<'r> {
6104 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6105 write!(f, "{}({:#x})", Self::NAME, self)
6106 }
6107}
6108impl<'r> ::core::fmt::Display for ConnectionRequestDeliveredReader<'r> {
6109 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6110 write!(f, "{} {{ ", Self::NAME)?;
6111 write!(f, "{}: {}", "from", self.from())?;
6112 write!(f, ", {}: {}", "to", self.to())?;
6113 write!(f, ", {}: {}", "route", self.route())?;
6114 write!(f, ", {}: {}", "sync_route", self.sync_route())?;
6115 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
6116 let extra_count = self.count_extra_fields();
6117 if extra_count != 0 {
6118 write!(f, ", .. ({} fields)", extra_count)?;
6119 }
6120 write!(f, " }}")
6121 }
6122}
6123impl<'r> ConnectionRequestDeliveredReader<'r> {
6124 pub const FIELD_COUNT: usize = 5;
6125 pub fn total_size(&self) -> usize {
6126 molecule::unpack_number(self.as_slice()) as usize
6127 }
6128 pub fn field_count(&self) -> usize {
6129 if self.total_size() == molecule::NUMBER_SIZE {
6130 0
6131 } else {
6132 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6133 }
6134 }
6135 pub fn count_extra_fields(&self) -> usize {
6136 self.field_count() - Self::FIELD_COUNT
6137 }
6138 pub fn has_extra_fields(&self) -> bool {
6139 Self::FIELD_COUNT != self.field_count()
6140 }
6141 pub fn from(&self) -> BytesReader<'r> {
6142 let slice = self.as_slice();
6143 let start = molecule::unpack_number(&slice[4..]) as usize;
6144 let end = molecule::unpack_number(&slice[8..]) as usize;
6145 BytesReader::new_unchecked(&self.as_slice()[start..end])
6146 }
6147 pub fn to(&self) -> BytesReader<'r> {
6148 let slice = self.as_slice();
6149 let start = molecule::unpack_number(&slice[8..]) as usize;
6150 let end = molecule::unpack_number(&slice[12..]) as usize;
6151 BytesReader::new_unchecked(&self.as_slice()[start..end])
6152 }
6153 pub fn route(&self) -> BytesVecReader<'r> {
6154 let slice = self.as_slice();
6155 let start = molecule::unpack_number(&slice[12..]) as usize;
6156 let end = molecule::unpack_number(&slice[16..]) as usize;
6157 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6158 }
6159 pub fn sync_route(&self) -> BytesVecReader<'r> {
6160 let slice = self.as_slice();
6161 let start = molecule::unpack_number(&slice[16..]) as usize;
6162 let end = molecule::unpack_number(&slice[20..]) as usize;
6163 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6164 }
6165 pub fn listen_addrs(&self) -> AddressVecReader<'r> {
6166 let slice = self.as_slice();
6167 let start = molecule::unpack_number(&slice[20..]) as usize;
6168 if self.has_extra_fields() {
6169 let end = molecule::unpack_number(&slice[24..]) as usize;
6170 AddressVecReader::new_unchecked(&self.as_slice()[start..end])
6171 } else {
6172 AddressVecReader::new_unchecked(&self.as_slice()[start..])
6173 }
6174 }
6175}
6176impl<'r> molecule::prelude::Reader<'r> for ConnectionRequestDeliveredReader<'r> {
6177 type Entity = ConnectionRequestDelivered;
6178 const NAME: &'static str = "ConnectionRequestDeliveredReader";
6179 fn to_entity(&self) -> Self::Entity {
6180 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6181 }
6182 fn new_unchecked(slice: &'r [u8]) -> Self {
6183 ConnectionRequestDeliveredReader(slice)
6184 }
6185 fn as_slice(&self) -> &'r [u8] {
6186 self.0
6187 }
6188 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6189 use molecule::verification_error as ve;
6190 let slice_len = slice.len();
6191 if slice_len < molecule::NUMBER_SIZE {
6192 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6193 }
6194 let total_size = molecule::unpack_number(slice) as usize;
6195 if slice_len != total_size {
6196 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6197 }
6198 if slice_len < molecule::NUMBER_SIZE * 2 {
6199 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6200 }
6201 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6202 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6203 return ve!(Self, OffsetsNotMatch);
6204 }
6205 if slice_len < offset_first {
6206 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6207 }
6208 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6209 if field_count < Self::FIELD_COUNT {
6210 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6211 } else if !compatible && field_count > Self::FIELD_COUNT {
6212 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6213 };
6214 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6215 .chunks_exact(molecule::NUMBER_SIZE)
6216 .map(|x| molecule::unpack_number(x) as usize)
6217 .collect();
6218 offsets.push(total_size);
6219 if offsets.windows(2).any(|i| i[0] > i[1]) {
6220 return ve!(Self, OffsetsNotMatch);
6221 }
6222 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6223 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6224 BytesVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6225 BytesVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
6226 AddressVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
6227 Ok(())
6228 }
6229}
6230#[derive(Clone, Debug, Default)]
6231pub struct ConnectionRequestDeliveredBuilder {
6232 pub(crate) from: Bytes,
6233 pub(crate) to: Bytes,
6234 pub(crate) route: BytesVec,
6235 pub(crate) sync_route: BytesVec,
6236 pub(crate) listen_addrs: AddressVec,
6237}
6238impl ConnectionRequestDeliveredBuilder {
6239 pub const FIELD_COUNT: usize = 5;
6240 pub fn from<T>(mut self, v: T) -> Self
6241 where
6242 T: ::core::convert::Into<Bytes>,
6243 {
6244 self.from = v.into();
6245 self
6246 }
6247 pub fn to<T>(mut self, v: T) -> Self
6248 where
6249 T: ::core::convert::Into<Bytes>,
6250 {
6251 self.to = v.into();
6252 self
6253 }
6254 pub fn route<T>(mut self, v: T) -> Self
6255 where
6256 T: ::core::convert::Into<BytesVec>,
6257 {
6258 self.route = v.into();
6259 self
6260 }
6261 pub fn sync_route<T>(mut self, v: T) -> Self
6262 where
6263 T: ::core::convert::Into<BytesVec>,
6264 {
6265 self.sync_route = v.into();
6266 self
6267 }
6268 pub fn listen_addrs<T>(mut self, v: T) -> Self
6269 where
6270 T: ::core::convert::Into<AddressVec>,
6271 {
6272 self.listen_addrs = v.into();
6273 self
6274 }
6275}
6276impl molecule::prelude::Builder for ConnectionRequestDeliveredBuilder {
6277 type Entity = ConnectionRequestDelivered;
6278 const NAME: &'static str = "ConnectionRequestDeliveredBuilder";
6279 fn expected_length(&self) -> usize {
6280 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6281 + self.from.as_slice().len()
6282 + self.to.as_slice().len()
6283 + self.route.as_slice().len()
6284 + self.sync_route.as_slice().len()
6285 + self.listen_addrs.as_slice().len()
6286 }
6287 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6288 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6289 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6290 offsets.push(total_size);
6291 total_size += self.from.as_slice().len();
6292 offsets.push(total_size);
6293 total_size += self.to.as_slice().len();
6294 offsets.push(total_size);
6295 total_size += self.route.as_slice().len();
6296 offsets.push(total_size);
6297 total_size += self.sync_route.as_slice().len();
6298 offsets.push(total_size);
6299 total_size += self.listen_addrs.as_slice().len();
6300 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6301 for offset in offsets.into_iter() {
6302 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6303 }
6304 writer.write_all(self.from.as_slice())?;
6305 writer.write_all(self.to.as_slice())?;
6306 writer.write_all(self.route.as_slice())?;
6307 writer.write_all(self.sync_route.as_slice())?;
6308 writer.write_all(self.listen_addrs.as_slice())?;
6309 Ok(())
6310 }
6311 fn build(&self) -> Self::Entity {
6312 let mut inner = Vec::with_capacity(self.expected_length());
6313 self.write(&mut inner)
6314 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6315 ConnectionRequestDelivered::new_unchecked(inner.into())
6316 }
6317}
6318#[derive(Clone)]
6319pub struct ConnectionSync(molecule::bytes::Bytes);
6320impl ::core::fmt::LowerHex for ConnectionSync {
6321 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6322 use molecule::hex_string;
6323 if f.alternate() {
6324 write!(f, "0x")?;
6325 }
6326 write!(f, "{}", hex_string(self.as_slice()))
6327 }
6328}
6329impl ::core::fmt::Debug for ConnectionSync {
6330 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6331 write!(f, "{}({:#x})", Self::NAME, self)
6332 }
6333}
6334impl ::core::fmt::Display for ConnectionSync {
6335 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6336 write!(f, "{} {{ ", Self::NAME)?;
6337 write!(f, "{}: {}", "from", self.from())?;
6338 write!(f, ", {}: {}", "to", self.to())?;
6339 write!(f, ", {}: {}", "route", self.route())?;
6340 let extra_count = self.count_extra_fields();
6341 if extra_count != 0 {
6342 write!(f, ", .. ({} fields)", extra_count)?;
6343 }
6344 write!(f, " }}")
6345 }
6346}
6347impl ::core::default::Default for ConnectionSync {
6348 fn default() -> Self {
6349 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6350 ConnectionSync::new_unchecked(v)
6351 }
6352}
6353impl ConnectionSync {
6354 const DEFAULT_VALUE: [u8; 28] = [
6355 28, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
6356 ];
6357 pub const FIELD_COUNT: usize = 3;
6358 pub fn total_size(&self) -> usize {
6359 molecule::unpack_number(self.as_slice()) as usize
6360 }
6361 pub fn field_count(&self) -> usize {
6362 if self.total_size() == molecule::NUMBER_SIZE {
6363 0
6364 } else {
6365 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6366 }
6367 }
6368 pub fn count_extra_fields(&self) -> usize {
6369 self.field_count() - Self::FIELD_COUNT
6370 }
6371 pub fn has_extra_fields(&self) -> bool {
6372 Self::FIELD_COUNT != self.field_count()
6373 }
6374 pub fn from(&self) -> Bytes {
6375 let slice = self.as_slice();
6376 let start = molecule::unpack_number(&slice[4..]) as usize;
6377 let end = molecule::unpack_number(&slice[8..]) as usize;
6378 Bytes::new_unchecked(self.0.slice(start..end))
6379 }
6380 pub fn to(&self) -> Bytes {
6381 let slice = self.as_slice();
6382 let start = molecule::unpack_number(&slice[8..]) as usize;
6383 let end = molecule::unpack_number(&slice[12..]) as usize;
6384 Bytes::new_unchecked(self.0.slice(start..end))
6385 }
6386 pub fn route(&self) -> BytesVec {
6387 let slice = self.as_slice();
6388 let start = molecule::unpack_number(&slice[12..]) as usize;
6389 if self.has_extra_fields() {
6390 let end = molecule::unpack_number(&slice[16..]) as usize;
6391 BytesVec::new_unchecked(self.0.slice(start..end))
6392 } else {
6393 BytesVec::new_unchecked(self.0.slice(start..))
6394 }
6395 }
6396 pub fn as_reader<'r>(&'r self) -> ConnectionSyncReader<'r> {
6397 ConnectionSyncReader::new_unchecked(self.as_slice())
6398 }
6399}
6400impl molecule::prelude::Entity for ConnectionSync {
6401 type Builder = ConnectionSyncBuilder;
6402 const NAME: &'static str = "ConnectionSync";
6403 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6404 ConnectionSync(data)
6405 }
6406 fn as_bytes(&self) -> molecule::bytes::Bytes {
6407 self.0.clone()
6408 }
6409 fn as_slice(&self) -> &[u8] {
6410 &self.0[..]
6411 }
6412 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6413 ConnectionSyncReader::from_slice(slice).map(|reader| reader.to_entity())
6414 }
6415 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6416 ConnectionSyncReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6417 }
6418 fn new_builder() -> Self::Builder {
6419 ::core::default::Default::default()
6420 }
6421 fn as_builder(self) -> Self::Builder {
6422 Self::new_builder()
6423 .from(self.from())
6424 .to(self.to())
6425 .route(self.route())
6426 }
6427}
6428#[derive(Clone, Copy)]
6429pub struct ConnectionSyncReader<'r>(&'r [u8]);
6430impl<'r> ::core::fmt::LowerHex for ConnectionSyncReader<'r> {
6431 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6432 use molecule::hex_string;
6433 if f.alternate() {
6434 write!(f, "0x")?;
6435 }
6436 write!(f, "{}", hex_string(self.as_slice()))
6437 }
6438}
6439impl<'r> ::core::fmt::Debug for ConnectionSyncReader<'r> {
6440 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6441 write!(f, "{}({:#x})", Self::NAME, self)
6442 }
6443}
6444impl<'r> ::core::fmt::Display for ConnectionSyncReader<'r> {
6445 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6446 write!(f, "{} {{ ", Self::NAME)?;
6447 write!(f, "{}: {}", "from", self.from())?;
6448 write!(f, ", {}: {}", "to", self.to())?;
6449 write!(f, ", {}: {}", "route", self.route())?;
6450 let extra_count = self.count_extra_fields();
6451 if extra_count != 0 {
6452 write!(f, ", .. ({} fields)", extra_count)?;
6453 }
6454 write!(f, " }}")
6455 }
6456}
6457impl<'r> ConnectionSyncReader<'r> {
6458 pub const FIELD_COUNT: usize = 3;
6459 pub fn total_size(&self) -> usize {
6460 molecule::unpack_number(self.as_slice()) as usize
6461 }
6462 pub fn field_count(&self) -> usize {
6463 if self.total_size() == molecule::NUMBER_SIZE {
6464 0
6465 } else {
6466 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6467 }
6468 }
6469 pub fn count_extra_fields(&self) -> usize {
6470 self.field_count() - Self::FIELD_COUNT
6471 }
6472 pub fn has_extra_fields(&self) -> bool {
6473 Self::FIELD_COUNT != self.field_count()
6474 }
6475 pub fn from(&self) -> BytesReader<'r> {
6476 let slice = self.as_slice();
6477 let start = molecule::unpack_number(&slice[4..]) as usize;
6478 let end = molecule::unpack_number(&slice[8..]) as usize;
6479 BytesReader::new_unchecked(&self.as_slice()[start..end])
6480 }
6481 pub fn to(&self) -> BytesReader<'r> {
6482 let slice = self.as_slice();
6483 let start = molecule::unpack_number(&slice[8..]) as usize;
6484 let end = molecule::unpack_number(&slice[12..]) as usize;
6485 BytesReader::new_unchecked(&self.as_slice()[start..end])
6486 }
6487 pub fn route(&self) -> BytesVecReader<'r> {
6488 let slice = self.as_slice();
6489 let start = molecule::unpack_number(&slice[12..]) as usize;
6490 if self.has_extra_fields() {
6491 let end = molecule::unpack_number(&slice[16..]) as usize;
6492 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6493 } else {
6494 BytesVecReader::new_unchecked(&self.as_slice()[start..])
6495 }
6496 }
6497}
6498impl<'r> molecule::prelude::Reader<'r> for ConnectionSyncReader<'r> {
6499 type Entity = ConnectionSync;
6500 const NAME: &'static str = "ConnectionSyncReader";
6501 fn to_entity(&self) -> Self::Entity {
6502 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6503 }
6504 fn new_unchecked(slice: &'r [u8]) -> Self {
6505 ConnectionSyncReader(slice)
6506 }
6507 fn as_slice(&self) -> &'r [u8] {
6508 self.0
6509 }
6510 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6511 use molecule::verification_error as ve;
6512 let slice_len = slice.len();
6513 if slice_len < molecule::NUMBER_SIZE {
6514 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6515 }
6516 let total_size = molecule::unpack_number(slice) as usize;
6517 if slice_len != total_size {
6518 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6519 }
6520 if slice_len < molecule::NUMBER_SIZE * 2 {
6521 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6522 }
6523 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6524 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6525 return ve!(Self, OffsetsNotMatch);
6526 }
6527 if slice_len < offset_first {
6528 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6529 }
6530 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6531 if field_count < Self::FIELD_COUNT {
6532 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6533 } else if !compatible && field_count > Self::FIELD_COUNT {
6534 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6535 };
6536 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6537 .chunks_exact(molecule::NUMBER_SIZE)
6538 .map(|x| molecule::unpack_number(x) as usize)
6539 .collect();
6540 offsets.push(total_size);
6541 if offsets.windows(2).any(|i| i[0] > i[1]) {
6542 return ve!(Self, OffsetsNotMatch);
6543 }
6544 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6545 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6546 BytesVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6547 Ok(())
6548 }
6549}
6550#[derive(Clone, Debug, Default)]
6551pub struct ConnectionSyncBuilder {
6552 pub(crate) from: Bytes,
6553 pub(crate) to: Bytes,
6554 pub(crate) route: BytesVec,
6555}
6556impl ConnectionSyncBuilder {
6557 pub const FIELD_COUNT: usize = 3;
6558 pub fn from<T>(mut self, v: T) -> Self
6559 where
6560 T: ::core::convert::Into<Bytes>,
6561 {
6562 self.from = v.into();
6563 self
6564 }
6565 pub fn to<T>(mut self, v: T) -> Self
6566 where
6567 T: ::core::convert::Into<Bytes>,
6568 {
6569 self.to = v.into();
6570 self
6571 }
6572 pub fn route<T>(mut self, v: T) -> Self
6573 where
6574 T: ::core::convert::Into<BytesVec>,
6575 {
6576 self.route = v.into();
6577 self
6578 }
6579}
6580impl molecule::prelude::Builder for ConnectionSyncBuilder {
6581 type Entity = ConnectionSync;
6582 const NAME: &'static str = "ConnectionSyncBuilder";
6583 fn expected_length(&self) -> usize {
6584 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6585 + self.from.as_slice().len()
6586 + self.to.as_slice().len()
6587 + self.route.as_slice().len()
6588 }
6589 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6590 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6591 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6592 offsets.push(total_size);
6593 total_size += self.from.as_slice().len();
6594 offsets.push(total_size);
6595 total_size += self.to.as_slice().len();
6596 offsets.push(total_size);
6597 total_size += self.route.as_slice().len();
6598 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6599 for offset in offsets.into_iter() {
6600 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6601 }
6602 writer.write_all(self.from.as_slice())?;
6603 writer.write_all(self.to.as_slice())?;
6604 writer.write_all(self.route.as_slice())?;
6605 Ok(())
6606 }
6607 fn build(&self) -> Self::Entity {
6608 let mut inner = Vec::with_capacity(self.expected_length());
6609 self.write(&mut inner)
6610 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6611 ConnectionSync::new_unchecked(inner.into())
6612 }
6613}