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 ::core::iter::FromIterator<Node> for NodeVec {
1366 fn from_iter<T: IntoIterator<Item = Node>>(iter: T) -> Self {
1367 Self::new_builder().extend(iter).build()
1368 }
1369}
1370impl From<Vec<Node>> for NodeVec {
1371 fn from(v: Vec<Node>) -> Self {
1372 Self::new_builder().set(v).build()
1373 }
1374}
1375#[derive(Clone)]
1376pub struct Node2Vec(molecule::bytes::Bytes);
1377impl ::core::fmt::LowerHex for Node2Vec {
1378 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1379 use molecule::hex_string;
1380 if f.alternate() {
1381 write!(f, "0x")?;
1382 }
1383 write!(f, "{}", hex_string(self.as_slice()))
1384 }
1385}
1386impl ::core::fmt::Debug for Node2Vec {
1387 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1388 write!(f, "{}({:#x})", Self::NAME, self)
1389 }
1390}
1391impl ::core::fmt::Display for Node2Vec {
1392 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1393 write!(f, "{} [", Self::NAME)?;
1394 for i in 0..self.len() {
1395 if i == 0 {
1396 write!(f, "{}", self.get_unchecked(i))?;
1397 } else {
1398 write!(f, ", {}", self.get_unchecked(i))?;
1399 }
1400 }
1401 write!(f, "]")
1402 }
1403}
1404impl ::core::default::Default for Node2Vec {
1405 fn default() -> Self {
1406 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1407 Node2Vec::new_unchecked(v)
1408 }
1409}
1410impl Node2Vec {
1411 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
1412 pub fn total_size(&self) -> usize {
1413 molecule::unpack_number(self.as_slice()) as usize
1414 }
1415 pub fn item_count(&self) -> usize {
1416 if self.total_size() == molecule::NUMBER_SIZE {
1417 0
1418 } else {
1419 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1420 }
1421 }
1422 pub fn len(&self) -> usize {
1423 self.item_count()
1424 }
1425 pub fn is_empty(&self) -> bool {
1426 self.len() == 0
1427 }
1428 pub fn get(&self, idx: usize) -> Option<Node2> {
1429 if idx >= self.len() {
1430 None
1431 } else {
1432 Some(self.get_unchecked(idx))
1433 }
1434 }
1435 pub fn get_unchecked(&self, idx: usize) -> Node2 {
1436 let slice = self.as_slice();
1437 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
1438 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
1439 if idx == self.len() - 1 {
1440 Node2::new_unchecked(self.0.slice(start..))
1441 } else {
1442 let end_idx = start_idx + molecule::NUMBER_SIZE;
1443 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
1444 Node2::new_unchecked(self.0.slice(start..end))
1445 }
1446 }
1447 pub fn as_reader<'r>(&'r self) -> Node2VecReader<'r> {
1448 Node2VecReader::new_unchecked(self.as_slice())
1449 }
1450}
1451impl molecule::prelude::Entity for Node2Vec {
1452 type Builder = Node2VecBuilder;
1453 const NAME: &'static str = "Node2Vec";
1454 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1455 Node2Vec(data)
1456 }
1457 fn as_bytes(&self) -> molecule::bytes::Bytes {
1458 self.0.clone()
1459 }
1460 fn as_slice(&self) -> &[u8] {
1461 &self.0[..]
1462 }
1463 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1464 Node2VecReader::from_slice(slice).map(|reader| reader.to_entity())
1465 }
1466 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1467 Node2VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1468 }
1469 fn new_builder() -> Self::Builder {
1470 ::core::default::Default::default()
1471 }
1472 fn as_builder(self) -> Self::Builder {
1473 Self::new_builder().extend(self.into_iter())
1474 }
1475}
1476#[derive(Clone, Copy)]
1477pub struct Node2VecReader<'r>(&'r [u8]);
1478impl<'r> ::core::fmt::LowerHex for Node2VecReader<'r> {
1479 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1480 use molecule::hex_string;
1481 if f.alternate() {
1482 write!(f, "0x")?;
1483 }
1484 write!(f, "{}", hex_string(self.as_slice()))
1485 }
1486}
1487impl<'r> ::core::fmt::Debug for Node2VecReader<'r> {
1488 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1489 write!(f, "{}({:#x})", Self::NAME, self)
1490 }
1491}
1492impl<'r> ::core::fmt::Display for Node2VecReader<'r> {
1493 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1494 write!(f, "{} [", Self::NAME)?;
1495 for i in 0..self.len() {
1496 if i == 0 {
1497 write!(f, "{}", self.get_unchecked(i))?;
1498 } else {
1499 write!(f, ", {}", self.get_unchecked(i))?;
1500 }
1501 }
1502 write!(f, "]")
1503 }
1504}
1505impl<'r> Node2VecReader<'r> {
1506 pub fn total_size(&self) -> usize {
1507 molecule::unpack_number(self.as_slice()) as usize
1508 }
1509 pub fn item_count(&self) -> usize {
1510 if self.total_size() == molecule::NUMBER_SIZE {
1511 0
1512 } else {
1513 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1514 }
1515 }
1516 pub fn len(&self) -> usize {
1517 self.item_count()
1518 }
1519 pub fn is_empty(&self) -> bool {
1520 self.len() == 0
1521 }
1522 pub fn get(&self, idx: usize) -> Option<Node2Reader<'r>> {
1523 if idx >= self.len() {
1524 None
1525 } else {
1526 Some(self.get_unchecked(idx))
1527 }
1528 }
1529 pub fn get_unchecked(&self, idx: usize) -> Node2Reader<'r> {
1530 let slice = self.as_slice();
1531 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
1532 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
1533 if idx == self.len() - 1 {
1534 Node2Reader::new_unchecked(&self.as_slice()[start..])
1535 } else {
1536 let end_idx = start_idx + molecule::NUMBER_SIZE;
1537 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
1538 Node2Reader::new_unchecked(&self.as_slice()[start..end])
1539 }
1540 }
1541}
1542impl<'r> molecule::prelude::Reader<'r> for Node2VecReader<'r> {
1543 type Entity = Node2Vec;
1544 const NAME: &'static str = "Node2VecReader";
1545 fn to_entity(&self) -> Self::Entity {
1546 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1547 }
1548 fn new_unchecked(slice: &'r [u8]) -> Self {
1549 Node2VecReader(slice)
1550 }
1551 fn as_slice(&self) -> &'r [u8] {
1552 self.0
1553 }
1554 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
1555 use molecule::verification_error as ve;
1556 let slice_len = slice.len();
1557 if slice_len < molecule::NUMBER_SIZE {
1558 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1559 }
1560 let total_size = molecule::unpack_number(slice) as usize;
1561 if slice_len != total_size {
1562 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1563 }
1564 if slice_len == molecule::NUMBER_SIZE {
1565 return Ok(());
1566 }
1567 if slice_len < molecule::NUMBER_SIZE * 2 {
1568 return ve!(
1569 Self,
1570 TotalSizeNotMatch,
1571 molecule::NUMBER_SIZE * 2,
1572 slice_len
1573 );
1574 }
1575 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
1576 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
1577 return ve!(Self, OffsetsNotMatch);
1578 }
1579 if slice_len < offset_first {
1580 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
1581 }
1582 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
1583 .chunks_exact(molecule::NUMBER_SIZE)
1584 .map(|x| molecule::unpack_number(x) as usize)
1585 .collect();
1586 offsets.push(total_size);
1587 if offsets.windows(2).any(|i| i[0] > i[1]) {
1588 return ve!(Self, OffsetsNotMatch);
1589 }
1590 for pair in offsets.windows(2) {
1591 let start = pair[0];
1592 let end = pair[1];
1593 Node2Reader::verify(&slice[start..end], compatible)?;
1594 }
1595 Ok(())
1596 }
1597}
1598#[derive(Clone, Debug, Default)]
1599pub struct Node2VecBuilder(pub(crate) Vec<Node2>);
1600impl Node2VecBuilder {
1601 pub fn set(mut self, v: Vec<Node2>) -> Self {
1602 self.0 = v;
1603 self
1604 }
1605 pub fn push<T>(mut self, v: T) -> Self
1606 where
1607 T: ::core::convert::Into<Node2>,
1608 {
1609 self.0.push(v.into());
1610 self
1611 }
1612 pub fn extend<T: ::core::iter::IntoIterator<Item = Node2>>(mut self, iter: T) -> Self {
1613 self.0.extend(iter);
1614 self
1615 }
1616 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Node2>
1617 where
1618 T: ::core::convert::Into<Node2>,
1619 {
1620 self.0
1621 .get_mut(index)
1622 .map(|item| ::core::mem::replace(item, v.into()))
1623 }
1624}
1625impl molecule::prelude::Builder for Node2VecBuilder {
1626 type Entity = Node2Vec;
1627 const NAME: &'static str = "Node2VecBuilder";
1628 fn expected_length(&self) -> usize {
1629 molecule::NUMBER_SIZE * (self.0.len() + 1)
1630 + self
1631 .0
1632 .iter()
1633 .map(|inner| inner.as_slice().len())
1634 .sum::<usize>()
1635 }
1636 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1637 let item_count = self.0.len();
1638 if item_count == 0 {
1639 writer.write_all(&molecule::pack_number(
1640 molecule::NUMBER_SIZE as molecule::Number,
1641 ))?;
1642 } else {
1643 let (total_size, offsets) = self.0.iter().fold(
1644 (
1645 molecule::NUMBER_SIZE * (item_count + 1),
1646 Vec::with_capacity(item_count),
1647 ),
1648 |(start, mut offsets), inner| {
1649 offsets.push(start);
1650 (start + inner.as_slice().len(), offsets)
1651 },
1652 );
1653 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
1654 for offset in offsets.into_iter() {
1655 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
1656 }
1657 for inner in self.0.iter() {
1658 writer.write_all(inner.as_slice())?;
1659 }
1660 }
1661 Ok(())
1662 }
1663 fn build(&self) -> Self::Entity {
1664 let mut inner = Vec::with_capacity(self.expected_length());
1665 self.write(&mut inner)
1666 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1667 Node2Vec::new_unchecked(inner.into())
1668 }
1669}
1670pub struct Node2VecIterator(Node2Vec, usize, usize);
1671impl ::core::iter::Iterator for Node2VecIterator {
1672 type Item = Node2;
1673 fn next(&mut self) -> Option<Self::Item> {
1674 if self.1 >= self.2 {
1675 None
1676 } else {
1677 let ret = self.0.get_unchecked(self.1);
1678 self.1 += 1;
1679 Some(ret)
1680 }
1681 }
1682}
1683impl ::core::iter::ExactSizeIterator for Node2VecIterator {
1684 fn len(&self) -> usize {
1685 self.2 - self.1
1686 }
1687}
1688impl ::core::iter::IntoIterator for Node2Vec {
1689 type Item = Node2;
1690 type IntoIter = Node2VecIterator;
1691 fn into_iter(self) -> Self::IntoIter {
1692 let len = self.len();
1693 Node2VecIterator(self, 0, len)
1694 }
1695}
1696impl<'r> Node2VecReader<'r> {
1697 pub fn iter<'t>(&'t self) -> Node2VecReaderIterator<'t, 'r> {
1698 Node2VecReaderIterator(&self, 0, self.len())
1699 }
1700}
1701pub struct Node2VecReaderIterator<'t, 'r>(&'t Node2VecReader<'r>, usize, usize);
1702impl<'t: 'r, 'r> ::core::iter::Iterator for Node2VecReaderIterator<'t, 'r> {
1703 type Item = Node2Reader<'t>;
1704 fn next(&mut self) -> Option<Self::Item> {
1705 if self.1 >= self.2 {
1706 None
1707 } else {
1708 let ret = self.0.get_unchecked(self.1);
1709 self.1 += 1;
1710 Some(ret)
1711 }
1712 }
1713}
1714impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Node2VecReaderIterator<'t, 'r> {
1715 fn len(&self) -> usize {
1716 self.2 - self.1
1717 }
1718}
1719impl ::core::iter::FromIterator<Node2> for Node2Vec {
1720 fn from_iter<T: IntoIterator<Item = Node2>>(iter: T) -> Self {
1721 Self::new_builder().extend(iter).build()
1722 }
1723}
1724impl From<Vec<Node2>> for Node2Vec {
1725 fn from(v: Vec<Node2>) -> Self {
1726 Self::new_builder().set(v).build()
1727 }
1728}
1729#[derive(Clone)]
1730pub struct Uint16(molecule::bytes::Bytes);
1731impl ::core::fmt::LowerHex for Uint16 {
1732 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1733 use molecule::hex_string;
1734 if f.alternate() {
1735 write!(f, "0x")?;
1736 }
1737 write!(f, "{}", hex_string(self.as_slice()))
1738 }
1739}
1740impl ::core::fmt::Debug for Uint16 {
1741 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1742 write!(f, "{}({:#x})", Self::NAME, self)
1743 }
1744}
1745impl ::core::fmt::Display for Uint16 {
1746 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1747 use molecule::hex_string;
1748 let raw_data = hex_string(&self.raw_data());
1749 write!(f, "{}(0x{})", Self::NAME, raw_data)
1750 }
1751}
1752impl ::core::default::Default for Uint16 {
1753 fn default() -> Self {
1754 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1755 Uint16::new_unchecked(v)
1756 }
1757}
1758impl Uint16 {
1759 const DEFAULT_VALUE: [u8; 2] = [0, 0];
1760 pub const TOTAL_SIZE: usize = 2;
1761 pub const ITEM_SIZE: usize = 1;
1762 pub const ITEM_COUNT: usize = 2;
1763 pub fn nth0(&self) -> Byte {
1764 Byte::new_unchecked(self.0.slice(0..1))
1765 }
1766 pub fn nth1(&self) -> Byte {
1767 Byte::new_unchecked(self.0.slice(1..2))
1768 }
1769 pub fn raw_data(&self) -> molecule::bytes::Bytes {
1770 self.as_bytes()
1771 }
1772 pub fn as_reader<'r>(&'r self) -> Uint16Reader<'r> {
1773 Uint16Reader::new_unchecked(self.as_slice())
1774 }
1775}
1776impl molecule::prelude::Entity for Uint16 {
1777 type Builder = Uint16Builder;
1778 const NAME: &'static str = "Uint16";
1779 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1780 Uint16(data)
1781 }
1782 fn as_bytes(&self) -> molecule::bytes::Bytes {
1783 self.0.clone()
1784 }
1785 fn as_slice(&self) -> &[u8] {
1786 &self.0[..]
1787 }
1788 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1789 Uint16Reader::from_slice(slice).map(|reader| reader.to_entity())
1790 }
1791 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1792 Uint16Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1793 }
1794 fn new_builder() -> Self::Builder {
1795 ::core::default::Default::default()
1796 }
1797 fn as_builder(self) -> Self::Builder {
1798 Self::new_builder().set([self.nth0(), self.nth1()])
1799 }
1800}
1801#[derive(Clone, Copy)]
1802pub struct Uint16Reader<'r>(&'r [u8]);
1803impl<'r> ::core::fmt::LowerHex for Uint16Reader<'r> {
1804 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1805 use molecule::hex_string;
1806 if f.alternate() {
1807 write!(f, "0x")?;
1808 }
1809 write!(f, "{}", hex_string(self.as_slice()))
1810 }
1811}
1812impl<'r> ::core::fmt::Debug for Uint16Reader<'r> {
1813 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1814 write!(f, "{}({:#x})", Self::NAME, self)
1815 }
1816}
1817impl<'r> ::core::fmt::Display for Uint16Reader<'r> {
1818 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1819 use molecule::hex_string;
1820 let raw_data = hex_string(&self.raw_data());
1821 write!(f, "{}(0x{})", Self::NAME, raw_data)
1822 }
1823}
1824impl<'r> Uint16Reader<'r> {
1825 pub const TOTAL_SIZE: usize = 2;
1826 pub const ITEM_SIZE: usize = 1;
1827 pub const ITEM_COUNT: usize = 2;
1828 pub fn nth0(&self) -> ByteReader<'r> {
1829 ByteReader::new_unchecked(&self.as_slice()[0..1])
1830 }
1831 pub fn nth1(&self) -> ByteReader<'r> {
1832 ByteReader::new_unchecked(&self.as_slice()[1..2])
1833 }
1834 pub fn raw_data(&self) -> &'r [u8] {
1835 self.as_slice()
1836 }
1837}
1838impl<'r> molecule::prelude::Reader<'r> for Uint16Reader<'r> {
1839 type Entity = Uint16;
1840 const NAME: &'static str = "Uint16Reader";
1841 fn to_entity(&self) -> Self::Entity {
1842 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1843 }
1844 fn new_unchecked(slice: &'r [u8]) -> Self {
1845 Uint16Reader(slice)
1846 }
1847 fn as_slice(&self) -> &'r [u8] {
1848 self.0
1849 }
1850 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1851 use molecule::verification_error as ve;
1852 let slice_len = slice.len();
1853 if slice_len != Self::TOTAL_SIZE {
1854 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
1855 }
1856 Ok(())
1857 }
1858}
1859#[derive(Clone)]
1860pub struct Uint16Builder(pub(crate) [Byte; 2]);
1861impl ::core::fmt::Debug for Uint16Builder {
1862 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1863 write!(f, "{}({:?})", Self::NAME, &self.0[..])
1864 }
1865}
1866impl ::core::default::Default for Uint16Builder {
1867 fn default() -> Self {
1868 Uint16Builder([Byte::default(), Byte::default()])
1869 }
1870}
1871impl Uint16Builder {
1872 pub const TOTAL_SIZE: usize = 2;
1873 pub const ITEM_SIZE: usize = 1;
1874 pub const ITEM_COUNT: usize = 2;
1875 pub fn set<T>(mut self, v: T) -> Self
1876 where
1877 T: ::core::convert::Into<[Byte; 2]>,
1878 {
1879 self.0 = v.into();
1880 self
1881 }
1882 pub fn nth0<T>(mut self, v: T) -> Self
1883 where
1884 T: ::core::convert::Into<Byte>,
1885 {
1886 self.0[0] = v.into();
1887 self
1888 }
1889 pub fn nth1<T>(mut self, v: T) -> Self
1890 where
1891 T: ::core::convert::Into<Byte>,
1892 {
1893 self.0[1] = v.into();
1894 self
1895 }
1896}
1897impl molecule::prelude::Builder for Uint16Builder {
1898 type Entity = Uint16;
1899 const NAME: &'static str = "Uint16Builder";
1900 fn expected_length(&self) -> usize {
1901 Self::TOTAL_SIZE
1902 }
1903 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1904 writer.write_all(self.0[0].as_slice())?;
1905 writer.write_all(self.0[1].as_slice())?;
1906 Ok(())
1907 }
1908 fn build(&self) -> Self::Entity {
1909 let mut inner = Vec::with_capacity(self.expected_length());
1910 self.write(&mut inner)
1911 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1912 Uint16::new_unchecked(inner.into())
1913 }
1914}
1915impl From<[Byte; 2usize]> for Uint16 {
1916 fn from(value: [Byte; 2usize]) -> Self {
1917 Self::new_builder().set(value).build()
1918 }
1919}
1920impl ::core::convert::TryFrom<&[Byte]> for Uint16 {
1921 type Error = ::core::array::TryFromSliceError;
1922 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
1923 Ok(Self::new_builder()
1924 .set(<&[Byte; 2usize]>::try_from(value)?.clone())
1925 .build())
1926 }
1927}
1928impl From<Uint16> for [Byte; 2usize] {
1929 #[track_caller]
1930 fn from(value: Uint16) -> Self {
1931 [value.nth0(), value.nth1()]
1932 }
1933}
1934impl From<[u8; 2usize]> for Uint16 {
1935 fn from(value: [u8; 2usize]) -> Self {
1936 Uint16Reader::new_unchecked(&value).to_entity()
1937 }
1938}
1939impl ::core::convert::TryFrom<&[u8]> for Uint16 {
1940 type Error = ::core::array::TryFromSliceError;
1941 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
1942 Ok(<[u8; 2usize]>::try_from(value)?.into())
1943 }
1944}
1945impl From<Uint16> for [u8; 2usize] {
1946 #[track_caller]
1947 fn from(value: Uint16) -> Self {
1948 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1949 }
1950}
1951impl<'a> From<Uint16Reader<'a>> for &'a [u8; 2usize] {
1952 #[track_caller]
1953 fn from(value: Uint16Reader<'a>) -> Self {
1954 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1955 }
1956}
1957impl<'a> From<&'a Uint16Reader<'a>> for &'a [u8; 2usize] {
1958 #[track_caller]
1959 fn from(value: &'a Uint16Reader<'a>) -> Self {
1960 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1961 }
1962}
1963#[derive(Clone)]
1964pub struct PortOpt(molecule::bytes::Bytes);
1965impl ::core::fmt::LowerHex for PortOpt {
1966 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1967 use molecule::hex_string;
1968 if f.alternate() {
1969 write!(f, "0x")?;
1970 }
1971 write!(f, "{}", hex_string(self.as_slice()))
1972 }
1973}
1974impl ::core::fmt::Debug for PortOpt {
1975 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1976 write!(f, "{}({:#x})", Self::NAME, self)
1977 }
1978}
1979impl ::core::fmt::Display for PortOpt {
1980 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1981 if let Some(v) = self.to_opt() {
1982 write!(f, "{}(Some({}))", Self::NAME, v)
1983 } else {
1984 write!(f, "{}(None)", Self::NAME)
1985 }
1986 }
1987}
1988impl ::core::default::Default for PortOpt {
1989 fn default() -> Self {
1990 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1991 PortOpt::new_unchecked(v)
1992 }
1993}
1994impl PortOpt {
1995 const DEFAULT_VALUE: [u8; 0] = [];
1996 pub fn is_none(&self) -> bool {
1997 self.0.is_empty()
1998 }
1999 pub fn is_some(&self) -> bool {
2000 !self.0.is_empty()
2001 }
2002 pub fn to_opt(&self) -> Option<Uint16> {
2003 if self.is_none() {
2004 None
2005 } else {
2006 Some(Uint16::new_unchecked(self.0.clone()))
2007 }
2008 }
2009 pub fn as_reader<'r>(&'r self) -> PortOptReader<'r> {
2010 PortOptReader::new_unchecked(self.as_slice())
2011 }
2012}
2013impl molecule::prelude::Entity for PortOpt {
2014 type Builder = PortOptBuilder;
2015 const NAME: &'static str = "PortOpt";
2016 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2017 PortOpt(data)
2018 }
2019 fn as_bytes(&self) -> molecule::bytes::Bytes {
2020 self.0.clone()
2021 }
2022 fn as_slice(&self) -> &[u8] {
2023 &self.0[..]
2024 }
2025 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2026 PortOptReader::from_slice(slice).map(|reader| reader.to_entity())
2027 }
2028 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2029 PortOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2030 }
2031 fn new_builder() -> Self::Builder {
2032 ::core::default::Default::default()
2033 }
2034 fn as_builder(self) -> Self::Builder {
2035 Self::new_builder().set(self.to_opt())
2036 }
2037}
2038#[derive(Clone, Copy)]
2039pub struct PortOptReader<'r>(&'r [u8]);
2040impl<'r> ::core::fmt::LowerHex for PortOptReader<'r> {
2041 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2042 use molecule::hex_string;
2043 if f.alternate() {
2044 write!(f, "0x")?;
2045 }
2046 write!(f, "{}", hex_string(self.as_slice()))
2047 }
2048}
2049impl<'r> ::core::fmt::Debug for PortOptReader<'r> {
2050 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2051 write!(f, "{}({:#x})", Self::NAME, self)
2052 }
2053}
2054impl<'r> ::core::fmt::Display for PortOptReader<'r> {
2055 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2056 if let Some(v) = self.to_opt() {
2057 write!(f, "{}(Some({}))", Self::NAME, v)
2058 } else {
2059 write!(f, "{}(None)", Self::NAME)
2060 }
2061 }
2062}
2063impl<'r> PortOptReader<'r> {
2064 pub fn is_none(&self) -> bool {
2065 self.0.is_empty()
2066 }
2067 pub fn is_some(&self) -> bool {
2068 !self.0.is_empty()
2069 }
2070 pub fn to_opt(&self) -> Option<Uint16Reader<'r>> {
2071 if self.is_none() {
2072 None
2073 } else {
2074 Some(Uint16Reader::new_unchecked(self.as_slice()))
2075 }
2076 }
2077}
2078impl<'r> molecule::prelude::Reader<'r> for PortOptReader<'r> {
2079 type Entity = PortOpt;
2080 const NAME: &'static str = "PortOptReader";
2081 fn to_entity(&self) -> Self::Entity {
2082 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2083 }
2084 fn new_unchecked(slice: &'r [u8]) -> Self {
2085 PortOptReader(slice)
2086 }
2087 fn as_slice(&self) -> &'r [u8] {
2088 self.0
2089 }
2090 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2091 if !slice.is_empty() {
2092 Uint16Reader::verify(&slice[..], compatible)?;
2093 }
2094 Ok(())
2095 }
2096}
2097#[derive(Clone, Debug, Default)]
2098pub struct PortOptBuilder(pub(crate) Option<Uint16>);
2099impl PortOptBuilder {
2100 pub fn set<T>(mut self, v: T) -> Self
2101 where
2102 T: ::core::convert::Into<Option<Uint16>>,
2103 {
2104 self.0 = v.into();
2105 self
2106 }
2107}
2108impl molecule::prelude::Builder for PortOptBuilder {
2109 type Entity = PortOpt;
2110 const NAME: &'static str = "PortOptBuilder";
2111 fn expected_length(&self) -> usize {
2112 self.0
2113 .as_ref()
2114 .map(|ref inner| inner.as_slice().len())
2115 .unwrap_or(0)
2116 }
2117 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2118 self.0
2119 .as_ref()
2120 .map(|ref inner| writer.write_all(inner.as_slice()))
2121 .unwrap_or(Ok(()))
2122 }
2123 fn build(&self) -> Self::Entity {
2124 let mut inner = Vec::with_capacity(self.expected_length());
2125 self.write(&mut inner)
2126 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2127 PortOpt::new_unchecked(inner.into())
2128 }
2129}
2130impl From<Uint16> for PortOpt {
2131 fn from(value: Uint16) -> Self {
2132 Self::new_builder().set(Some(value)).build()
2133 }
2134}
2135#[derive(Clone)]
2136pub struct DiscoveryPayload(molecule::bytes::Bytes);
2137impl ::core::fmt::LowerHex for DiscoveryPayload {
2138 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2139 use molecule::hex_string;
2140 if f.alternate() {
2141 write!(f, "0x")?;
2142 }
2143 write!(f, "{}", hex_string(self.as_slice()))
2144 }
2145}
2146impl ::core::fmt::Debug for DiscoveryPayload {
2147 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2148 write!(f, "{}({:#x})", Self::NAME, self)
2149 }
2150}
2151impl ::core::fmt::Display for DiscoveryPayload {
2152 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2153 write!(f, "{}(", Self::NAME)?;
2154 self.to_enum().display_inner(f)?;
2155 write!(f, ")")
2156 }
2157}
2158impl ::core::default::Default for DiscoveryPayload {
2159 fn default() -> Self {
2160 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2161 DiscoveryPayload::new_unchecked(v)
2162 }
2163}
2164impl DiscoveryPayload {
2165 const DEFAULT_VALUE: [u8; 28] = [
2166 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,
2167 ];
2168 pub const ITEMS_COUNT: usize = 2;
2169 pub fn item_id(&self) -> molecule::Number {
2170 molecule::unpack_number(self.as_slice())
2171 }
2172 pub fn to_enum(&self) -> DiscoveryPayloadUnion {
2173 let inner = self.0.slice(molecule::NUMBER_SIZE..);
2174 match self.item_id() {
2175 0 => GetNodes::new_unchecked(inner).into(),
2176 1 => Nodes::new_unchecked(inner).into(),
2177 _ => panic!("{}: invalid data", Self::NAME),
2178 }
2179 }
2180 pub fn as_reader<'r>(&'r self) -> DiscoveryPayloadReader<'r> {
2181 DiscoveryPayloadReader::new_unchecked(self.as_slice())
2182 }
2183}
2184impl molecule::prelude::Entity for DiscoveryPayload {
2185 type Builder = DiscoveryPayloadBuilder;
2186 const NAME: &'static str = "DiscoveryPayload";
2187 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2188 DiscoveryPayload(data)
2189 }
2190 fn as_bytes(&self) -> molecule::bytes::Bytes {
2191 self.0.clone()
2192 }
2193 fn as_slice(&self) -> &[u8] {
2194 &self.0[..]
2195 }
2196 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2197 DiscoveryPayloadReader::from_slice(slice).map(|reader| reader.to_entity())
2198 }
2199 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2200 DiscoveryPayloadReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2201 }
2202 fn new_builder() -> Self::Builder {
2203 ::core::default::Default::default()
2204 }
2205 fn as_builder(self) -> Self::Builder {
2206 Self::new_builder().set(self.to_enum())
2207 }
2208}
2209#[derive(Clone, Copy)]
2210pub struct DiscoveryPayloadReader<'r>(&'r [u8]);
2211impl<'r> ::core::fmt::LowerHex for DiscoveryPayloadReader<'r> {
2212 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2213 use molecule::hex_string;
2214 if f.alternate() {
2215 write!(f, "0x")?;
2216 }
2217 write!(f, "{}", hex_string(self.as_slice()))
2218 }
2219}
2220impl<'r> ::core::fmt::Debug for DiscoveryPayloadReader<'r> {
2221 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2222 write!(f, "{}({:#x})", Self::NAME, self)
2223 }
2224}
2225impl<'r> ::core::fmt::Display for DiscoveryPayloadReader<'r> {
2226 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2227 write!(f, "{}(", Self::NAME)?;
2228 self.to_enum().display_inner(f)?;
2229 write!(f, ")")
2230 }
2231}
2232impl<'r> DiscoveryPayloadReader<'r> {
2233 pub const ITEMS_COUNT: usize = 2;
2234 pub fn item_id(&self) -> molecule::Number {
2235 molecule::unpack_number(self.as_slice())
2236 }
2237 pub fn to_enum(&self) -> DiscoveryPayloadUnionReader<'r> {
2238 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
2239 match self.item_id() {
2240 0 => GetNodesReader::new_unchecked(inner).into(),
2241 1 => NodesReader::new_unchecked(inner).into(),
2242 _ => panic!("{}: invalid data", Self::NAME),
2243 }
2244 }
2245}
2246impl<'r> molecule::prelude::Reader<'r> for DiscoveryPayloadReader<'r> {
2247 type Entity = DiscoveryPayload;
2248 const NAME: &'static str = "DiscoveryPayloadReader";
2249 fn to_entity(&self) -> Self::Entity {
2250 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2251 }
2252 fn new_unchecked(slice: &'r [u8]) -> Self {
2253 DiscoveryPayloadReader(slice)
2254 }
2255 fn as_slice(&self) -> &'r [u8] {
2256 self.0
2257 }
2258 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2259 use molecule::verification_error as ve;
2260 let slice_len = slice.len();
2261 if slice_len < molecule::NUMBER_SIZE {
2262 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2263 }
2264 let item_id = molecule::unpack_number(slice);
2265 let inner_slice = &slice[molecule::NUMBER_SIZE..];
2266 match item_id {
2267 0 => GetNodesReader::verify(inner_slice, compatible),
2268 1 => NodesReader::verify(inner_slice, compatible),
2269 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
2270 }?;
2271 Ok(())
2272 }
2273}
2274#[derive(Clone, Debug, Default)]
2275pub struct DiscoveryPayloadBuilder(pub(crate) DiscoveryPayloadUnion);
2276impl DiscoveryPayloadBuilder {
2277 pub const ITEMS_COUNT: usize = 2;
2278 pub fn set<I>(mut self, v: I) -> Self
2279 where
2280 I: ::core::convert::Into<DiscoveryPayloadUnion>,
2281 {
2282 self.0 = v.into();
2283 self
2284 }
2285}
2286impl molecule::prelude::Builder for DiscoveryPayloadBuilder {
2287 type Entity = DiscoveryPayload;
2288 const NAME: &'static str = "DiscoveryPayloadBuilder";
2289 fn expected_length(&self) -> usize {
2290 molecule::NUMBER_SIZE + self.0.as_slice().len()
2291 }
2292 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2293 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
2294 writer.write_all(self.0.as_slice())
2295 }
2296 fn build(&self) -> Self::Entity {
2297 let mut inner = Vec::with_capacity(self.expected_length());
2298 self.write(&mut inner)
2299 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2300 DiscoveryPayload::new_unchecked(inner.into())
2301 }
2302}
2303#[derive(Debug, Clone)]
2304pub enum DiscoveryPayloadUnion {
2305 GetNodes(GetNodes),
2306 Nodes(Nodes),
2307}
2308#[derive(Debug, Clone, Copy)]
2309pub enum DiscoveryPayloadUnionReader<'r> {
2310 GetNodes(GetNodesReader<'r>),
2311 Nodes(NodesReader<'r>),
2312}
2313impl ::core::default::Default for DiscoveryPayloadUnion {
2314 fn default() -> Self {
2315 DiscoveryPayloadUnion::GetNodes(::core::default::Default::default())
2316 }
2317}
2318impl ::core::fmt::Display for DiscoveryPayloadUnion {
2319 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2320 match self {
2321 DiscoveryPayloadUnion::GetNodes(ref item) => {
2322 write!(f, "{}::{}({})", Self::NAME, GetNodes::NAME, item)
2323 }
2324 DiscoveryPayloadUnion::Nodes(ref item) => {
2325 write!(f, "{}::{}({})", Self::NAME, Nodes::NAME, item)
2326 }
2327 }
2328 }
2329}
2330impl<'r> ::core::fmt::Display for DiscoveryPayloadUnionReader<'r> {
2331 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2332 match self {
2333 DiscoveryPayloadUnionReader::GetNodes(ref item) => {
2334 write!(f, "{}::{}({})", Self::NAME, GetNodes::NAME, item)
2335 }
2336 DiscoveryPayloadUnionReader::Nodes(ref item) => {
2337 write!(f, "{}::{}({})", Self::NAME, Nodes::NAME, item)
2338 }
2339 }
2340 }
2341}
2342impl DiscoveryPayloadUnion {
2343 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2344 match self {
2345 DiscoveryPayloadUnion::GetNodes(ref item) => write!(f, "{}", item),
2346 DiscoveryPayloadUnion::Nodes(ref item) => write!(f, "{}", item),
2347 }
2348 }
2349}
2350impl<'r> DiscoveryPayloadUnionReader<'r> {
2351 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2352 match self {
2353 DiscoveryPayloadUnionReader::GetNodes(ref item) => write!(f, "{}", item),
2354 DiscoveryPayloadUnionReader::Nodes(ref item) => write!(f, "{}", item),
2355 }
2356 }
2357}
2358impl ::core::convert::From<GetNodes> for DiscoveryPayloadUnion {
2359 fn from(item: GetNodes) -> Self {
2360 DiscoveryPayloadUnion::GetNodes(item)
2361 }
2362}
2363impl ::core::convert::From<Nodes> for DiscoveryPayloadUnion {
2364 fn from(item: Nodes) -> Self {
2365 DiscoveryPayloadUnion::Nodes(item)
2366 }
2367}
2368impl<'r> ::core::convert::From<GetNodesReader<'r>> for DiscoveryPayloadUnionReader<'r> {
2369 fn from(item: GetNodesReader<'r>) -> Self {
2370 DiscoveryPayloadUnionReader::GetNodes(item)
2371 }
2372}
2373impl<'r> ::core::convert::From<NodesReader<'r>> for DiscoveryPayloadUnionReader<'r> {
2374 fn from(item: NodesReader<'r>) -> Self {
2375 DiscoveryPayloadUnionReader::Nodes(item)
2376 }
2377}
2378impl DiscoveryPayloadUnion {
2379 pub const NAME: &'static str = "DiscoveryPayloadUnion";
2380 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
2381 match self {
2382 DiscoveryPayloadUnion::GetNodes(item) => item.as_bytes(),
2383 DiscoveryPayloadUnion::Nodes(item) => item.as_bytes(),
2384 }
2385 }
2386 pub fn as_slice(&self) -> &[u8] {
2387 match self {
2388 DiscoveryPayloadUnion::GetNodes(item) => item.as_slice(),
2389 DiscoveryPayloadUnion::Nodes(item) => item.as_slice(),
2390 }
2391 }
2392 pub fn item_id(&self) -> molecule::Number {
2393 match self {
2394 DiscoveryPayloadUnion::GetNodes(_) => 0,
2395 DiscoveryPayloadUnion::Nodes(_) => 1,
2396 }
2397 }
2398 pub fn item_name(&self) -> &str {
2399 match self {
2400 DiscoveryPayloadUnion::GetNodes(_) => "GetNodes",
2401 DiscoveryPayloadUnion::Nodes(_) => "Nodes",
2402 }
2403 }
2404 pub fn as_reader<'r>(&'r self) -> DiscoveryPayloadUnionReader<'r> {
2405 match self {
2406 DiscoveryPayloadUnion::GetNodes(item) => item.as_reader().into(),
2407 DiscoveryPayloadUnion::Nodes(item) => item.as_reader().into(),
2408 }
2409 }
2410}
2411impl<'r> DiscoveryPayloadUnionReader<'r> {
2412 pub const NAME: &'r str = "DiscoveryPayloadUnionReader";
2413 pub fn as_slice(&self) -> &'r [u8] {
2414 match self {
2415 DiscoveryPayloadUnionReader::GetNodes(item) => item.as_slice(),
2416 DiscoveryPayloadUnionReader::Nodes(item) => item.as_slice(),
2417 }
2418 }
2419 pub fn item_id(&self) -> molecule::Number {
2420 match self {
2421 DiscoveryPayloadUnionReader::GetNodes(_) => 0,
2422 DiscoveryPayloadUnionReader::Nodes(_) => 1,
2423 }
2424 }
2425 pub fn item_name(&self) -> &str {
2426 match self {
2427 DiscoveryPayloadUnionReader::GetNodes(_) => "GetNodes",
2428 DiscoveryPayloadUnionReader::Nodes(_) => "Nodes",
2429 }
2430 }
2431}
2432impl From<GetNodes> for DiscoveryPayload {
2433 fn from(value: GetNodes) -> Self {
2434 Self::new_builder().set(value).build()
2435 }
2436}
2437impl From<Nodes> for DiscoveryPayload {
2438 fn from(value: Nodes) -> Self {
2439 Self::new_builder().set(value).build()
2440 }
2441}
2442#[derive(Clone)]
2443pub struct DiscoveryMessage(molecule::bytes::Bytes);
2444impl ::core::fmt::LowerHex for DiscoveryMessage {
2445 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2446 use molecule::hex_string;
2447 if f.alternate() {
2448 write!(f, "0x")?;
2449 }
2450 write!(f, "{}", hex_string(self.as_slice()))
2451 }
2452}
2453impl ::core::fmt::Debug for DiscoveryMessage {
2454 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2455 write!(f, "{}({:#x})", Self::NAME, self)
2456 }
2457}
2458impl ::core::fmt::Display for DiscoveryMessage {
2459 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2460 write!(f, "{} {{ ", Self::NAME)?;
2461 write!(f, "{}: {}", "payload", self.payload())?;
2462 let extra_count = self.count_extra_fields();
2463 if extra_count != 0 {
2464 write!(f, ", .. ({} fields)", extra_count)?;
2465 }
2466 write!(f, " }}")
2467 }
2468}
2469impl ::core::default::Default for DiscoveryMessage {
2470 fn default() -> Self {
2471 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2472 DiscoveryMessage::new_unchecked(v)
2473 }
2474}
2475impl DiscoveryMessage {
2476 const DEFAULT_VALUE: [u8; 36] = [
2477 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,
2478 0, 0, 0, 0, 0, 0, 0,
2479 ];
2480 pub const FIELD_COUNT: usize = 1;
2481 pub fn total_size(&self) -> usize {
2482 molecule::unpack_number(self.as_slice()) as usize
2483 }
2484 pub fn field_count(&self) -> usize {
2485 if self.total_size() == molecule::NUMBER_SIZE {
2486 0
2487 } else {
2488 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2489 }
2490 }
2491 pub fn count_extra_fields(&self) -> usize {
2492 self.field_count() - Self::FIELD_COUNT
2493 }
2494 pub fn has_extra_fields(&self) -> bool {
2495 Self::FIELD_COUNT != self.field_count()
2496 }
2497 pub fn payload(&self) -> DiscoveryPayload {
2498 let slice = self.as_slice();
2499 let start = molecule::unpack_number(&slice[4..]) as usize;
2500 if self.has_extra_fields() {
2501 let end = molecule::unpack_number(&slice[8..]) as usize;
2502 DiscoveryPayload::new_unchecked(self.0.slice(start..end))
2503 } else {
2504 DiscoveryPayload::new_unchecked(self.0.slice(start..))
2505 }
2506 }
2507 pub fn as_reader<'r>(&'r self) -> DiscoveryMessageReader<'r> {
2508 DiscoveryMessageReader::new_unchecked(self.as_slice())
2509 }
2510}
2511impl molecule::prelude::Entity for DiscoveryMessage {
2512 type Builder = DiscoveryMessageBuilder;
2513 const NAME: &'static str = "DiscoveryMessage";
2514 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2515 DiscoveryMessage(data)
2516 }
2517 fn as_bytes(&self) -> molecule::bytes::Bytes {
2518 self.0.clone()
2519 }
2520 fn as_slice(&self) -> &[u8] {
2521 &self.0[..]
2522 }
2523 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2524 DiscoveryMessageReader::from_slice(slice).map(|reader| reader.to_entity())
2525 }
2526 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2527 DiscoveryMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2528 }
2529 fn new_builder() -> Self::Builder {
2530 ::core::default::Default::default()
2531 }
2532 fn as_builder(self) -> Self::Builder {
2533 Self::new_builder().payload(self.payload())
2534 }
2535}
2536#[derive(Clone, Copy)]
2537pub struct DiscoveryMessageReader<'r>(&'r [u8]);
2538impl<'r> ::core::fmt::LowerHex for DiscoveryMessageReader<'r> {
2539 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2540 use molecule::hex_string;
2541 if f.alternate() {
2542 write!(f, "0x")?;
2543 }
2544 write!(f, "{}", hex_string(self.as_slice()))
2545 }
2546}
2547impl<'r> ::core::fmt::Debug for DiscoveryMessageReader<'r> {
2548 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2549 write!(f, "{}({:#x})", Self::NAME, self)
2550 }
2551}
2552impl<'r> ::core::fmt::Display for DiscoveryMessageReader<'r> {
2553 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2554 write!(f, "{} {{ ", Self::NAME)?;
2555 write!(f, "{}: {}", "payload", self.payload())?;
2556 let extra_count = self.count_extra_fields();
2557 if extra_count != 0 {
2558 write!(f, ", .. ({} fields)", extra_count)?;
2559 }
2560 write!(f, " }}")
2561 }
2562}
2563impl<'r> DiscoveryMessageReader<'r> {
2564 pub const FIELD_COUNT: usize = 1;
2565 pub fn total_size(&self) -> usize {
2566 molecule::unpack_number(self.as_slice()) as usize
2567 }
2568 pub fn field_count(&self) -> usize {
2569 if self.total_size() == molecule::NUMBER_SIZE {
2570 0
2571 } else {
2572 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2573 }
2574 }
2575 pub fn count_extra_fields(&self) -> usize {
2576 self.field_count() - Self::FIELD_COUNT
2577 }
2578 pub fn has_extra_fields(&self) -> bool {
2579 Self::FIELD_COUNT != self.field_count()
2580 }
2581 pub fn payload(&self) -> DiscoveryPayloadReader<'r> {
2582 let slice = self.as_slice();
2583 let start = molecule::unpack_number(&slice[4..]) as usize;
2584 if self.has_extra_fields() {
2585 let end = molecule::unpack_number(&slice[8..]) as usize;
2586 DiscoveryPayloadReader::new_unchecked(&self.as_slice()[start..end])
2587 } else {
2588 DiscoveryPayloadReader::new_unchecked(&self.as_slice()[start..])
2589 }
2590 }
2591}
2592impl<'r> molecule::prelude::Reader<'r> for DiscoveryMessageReader<'r> {
2593 type Entity = DiscoveryMessage;
2594 const NAME: &'static str = "DiscoveryMessageReader";
2595 fn to_entity(&self) -> Self::Entity {
2596 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2597 }
2598 fn new_unchecked(slice: &'r [u8]) -> Self {
2599 DiscoveryMessageReader(slice)
2600 }
2601 fn as_slice(&self) -> &'r [u8] {
2602 self.0
2603 }
2604 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2605 use molecule::verification_error as ve;
2606 let slice_len = slice.len();
2607 if slice_len < molecule::NUMBER_SIZE {
2608 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2609 }
2610 let total_size = molecule::unpack_number(slice) as usize;
2611 if slice_len != total_size {
2612 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2613 }
2614 if slice_len < molecule::NUMBER_SIZE * 2 {
2615 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
2616 }
2617 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
2618 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
2619 return ve!(Self, OffsetsNotMatch);
2620 }
2621 if slice_len < offset_first {
2622 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
2623 }
2624 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
2625 if field_count < Self::FIELD_COUNT {
2626 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2627 } else if !compatible && field_count > Self::FIELD_COUNT {
2628 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2629 };
2630 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
2631 .chunks_exact(molecule::NUMBER_SIZE)
2632 .map(|x| molecule::unpack_number(x) as usize)
2633 .collect();
2634 offsets.push(total_size);
2635 if offsets.windows(2).any(|i| i[0] > i[1]) {
2636 return ve!(Self, OffsetsNotMatch);
2637 }
2638 DiscoveryPayloadReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
2639 Ok(())
2640 }
2641}
2642#[derive(Clone, Debug, Default)]
2643pub struct DiscoveryMessageBuilder {
2644 pub(crate) payload: DiscoveryPayload,
2645}
2646impl DiscoveryMessageBuilder {
2647 pub const FIELD_COUNT: usize = 1;
2648 pub fn payload<T>(mut self, v: T) -> Self
2649 where
2650 T: ::core::convert::Into<DiscoveryPayload>,
2651 {
2652 self.payload = v.into();
2653 self
2654 }
2655}
2656impl molecule::prelude::Builder for DiscoveryMessageBuilder {
2657 type Entity = DiscoveryMessage;
2658 const NAME: &'static str = "DiscoveryMessageBuilder";
2659 fn expected_length(&self) -> usize {
2660 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.payload.as_slice().len()
2661 }
2662 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2663 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
2664 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
2665 offsets.push(total_size);
2666 total_size += self.payload.as_slice().len();
2667 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
2668 for offset in offsets.into_iter() {
2669 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
2670 }
2671 writer.write_all(self.payload.as_slice())?;
2672 Ok(())
2673 }
2674 fn build(&self) -> Self::Entity {
2675 let mut inner = Vec::with_capacity(self.expected_length());
2676 self.write(&mut inner)
2677 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2678 DiscoveryMessage::new_unchecked(inner.into())
2679 }
2680}
2681#[derive(Clone)]
2682pub struct GetNodes(molecule::bytes::Bytes);
2683impl ::core::fmt::LowerHex for GetNodes {
2684 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2685 use molecule::hex_string;
2686 if f.alternate() {
2687 write!(f, "0x")?;
2688 }
2689 write!(f, "{}", hex_string(self.as_slice()))
2690 }
2691}
2692impl ::core::fmt::Debug for GetNodes {
2693 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2694 write!(f, "{}({:#x})", Self::NAME, self)
2695 }
2696}
2697impl ::core::fmt::Display for GetNodes {
2698 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2699 write!(f, "{} {{ ", Self::NAME)?;
2700 write!(f, "{}: {}", "version", self.version())?;
2701 write!(f, ", {}: {}", "count", self.count())?;
2702 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
2703 let extra_count = self.count_extra_fields();
2704 if extra_count != 0 {
2705 write!(f, ", .. ({} fields)", extra_count)?;
2706 }
2707 write!(f, " }}")
2708 }
2709}
2710impl ::core::default::Default for GetNodes {
2711 fn default() -> Self {
2712 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2713 GetNodes::new_unchecked(v)
2714 }
2715}
2716impl GetNodes {
2717 const DEFAULT_VALUE: [u8; 24] = [
2718 24, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2719 ];
2720 pub const FIELD_COUNT: usize = 3;
2721 pub fn total_size(&self) -> usize {
2722 molecule::unpack_number(self.as_slice()) as usize
2723 }
2724 pub fn field_count(&self) -> usize {
2725 if self.total_size() == molecule::NUMBER_SIZE {
2726 0
2727 } else {
2728 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2729 }
2730 }
2731 pub fn count_extra_fields(&self) -> usize {
2732 self.field_count() - Self::FIELD_COUNT
2733 }
2734 pub fn has_extra_fields(&self) -> bool {
2735 Self::FIELD_COUNT != self.field_count()
2736 }
2737 pub fn version(&self) -> Uint32 {
2738 let slice = self.as_slice();
2739 let start = molecule::unpack_number(&slice[4..]) as usize;
2740 let end = molecule::unpack_number(&slice[8..]) as usize;
2741 Uint32::new_unchecked(self.0.slice(start..end))
2742 }
2743 pub fn count(&self) -> Uint32 {
2744 let slice = self.as_slice();
2745 let start = molecule::unpack_number(&slice[8..]) as usize;
2746 let end = molecule::unpack_number(&slice[12..]) as usize;
2747 Uint32::new_unchecked(self.0.slice(start..end))
2748 }
2749 pub fn listen_port(&self) -> PortOpt {
2750 let slice = self.as_slice();
2751 let start = molecule::unpack_number(&slice[12..]) as usize;
2752 if self.has_extra_fields() {
2753 let end = molecule::unpack_number(&slice[16..]) as usize;
2754 PortOpt::new_unchecked(self.0.slice(start..end))
2755 } else {
2756 PortOpt::new_unchecked(self.0.slice(start..))
2757 }
2758 }
2759 pub fn as_reader<'r>(&'r self) -> GetNodesReader<'r> {
2760 GetNodesReader::new_unchecked(self.as_slice())
2761 }
2762}
2763impl molecule::prelude::Entity for GetNodes {
2764 type Builder = GetNodesBuilder;
2765 const NAME: &'static str = "GetNodes";
2766 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2767 GetNodes(data)
2768 }
2769 fn as_bytes(&self) -> molecule::bytes::Bytes {
2770 self.0.clone()
2771 }
2772 fn as_slice(&self) -> &[u8] {
2773 &self.0[..]
2774 }
2775 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2776 GetNodesReader::from_slice(slice).map(|reader| reader.to_entity())
2777 }
2778 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2779 GetNodesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2780 }
2781 fn new_builder() -> Self::Builder {
2782 ::core::default::Default::default()
2783 }
2784 fn as_builder(self) -> Self::Builder {
2785 Self::new_builder()
2786 .version(self.version())
2787 .count(self.count())
2788 .listen_port(self.listen_port())
2789 }
2790}
2791#[derive(Clone, Copy)]
2792pub struct GetNodesReader<'r>(&'r [u8]);
2793impl<'r> ::core::fmt::LowerHex for GetNodesReader<'r> {
2794 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2795 use molecule::hex_string;
2796 if f.alternate() {
2797 write!(f, "0x")?;
2798 }
2799 write!(f, "{}", hex_string(self.as_slice()))
2800 }
2801}
2802impl<'r> ::core::fmt::Debug for GetNodesReader<'r> {
2803 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2804 write!(f, "{}({:#x})", Self::NAME, self)
2805 }
2806}
2807impl<'r> ::core::fmt::Display for GetNodesReader<'r> {
2808 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2809 write!(f, "{} {{ ", Self::NAME)?;
2810 write!(f, "{}: {}", "version", self.version())?;
2811 write!(f, ", {}: {}", "count", self.count())?;
2812 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
2813 let extra_count = self.count_extra_fields();
2814 if extra_count != 0 {
2815 write!(f, ", .. ({} fields)", extra_count)?;
2816 }
2817 write!(f, " }}")
2818 }
2819}
2820impl<'r> GetNodesReader<'r> {
2821 pub const FIELD_COUNT: usize = 3;
2822 pub fn total_size(&self) -> usize {
2823 molecule::unpack_number(self.as_slice()) as usize
2824 }
2825 pub fn field_count(&self) -> usize {
2826 if self.total_size() == molecule::NUMBER_SIZE {
2827 0
2828 } else {
2829 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2830 }
2831 }
2832 pub fn count_extra_fields(&self) -> usize {
2833 self.field_count() - Self::FIELD_COUNT
2834 }
2835 pub fn has_extra_fields(&self) -> bool {
2836 Self::FIELD_COUNT != self.field_count()
2837 }
2838 pub fn version(&self) -> Uint32Reader<'r> {
2839 let slice = self.as_slice();
2840 let start = molecule::unpack_number(&slice[4..]) as usize;
2841 let end = molecule::unpack_number(&slice[8..]) as usize;
2842 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
2843 }
2844 pub fn count(&self) -> Uint32Reader<'r> {
2845 let slice = self.as_slice();
2846 let start = molecule::unpack_number(&slice[8..]) as usize;
2847 let end = molecule::unpack_number(&slice[12..]) as usize;
2848 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
2849 }
2850 pub fn listen_port(&self) -> PortOptReader<'r> {
2851 let slice = self.as_slice();
2852 let start = molecule::unpack_number(&slice[12..]) as usize;
2853 if self.has_extra_fields() {
2854 let end = molecule::unpack_number(&slice[16..]) as usize;
2855 PortOptReader::new_unchecked(&self.as_slice()[start..end])
2856 } else {
2857 PortOptReader::new_unchecked(&self.as_slice()[start..])
2858 }
2859 }
2860}
2861impl<'r> molecule::prelude::Reader<'r> for GetNodesReader<'r> {
2862 type Entity = GetNodes;
2863 const NAME: &'static str = "GetNodesReader";
2864 fn to_entity(&self) -> Self::Entity {
2865 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2866 }
2867 fn new_unchecked(slice: &'r [u8]) -> Self {
2868 GetNodesReader(slice)
2869 }
2870 fn as_slice(&self) -> &'r [u8] {
2871 self.0
2872 }
2873 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2874 use molecule::verification_error as ve;
2875 let slice_len = slice.len();
2876 if slice_len < molecule::NUMBER_SIZE {
2877 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2878 }
2879 let total_size = molecule::unpack_number(slice) as usize;
2880 if slice_len != total_size {
2881 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2882 }
2883 if slice_len < molecule::NUMBER_SIZE * 2 {
2884 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
2885 }
2886 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
2887 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
2888 return ve!(Self, OffsetsNotMatch);
2889 }
2890 if slice_len < offset_first {
2891 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
2892 }
2893 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
2894 if field_count < Self::FIELD_COUNT {
2895 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2896 } else if !compatible && field_count > Self::FIELD_COUNT {
2897 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
2898 };
2899 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
2900 .chunks_exact(molecule::NUMBER_SIZE)
2901 .map(|x| molecule::unpack_number(x) as usize)
2902 .collect();
2903 offsets.push(total_size);
2904 if offsets.windows(2).any(|i| i[0] > i[1]) {
2905 return ve!(Self, OffsetsNotMatch);
2906 }
2907 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
2908 Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
2909 PortOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
2910 Ok(())
2911 }
2912}
2913#[derive(Clone, Debug, Default)]
2914pub struct GetNodesBuilder {
2915 pub(crate) version: Uint32,
2916 pub(crate) count: Uint32,
2917 pub(crate) listen_port: PortOpt,
2918}
2919impl GetNodesBuilder {
2920 pub const FIELD_COUNT: usize = 3;
2921 pub fn version<T>(mut self, v: T) -> Self
2922 where
2923 T: ::core::convert::Into<Uint32>,
2924 {
2925 self.version = v.into();
2926 self
2927 }
2928 pub fn count<T>(mut self, v: T) -> Self
2929 where
2930 T: ::core::convert::Into<Uint32>,
2931 {
2932 self.count = v.into();
2933 self
2934 }
2935 pub fn listen_port<T>(mut self, v: T) -> Self
2936 where
2937 T: ::core::convert::Into<PortOpt>,
2938 {
2939 self.listen_port = v.into();
2940 self
2941 }
2942}
2943impl molecule::prelude::Builder for GetNodesBuilder {
2944 type Entity = GetNodes;
2945 const NAME: &'static str = "GetNodesBuilder";
2946 fn expected_length(&self) -> usize {
2947 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
2948 + self.version.as_slice().len()
2949 + self.count.as_slice().len()
2950 + self.listen_port.as_slice().len()
2951 }
2952 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2953 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
2954 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
2955 offsets.push(total_size);
2956 total_size += self.version.as_slice().len();
2957 offsets.push(total_size);
2958 total_size += self.count.as_slice().len();
2959 offsets.push(total_size);
2960 total_size += self.listen_port.as_slice().len();
2961 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
2962 for offset in offsets.into_iter() {
2963 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
2964 }
2965 writer.write_all(self.version.as_slice())?;
2966 writer.write_all(self.count.as_slice())?;
2967 writer.write_all(self.listen_port.as_slice())?;
2968 Ok(())
2969 }
2970 fn build(&self) -> Self::Entity {
2971 let mut inner = Vec::with_capacity(self.expected_length());
2972 self.write(&mut inner)
2973 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2974 GetNodes::new_unchecked(inner.into())
2975 }
2976}
2977#[derive(Clone)]
2978pub struct GetNodes2(molecule::bytes::Bytes);
2979impl ::core::fmt::LowerHex for GetNodes2 {
2980 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2981 use molecule::hex_string;
2982 if f.alternate() {
2983 write!(f, "0x")?;
2984 }
2985 write!(f, "{}", hex_string(self.as_slice()))
2986 }
2987}
2988impl ::core::fmt::Debug for GetNodes2 {
2989 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2990 write!(f, "{}({:#x})", Self::NAME, self)
2991 }
2992}
2993impl ::core::fmt::Display for GetNodes2 {
2994 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2995 write!(f, "{} {{ ", Self::NAME)?;
2996 write!(f, "{}: {}", "version", self.version())?;
2997 write!(f, ", {}: {}", "count", self.count())?;
2998 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
2999 write!(f, ", {}: {}", "required_flags", self.required_flags())?;
3000 let extra_count = self.count_extra_fields();
3001 if extra_count != 0 {
3002 write!(f, ", .. ({} fields)", extra_count)?;
3003 }
3004 write!(f, " }}")
3005 }
3006}
3007impl ::core::default::Default for GetNodes2 {
3008 fn default() -> Self {
3009 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3010 GetNodes2::new_unchecked(v)
3011 }
3012}
3013impl GetNodes2 {
3014 const DEFAULT_VALUE: [u8; 36] = [
3015 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,
3016 0, 0, 0, 0, 0, 0, 0,
3017 ];
3018 pub const FIELD_COUNT: usize = 4;
3019 pub fn total_size(&self) -> usize {
3020 molecule::unpack_number(self.as_slice()) as usize
3021 }
3022 pub fn field_count(&self) -> usize {
3023 if self.total_size() == molecule::NUMBER_SIZE {
3024 0
3025 } else {
3026 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3027 }
3028 }
3029 pub fn count_extra_fields(&self) -> usize {
3030 self.field_count() - Self::FIELD_COUNT
3031 }
3032 pub fn has_extra_fields(&self) -> bool {
3033 Self::FIELD_COUNT != self.field_count()
3034 }
3035 pub fn version(&self) -> Uint32 {
3036 let slice = self.as_slice();
3037 let start = molecule::unpack_number(&slice[4..]) as usize;
3038 let end = molecule::unpack_number(&slice[8..]) as usize;
3039 Uint32::new_unchecked(self.0.slice(start..end))
3040 }
3041 pub fn count(&self) -> Uint32 {
3042 let slice = self.as_slice();
3043 let start = molecule::unpack_number(&slice[8..]) as usize;
3044 let end = molecule::unpack_number(&slice[12..]) as usize;
3045 Uint32::new_unchecked(self.0.slice(start..end))
3046 }
3047 pub fn listen_port(&self) -> PortOpt {
3048 let slice = self.as_slice();
3049 let start = molecule::unpack_number(&slice[12..]) as usize;
3050 let end = molecule::unpack_number(&slice[16..]) as usize;
3051 PortOpt::new_unchecked(self.0.slice(start..end))
3052 }
3053 pub fn required_flags(&self) -> Uint64 {
3054 let slice = self.as_slice();
3055 let start = molecule::unpack_number(&slice[16..]) as usize;
3056 if self.has_extra_fields() {
3057 let end = molecule::unpack_number(&slice[20..]) as usize;
3058 Uint64::new_unchecked(self.0.slice(start..end))
3059 } else {
3060 Uint64::new_unchecked(self.0.slice(start..))
3061 }
3062 }
3063 pub fn as_reader<'r>(&'r self) -> GetNodes2Reader<'r> {
3064 GetNodes2Reader::new_unchecked(self.as_slice())
3065 }
3066}
3067impl molecule::prelude::Entity for GetNodes2 {
3068 type Builder = GetNodes2Builder;
3069 const NAME: &'static str = "GetNodes2";
3070 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3071 GetNodes2(data)
3072 }
3073 fn as_bytes(&self) -> molecule::bytes::Bytes {
3074 self.0.clone()
3075 }
3076 fn as_slice(&self) -> &[u8] {
3077 &self.0[..]
3078 }
3079 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3080 GetNodes2Reader::from_slice(slice).map(|reader| reader.to_entity())
3081 }
3082 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3083 GetNodes2Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3084 }
3085 fn new_builder() -> Self::Builder {
3086 ::core::default::Default::default()
3087 }
3088 fn as_builder(self) -> Self::Builder {
3089 Self::new_builder()
3090 .version(self.version())
3091 .count(self.count())
3092 .listen_port(self.listen_port())
3093 .required_flags(self.required_flags())
3094 }
3095}
3096#[derive(Clone, Copy)]
3097pub struct GetNodes2Reader<'r>(&'r [u8]);
3098impl<'r> ::core::fmt::LowerHex for GetNodes2Reader<'r> {
3099 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3100 use molecule::hex_string;
3101 if f.alternate() {
3102 write!(f, "0x")?;
3103 }
3104 write!(f, "{}", hex_string(self.as_slice()))
3105 }
3106}
3107impl<'r> ::core::fmt::Debug for GetNodes2Reader<'r> {
3108 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3109 write!(f, "{}({:#x})", Self::NAME, self)
3110 }
3111}
3112impl<'r> ::core::fmt::Display for GetNodes2Reader<'r> {
3113 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3114 write!(f, "{} {{ ", Self::NAME)?;
3115 write!(f, "{}: {}", "version", self.version())?;
3116 write!(f, ", {}: {}", "count", self.count())?;
3117 write!(f, ", {}: {}", "listen_port", self.listen_port())?;
3118 write!(f, ", {}: {}", "required_flags", self.required_flags())?;
3119 let extra_count = self.count_extra_fields();
3120 if extra_count != 0 {
3121 write!(f, ", .. ({} fields)", extra_count)?;
3122 }
3123 write!(f, " }}")
3124 }
3125}
3126impl<'r> GetNodes2Reader<'r> {
3127 pub const FIELD_COUNT: usize = 4;
3128 pub fn total_size(&self) -> usize {
3129 molecule::unpack_number(self.as_slice()) as usize
3130 }
3131 pub fn field_count(&self) -> usize {
3132 if self.total_size() == molecule::NUMBER_SIZE {
3133 0
3134 } else {
3135 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3136 }
3137 }
3138 pub fn count_extra_fields(&self) -> usize {
3139 self.field_count() - Self::FIELD_COUNT
3140 }
3141 pub fn has_extra_fields(&self) -> bool {
3142 Self::FIELD_COUNT != self.field_count()
3143 }
3144 pub fn version(&self) -> Uint32Reader<'r> {
3145 let slice = self.as_slice();
3146 let start = molecule::unpack_number(&slice[4..]) as usize;
3147 let end = molecule::unpack_number(&slice[8..]) as usize;
3148 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
3149 }
3150 pub fn count(&self) -> Uint32Reader<'r> {
3151 let slice = self.as_slice();
3152 let start = molecule::unpack_number(&slice[8..]) as usize;
3153 let end = molecule::unpack_number(&slice[12..]) as usize;
3154 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
3155 }
3156 pub fn listen_port(&self) -> PortOptReader<'r> {
3157 let slice = self.as_slice();
3158 let start = molecule::unpack_number(&slice[12..]) as usize;
3159 let end = molecule::unpack_number(&slice[16..]) as usize;
3160 PortOptReader::new_unchecked(&self.as_slice()[start..end])
3161 }
3162 pub fn required_flags(&self) -> Uint64Reader<'r> {
3163 let slice = self.as_slice();
3164 let start = molecule::unpack_number(&slice[16..]) as usize;
3165 if self.has_extra_fields() {
3166 let end = molecule::unpack_number(&slice[20..]) as usize;
3167 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
3168 } else {
3169 Uint64Reader::new_unchecked(&self.as_slice()[start..])
3170 }
3171 }
3172}
3173impl<'r> molecule::prelude::Reader<'r> for GetNodes2Reader<'r> {
3174 type Entity = GetNodes2;
3175 const NAME: &'static str = "GetNodes2Reader";
3176 fn to_entity(&self) -> Self::Entity {
3177 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3178 }
3179 fn new_unchecked(slice: &'r [u8]) -> Self {
3180 GetNodes2Reader(slice)
3181 }
3182 fn as_slice(&self) -> &'r [u8] {
3183 self.0
3184 }
3185 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3186 use molecule::verification_error as ve;
3187 let slice_len = slice.len();
3188 if slice_len < molecule::NUMBER_SIZE {
3189 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3190 }
3191 let total_size = molecule::unpack_number(slice) as usize;
3192 if slice_len != total_size {
3193 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3194 }
3195 if slice_len < molecule::NUMBER_SIZE * 2 {
3196 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3197 }
3198 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3199 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3200 return ve!(Self, OffsetsNotMatch);
3201 }
3202 if slice_len < offset_first {
3203 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3204 }
3205 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3206 if field_count < Self::FIELD_COUNT {
3207 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3208 } else if !compatible && field_count > Self::FIELD_COUNT {
3209 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3210 };
3211 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3212 .chunks_exact(molecule::NUMBER_SIZE)
3213 .map(|x| molecule::unpack_number(x) as usize)
3214 .collect();
3215 offsets.push(total_size);
3216 if offsets.windows(2).any(|i| i[0] > i[1]) {
3217 return ve!(Self, OffsetsNotMatch);
3218 }
3219 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3220 Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3221 PortOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
3222 Uint64Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
3223 Ok(())
3224 }
3225}
3226#[derive(Clone, Debug, Default)]
3227pub struct GetNodes2Builder {
3228 pub(crate) version: Uint32,
3229 pub(crate) count: Uint32,
3230 pub(crate) listen_port: PortOpt,
3231 pub(crate) required_flags: Uint64,
3232}
3233impl GetNodes2Builder {
3234 pub const FIELD_COUNT: usize = 4;
3235 pub fn version<T>(mut self, v: T) -> Self
3236 where
3237 T: ::core::convert::Into<Uint32>,
3238 {
3239 self.version = v.into();
3240 self
3241 }
3242 pub fn count<T>(mut self, v: T) -> Self
3243 where
3244 T: ::core::convert::Into<Uint32>,
3245 {
3246 self.count = v.into();
3247 self
3248 }
3249 pub fn listen_port<T>(mut self, v: T) -> Self
3250 where
3251 T: ::core::convert::Into<PortOpt>,
3252 {
3253 self.listen_port = v.into();
3254 self
3255 }
3256 pub fn required_flags<T>(mut self, v: T) -> Self
3257 where
3258 T: ::core::convert::Into<Uint64>,
3259 {
3260 self.required_flags = v.into();
3261 self
3262 }
3263}
3264impl molecule::prelude::Builder for GetNodes2Builder {
3265 type Entity = GetNodes2;
3266 const NAME: &'static str = "GetNodes2Builder";
3267 fn expected_length(&self) -> usize {
3268 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3269 + self.version.as_slice().len()
3270 + self.count.as_slice().len()
3271 + self.listen_port.as_slice().len()
3272 + self.required_flags.as_slice().len()
3273 }
3274 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3275 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3276 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3277 offsets.push(total_size);
3278 total_size += self.version.as_slice().len();
3279 offsets.push(total_size);
3280 total_size += self.count.as_slice().len();
3281 offsets.push(total_size);
3282 total_size += self.listen_port.as_slice().len();
3283 offsets.push(total_size);
3284 total_size += self.required_flags.as_slice().len();
3285 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3286 for offset in offsets.into_iter() {
3287 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3288 }
3289 writer.write_all(self.version.as_slice())?;
3290 writer.write_all(self.count.as_slice())?;
3291 writer.write_all(self.listen_port.as_slice())?;
3292 writer.write_all(self.required_flags.as_slice())?;
3293 Ok(())
3294 }
3295 fn build(&self) -> Self::Entity {
3296 let mut inner = Vec::with_capacity(self.expected_length());
3297 self.write(&mut inner)
3298 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3299 GetNodes2::new_unchecked(inner.into())
3300 }
3301}
3302#[derive(Clone)]
3303pub struct Nodes(molecule::bytes::Bytes);
3304impl ::core::fmt::LowerHex for Nodes {
3305 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3306 use molecule::hex_string;
3307 if f.alternate() {
3308 write!(f, "0x")?;
3309 }
3310 write!(f, "{}", hex_string(self.as_slice()))
3311 }
3312}
3313impl ::core::fmt::Debug for Nodes {
3314 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3315 write!(f, "{}({:#x})", Self::NAME, self)
3316 }
3317}
3318impl ::core::fmt::Display for Nodes {
3319 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3320 write!(f, "{} {{ ", Self::NAME)?;
3321 write!(f, "{}: {}", "announce", self.announce())?;
3322 write!(f, ", {}: {}", "items", self.items())?;
3323 let extra_count = self.count_extra_fields();
3324 if extra_count != 0 {
3325 write!(f, ", .. ({} fields)", extra_count)?;
3326 }
3327 write!(f, " }}")
3328 }
3329}
3330impl ::core::default::Default for Nodes {
3331 fn default() -> Self {
3332 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3333 Nodes::new_unchecked(v)
3334 }
3335}
3336impl Nodes {
3337 const DEFAULT_VALUE: [u8; 17] = [17, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 0, 4, 0, 0, 0];
3338 pub const FIELD_COUNT: usize = 2;
3339 pub fn total_size(&self) -> usize {
3340 molecule::unpack_number(self.as_slice()) as usize
3341 }
3342 pub fn field_count(&self) -> usize {
3343 if self.total_size() == molecule::NUMBER_SIZE {
3344 0
3345 } else {
3346 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3347 }
3348 }
3349 pub fn count_extra_fields(&self) -> usize {
3350 self.field_count() - Self::FIELD_COUNT
3351 }
3352 pub fn has_extra_fields(&self) -> bool {
3353 Self::FIELD_COUNT != self.field_count()
3354 }
3355 pub fn announce(&self) -> Bool {
3356 let slice = self.as_slice();
3357 let start = molecule::unpack_number(&slice[4..]) as usize;
3358 let end = molecule::unpack_number(&slice[8..]) as usize;
3359 Bool::new_unchecked(self.0.slice(start..end))
3360 }
3361 pub fn items(&self) -> NodeVec {
3362 let slice = self.as_slice();
3363 let start = molecule::unpack_number(&slice[8..]) as usize;
3364 if self.has_extra_fields() {
3365 let end = molecule::unpack_number(&slice[12..]) as usize;
3366 NodeVec::new_unchecked(self.0.slice(start..end))
3367 } else {
3368 NodeVec::new_unchecked(self.0.slice(start..))
3369 }
3370 }
3371 pub fn as_reader<'r>(&'r self) -> NodesReader<'r> {
3372 NodesReader::new_unchecked(self.as_slice())
3373 }
3374}
3375impl molecule::prelude::Entity for Nodes {
3376 type Builder = NodesBuilder;
3377 const NAME: &'static str = "Nodes";
3378 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3379 Nodes(data)
3380 }
3381 fn as_bytes(&self) -> molecule::bytes::Bytes {
3382 self.0.clone()
3383 }
3384 fn as_slice(&self) -> &[u8] {
3385 &self.0[..]
3386 }
3387 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3388 NodesReader::from_slice(slice).map(|reader| reader.to_entity())
3389 }
3390 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3391 NodesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3392 }
3393 fn new_builder() -> Self::Builder {
3394 ::core::default::Default::default()
3395 }
3396 fn as_builder(self) -> Self::Builder {
3397 Self::new_builder()
3398 .announce(self.announce())
3399 .items(self.items())
3400 }
3401}
3402#[derive(Clone, Copy)]
3403pub struct NodesReader<'r>(&'r [u8]);
3404impl<'r> ::core::fmt::LowerHex for NodesReader<'r> {
3405 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3406 use molecule::hex_string;
3407 if f.alternate() {
3408 write!(f, "0x")?;
3409 }
3410 write!(f, "{}", hex_string(self.as_slice()))
3411 }
3412}
3413impl<'r> ::core::fmt::Debug for NodesReader<'r> {
3414 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3415 write!(f, "{}({:#x})", Self::NAME, self)
3416 }
3417}
3418impl<'r> ::core::fmt::Display for NodesReader<'r> {
3419 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3420 write!(f, "{} {{ ", Self::NAME)?;
3421 write!(f, "{}: {}", "announce", self.announce())?;
3422 write!(f, ", {}: {}", "items", self.items())?;
3423 let extra_count = self.count_extra_fields();
3424 if extra_count != 0 {
3425 write!(f, ", .. ({} fields)", extra_count)?;
3426 }
3427 write!(f, " }}")
3428 }
3429}
3430impl<'r> NodesReader<'r> {
3431 pub const FIELD_COUNT: usize = 2;
3432 pub fn total_size(&self) -> usize {
3433 molecule::unpack_number(self.as_slice()) as usize
3434 }
3435 pub fn field_count(&self) -> usize {
3436 if self.total_size() == molecule::NUMBER_SIZE {
3437 0
3438 } else {
3439 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3440 }
3441 }
3442 pub fn count_extra_fields(&self) -> usize {
3443 self.field_count() - Self::FIELD_COUNT
3444 }
3445 pub fn has_extra_fields(&self) -> bool {
3446 Self::FIELD_COUNT != self.field_count()
3447 }
3448 pub fn announce(&self) -> BoolReader<'r> {
3449 let slice = self.as_slice();
3450 let start = molecule::unpack_number(&slice[4..]) as usize;
3451 let end = molecule::unpack_number(&slice[8..]) as usize;
3452 BoolReader::new_unchecked(&self.as_slice()[start..end])
3453 }
3454 pub fn items(&self) -> NodeVecReader<'r> {
3455 let slice = self.as_slice();
3456 let start = molecule::unpack_number(&slice[8..]) as usize;
3457 if self.has_extra_fields() {
3458 let end = molecule::unpack_number(&slice[12..]) as usize;
3459 NodeVecReader::new_unchecked(&self.as_slice()[start..end])
3460 } else {
3461 NodeVecReader::new_unchecked(&self.as_slice()[start..])
3462 }
3463 }
3464}
3465impl<'r> molecule::prelude::Reader<'r> for NodesReader<'r> {
3466 type Entity = Nodes;
3467 const NAME: &'static str = "NodesReader";
3468 fn to_entity(&self) -> Self::Entity {
3469 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3470 }
3471 fn new_unchecked(slice: &'r [u8]) -> Self {
3472 NodesReader(slice)
3473 }
3474 fn as_slice(&self) -> &'r [u8] {
3475 self.0
3476 }
3477 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3478 use molecule::verification_error as ve;
3479 let slice_len = slice.len();
3480 if slice_len < molecule::NUMBER_SIZE {
3481 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3482 }
3483 let total_size = molecule::unpack_number(slice) as usize;
3484 if slice_len != total_size {
3485 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3486 }
3487 if slice_len < molecule::NUMBER_SIZE * 2 {
3488 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3489 }
3490 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3491 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3492 return ve!(Self, OffsetsNotMatch);
3493 }
3494 if slice_len < offset_first {
3495 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3496 }
3497 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3498 if field_count < Self::FIELD_COUNT {
3499 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3500 } else if !compatible && field_count > Self::FIELD_COUNT {
3501 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3502 };
3503 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3504 .chunks_exact(molecule::NUMBER_SIZE)
3505 .map(|x| molecule::unpack_number(x) as usize)
3506 .collect();
3507 offsets.push(total_size);
3508 if offsets.windows(2).any(|i| i[0] > i[1]) {
3509 return ve!(Self, OffsetsNotMatch);
3510 }
3511 BoolReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3512 NodeVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3513 Ok(())
3514 }
3515}
3516#[derive(Clone, Debug, Default)]
3517pub struct NodesBuilder {
3518 pub(crate) announce: Bool,
3519 pub(crate) items: NodeVec,
3520}
3521impl NodesBuilder {
3522 pub const FIELD_COUNT: usize = 2;
3523 pub fn announce<T>(mut self, v: T) -> Self
3524 where
3525 T: ::core::convert::Into<Bool>,
3526 {
3527 self.announce = v.into();
3528 self
3529 }
3530 pub fn items<T>(mut self, v: T) -> Self
3531 where
3532 T: ::core::convert::Into<NodeVec>,
3533 {
3534 self.items = v.into();
3535 self
3536 }
3537}
3538impl molecule::prelude::Builder for NodesBuilder {
3539 type Entity = Nodes;
3540 const NAME: &'static str = "NodesBuilder";
3541 fn expected_length(&self) -> usize {
3542 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3543 + self.announce.as_slice().len()
3544 + self.items.as_slice().len()
3545 }
3546 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3547 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3548 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3549 offsets.push(total_size);
3550 total_size += self.announce.as_slice().len();
3551 offsets.push(total_size);
3552 total_size += self.items.as_slice().len();
3553 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3554 for offset in offsets.into_iter() {
3555 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3556 }
3557 writer.write_all(self.announce.as_slice())?;
3558 writer.write_all(self.items.as_slice())?;
3559 Ok(())
3560 }
3561 fn build(&self) -> Self::Entity {
3562 let mut inner = Vec::with_capacity(self.expected_length());
3563 self.write(&mut inner)
3564 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3565 Nodes::new_unchecked(inner.into())
3566 }
3567}
3568#[derive(Clone)]
3569pub struct Nodes2(molecule::bytes::Bytes);
3570impl ::core::fmt::LowerHex for Nodes2 {
3571 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3572 use molecule::hex_string;
3573 if f.alternate() {
3574 write!(f, "0x")?;
3575 }
3576 write!(f, "{}", hex_string(self.as_slice()))
3577 }
3578}
3579impl ::core::fmt::Debug for Nodes2 {
3580 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3581 write!(f, "{}({:#x})", Self::NAME, self)
3582 }
3583}
3584impl ::core::fmt::Display for Nodes2 {
3585 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3586 write!(f, "{} {{ ", Self::NAME)?;
3587 write!(f, "{}: {}", "announce", self.announce())?;
3588 write!(f, ", {}: {}", "items", self.items())?;
3589 let extra_count = self.count_extra_fields();
3590 if extra_count != 0 {
3591 write!(f, ", .. ({} fields)", extra_count)?;
3592 }
3593 write!(f, " }}")
3594 }
3595}
3596impl ::core::default::Default for Nodes2 {
3597 fn default() -> Self {
3598 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3599 Nodes2::new_unchecked(v)
3600 }
3601}
3602impl Nodes2 {
3603 const DEFAULT_VALUE: [u8; 17] = [17, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 0, 4, 0, 0, 0];
3604 pub const FIELD_COUNT: usize = 2;
3605 pub fn total_size(&self) -> usize {
3606 molecule::unpack_number(self.as_slice()) as usize
3607 }
3608 pub fn field_count(&self) -> usize {
3609 if self.total_size() == molecule::NUMBER_SIZE {
3610 0
3611 } else {
3612 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3613 }
3614 }
3615 pub fn count_extra_fields(&self) -> usize {
3616 self.field_count() - Self::FIELD_COUNT
3617 }
3618 pub fn has_extra_fields(&self) -> bool {
3619 Self::FIELD_COUNT != self.field_count()
3620 }
3621 pub fn announce(&self) -> Bool {
3622 let slice = self.as_slice();
3623 let start = molecule::unpack_number(&slice[4..]) as usize;
3624 let end = molecule::unpack_number(&slice[8..]) as usize;
3625 Bool::new_unchecked(self.0.slice(start..end))
3626 }
3627 pub fn items(&self) -> Node2Vec {
3628 let slice = self.as_slice();
3629 let start = molecule::unpack_number(&slice[8..]) as usize;
3630 if self.has_extra_fields() {
3631 let end = molecule::unpack_number(&slice[12..]) as usize;
3632 Node2Vec::new_unchecked(self.0.slice(start..end))
3633 } else {
3634 Node2Vec::new_unchecked(self.0.slice(start..))
3635 }
3636 }
3637 pub fn as_reader<'r>(&'r self) -> Nodes2Reader<'r> {
3638 Nodes2Reader::new_unchecked(self.as_slice())
3639 }
3640}
3641impl molecule::prelude::Entity for Nodes2 {
3642 type Builder = Nodes2Builder;
3643 const NAME: &'static str = "Nodes2";
3644 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3645 Nodes2(data)
3646 }
3647 fn as_bytes(&self) -> molecule::bytes::Bytes {
3648 self.0.clone()
3649 }
3650 fn as_slice(&self) -> &[u8] {
3651 &self.0[..]
3652 }
3653 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3654 Nodes2Reader::from_slice(slice).map(|reader| reader.to_entity())
3655 }
3656 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3657 Nodes2Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3658 }
3659 fn new_builder() -> Self::Builder {
3660 ::core::default::Default::default()
3661 }
3662 fn as_builder(self) -> Self::Builder {
3663 Self::new_builder()
3664 .announce(self.announce())
3665 .items(self.items())
3666 }
3667}
3668#[derive(Clone, Copy)]
3669pub struct Nodes2Reader<'r>(&'r [u8]);
3670impl<'r> ::core::fmt::LowerHex for Nodes2Reader<'r> {
3671 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3672 use molecule::hex_string;
3673 if f.alternate() {
3674 write!(f, "0x")?;
3675 }
3676 write!(f, "{}", hex_string(self.as_slice()))
3677 }
3678}
3679impl<'r> ::core::fmt::Debug for Nodes2Reader<'r> {
3680 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3681 write!(f, "{}({:#x})", Self::NAME, self)
3682 }
3683}
3684impl<'r> ::core::fmt::Display for Nodes2Reader<'r> {
3685 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3686 write!(f, "{} {{ ", Self::NAME)?;
3687 write!(f, "{}: {}", "announce", self.announce())?;
3688 write!(f, ", {}: {}", "items", self.items())?;
3689 let extra_count = self.count_extra_fields();
3690 if extra_count != 0 {
3691 write!(f, ", .. ({} fields)", extra_count)?;
3692 }
3693 write!(f, " }}")
3694 }
3695}
3696impl<'r> Nodes2Reader<'r> {
3697 pub const FIELD_COUNT: usize = 2;
3698 pub fn total_size(&self) -> usize {
3699 molecule::unpack_number(self.as_slice()) as usize
3700 }
3701 pub fn field_count(&self) -> usize {
3702 if self.total_size() == molecule::NUMBER_SIZE {
3703 0
3704 } else {
3705 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3706 }
3707 }
3708 pub fn count_extra_fields(&self) -> usize {
3709 self.field_count() - Self::FIELD_COUNT
3710 }
3711 pub fn has_extra_fields(&self) -> bool {
3712 Self::FIELD_COUNT != self.field_count()
3713 }
3714 pub fn announce(&self) -> BoolReader<'r> {
3715 let slice = self.as_slice();
3716 let start = molecule::unpack_number(&slice[4..]) as usize;
3717 let end = molecule::unpack_number(&slice[8..]) as usize;
3718 BoolReader::new_unchecked(&self.as_slice()[start..end])
3719 }
3720 pub fn items(&self) -> Node2VecReader<'r> {
3721 let slice = self.as_slice();
3722 let start = molecule::unpack_number(&slice[8..]) as usize;
3723 if self.has_extra_fields() {
3724 let end = molecule::unpack_number(&slice[12..]) as usize;
3725 Node2VecReader::new_unchecked(&self.as_slice()[start..end])
3726 } else {
3727 Node2VecReader::new_unchecked(&self.as_slice()[start..])
3728 }
3729 }
3730}
3731impl<'r> molecule::prelude::Reader<'r> for Nodes2Reader<'r> {
3732 type Entity = Nodes2;
3733 const NAME: &'static str = "Nodes2Reader";
3734 fn to_entity(&self) -> Self::Entity {
3735 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3736 }
3737 fn new_unchecked(slice: &'r [u8]) -> Self {
3738 Nodes2Reader(slice)
3739 }
3740 fn as_slice(&self) -> &'r [u8] {
3741 self.0
3742 }
3743 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3744 use molecule::verification_error as ve;
3745 let slice_len = slice.len();
3746 if slice_len < molecule::NUMBER_SIZE {
3747 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3748 }
3749 let total_size = molecule::unpack_number(slice) as usize;
3750 if slice_len != total_size {
3751 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3752 }
3753 if slice_len < molecule::NUMBER_SIZE * 2 {
3754 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
3755 }
3756 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3757 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3758 return ve!(Self, OffsetsNotMatch);
3759 }
3760 if slice_len < offset_first {
3761 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3762 }
3763 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
3764 if field_count < Self::FIELD_COUNT {
3765 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3766 } else if !compatible && field_count > Self::FIELD_COUNT {
3767 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
3768 };
3769 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3770 .chunks_exact(molecule::NUMBER_SIZE)
3771 .map(|x| molecule::unpack_number(x) as usize)
3772 .collect();
3773 offsets.push(total_size);
3774 if offsets.windows(2).any(|i| i[0] > i[1]) {
3775 return ve!(Self, OffsetsNotMatch);
3776 }
3777 BoolReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
3778 Node2VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
3779 Ok(())
3780 }
3781}
3782#[derive(Clone, Debug, Default)]
3783pub struct Nodes2Builder {
3784 pub(crate) announce: Bool,
3785 pub(crate) items: Node2Vec,
3786}
3787impl Nodes2Builder {
3788 pub const FIELD_COUNT: usize = 2;
3789 pub fn announce<T>(mut self, v: T) -> Self
3790 where
3791 T: ::core::convert::Into<Bool>,
3792 {
3793 self.announce = v.into();
3794 self
3795 }
3796 pub fn items<T>(mut self, v: T) -> Self
3797 where
3798 T: ::core::convert::Into<Node2Vec>,
3799 {
3800 self.items = v.into();
3801 self
3802 }
3803}
3804impl molecule::prelude::Builder for Nodes2Builder {
3805 type Entity = Nodes2;
3806 const NAME: &'static str = "Nodes2Builder";
3807 fn expected_length(&self) -> usize {
3808 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
3809 + self.announce.as_slice().len()
3810 + self.items.as_slice().len()
3811 }
3812 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3813 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
3814 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
3815 offsets.push(total_size);
3816 total_size += self.announce.as_slice().len();
3817 offsets.push(total_size);
3818 total_size += self.items.as_slice().len();
3819 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3820 for offset in offsets.into_iter() {
3821 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3822 }
3823 writer.write_all(self.announce.as_slice())?;
3824 writer.write_all(self.items.as_slice())?;
3825 Ok(())
3826 }
3827 fn build(&self) -> Self::Entity {
3828 let mut inner = Vec::with_capacity(self.expected_length());
3829 self.write(&mut inner)
3830 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3831 Nodes2::new_unchecked(inner.into())
3832 }
3833}
3834#[derive(Clone)]
3835pub struct Node(molecule::bytes::Bytes);
3836impl ::core::fmt::LowerHex for Node {
3837 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3838 use molecule::hex_string;
3839 if f.alternate() {
3840 write!(f, "0x")?;
3841 }
3842 write!(f, "{}", hex_string(self.as_slice()))
3843 }
3844}
3845impl ::core::fmt::Debug for Node {
3846 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3847 write!(f, "{}({:#x})", Self::NAME, self)
3848 }
3849}
3850impl ::core::fmt::Display for Node {
3851 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3852 write!(f, "{} {{ ", Self::NAME)?;
3853 write!(f, "{}: {}", "addresses", self.addresses())?;
3854 let extra_count = self.count_extra_fields();
3855 if extra_count != 0 {
3856 write!(f, ", .. ({} fields)", extra_count)?;
3857 }
3858 write!(f, " }}")
3859 }
3860}
3861impl ::core::default::Default for Node {
3862 fn default() -> Self {
3863 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3864 Node::new_unchecked(v)
3865 }
3866}
3867impl Node {
3868 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0];
3869 pub const FIELD_COUNT: usize = 1;
3870 pub fn total_size(&self) -> usize {
3871 molecule::unpack_number(self.as_slice()) as usize
3872 }
3873 pub fn field_count(&self) -> usize {
3874 if self.total_size() == molecule::NUMBER_SIZE {
3875 0
3876 } else {
3877 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3878 }
3879 }
3880 pub fn count_extra_fields(&self) -> usize {
3881 self.field_count() - Self::FIELD_COUNT
3882 }
3883 pub fn has_extra_fields(&self) -> bool {
3884 Self::FIELD_COUNT != self.field_count()
3885 }
3886 pub fn addresses(&self) -> BytesVec {
3887 let slice = self.as_slice();
3888 let start = molecule::unpack_number(&slice[4..]) as usize;
3889 if self.has_extra_fields() {
3890 let end = molecule::unpack_number(&slice[8..]) as usize;
3891 BytesVec::new_unchecked(self.0.slice(start..end))
3892 } else {
3893 BytesVec::new_unchecked(self.0.slice(start..))
3894 }
3895 }
3896 pub fn as_reader<'r>(&'r self) -> NodeReader<'r> {
3897 NodeReader::new_unchecked(self.as_slice())
3898 }
3899}
3900impl molecule::prelude::Entity for Node {
3901 type Builder = NodeBuilder;
3902 const NAME: &'static str = "Node";
3903 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3904 Node(data)
3905 }
3906 fn as_bytes(&self) -> molecule::bytes::Bytes {
3907 self.0.clone()
3908 }
3909 fn as_slice(&self) -> &[u8] {
3910 &self.0[..]
3911 }
3912 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3913 NodeReader::from_slice(slice).map(|reader| reader.to_entity())
3914 }
3915 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3916 NodeReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3917 }
3918 fn new_builder() -> Self::Builder {
3919 ::core::default::Default::default()
3920 }
3921 fn as_builder(self) -> Self::Builder {
3922 Self::new_builder().addresses(self.addresses())
3923 }
3924}
3925#[derive(Clone, Copy)]
3926pub struct NodeReader<'r>(&'r [u8]);
3927impl<'r> ::core::fmt::LowerHex for NodeReader<'r> {
3928 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3929 use molecule::hex_string;
3930 if f.alternate() {
3931 write!(f, "0x")?;
3932 }
3933 write!(f, "{}", hex_string(self.as_slice()))
3934 }
3935}
3936impl<'r> ::core::fmt::Debug for NodeReader<'r> {
3937 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3938 write!(f, "{}({:#x})", Self::NAME, self)
3939 }
3940}
3941impl<'r> ::core::fmt::Display for NodeReader<'r> {
3942 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3943 write!(f, "{} {{ ", Self::NAME)?;
3944 write!(f, "{}: {}", "addresses", self.addresses())?;
3945 let extra_count = self.count_extra_fields();
3946 if extra_count != 0 {
3947 write!(f, ", .. ({} fields)", extra_count)?;
3948 }
3949 write!(f, " }}")
3950 }
3951}
3952impl<'r> NodeReader<'r> {
3953 pub const FIELD_COUNT: usize = 1;
3954 pub fn total_size(&self) -> usize {
3955 molecule::unpack_number(self.as_slice()) as usize
3956 }
3957 pub fn field_count(&self) -> usize {
3958 if self.total_size() == molecule::NUMBER_SIZE {
3959 0
3960 } else {
3961 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3962 }
3963 }
3964 pub fn count_extra_fields(&self) -> usize {
3965 self.field_count() - Self::FIELD_COUNT
3966 }
3967 pub fn has_extra_fields(&self) -> bool {
3968 Self::FIELD_COUNT != self.field_count()
3969 }
3970 pub fn addresses(&self) -> BytesVecReader<'r> {
3971 let slice = self.as_slice();
3972 let start = molecule::unpack_number(&slice[4..]) as usize;
3973 if self.has_extra_fields() {
3974 let end = molecule::unpack_number(&slice[8..]) as usize;
3975 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
3976 } else {
3977 BytesVecReader::new_unchecked(&self.as_slice()[start..])
3978 }
3979 }
3980}
3981impl<'r> molecule::prelude::Reader<'r> for NodeReader<'r> {
3982 type Entity = Node;
3983 const NAME: &'static str = "NodeReader";
3984 fn to_entity(&self) -> Self::Entity {
3985 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3986 }
3987 fn new_unchecked(slice: &'r [u8]) -> Self {
3988 NodeReader(slice)
3989 }
3990 fn as_slice(&self) -> &'r [u8] {
3991 self.0
3992 }
3993 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3994 use molecule::verification_error as ve;
3995 let slice_len = slice.len();
3996 if slice_len < molecule::NUMBER_SIZE {
3997 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3998 }
3999 let total_size = molecule::unpack_number(slice) as usize;
4000 if slice_len != total_size {
4001 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4002 }
4003 if slice_len < molecule::NUMBER_SIZE * 2 {
4004 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4005 }
4006 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4007 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4008 return ve!(Self, OffsetsNotMatch);
4009 }
4010 if slice_len < offset_first {
4011 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4012 }
4013 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4014 if field_count < Self::FIELD_COUNT {
4015 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4016 } else if !compatible && field_count > Self::FIELD_COUNT {
4017 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4018 };
4019 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4020 .chunks_exact(molecule::NUMBER_SIZE)
4021 .map(|x| molecule::unpack_number(x) as usize)
4022 .collect();
4023 offsets.push(total_size);
4024 if offsets.windows(2).any(|i| i[0] > i[1]) {
4025 return ve!(Self, OffsetsNotMatch);
4026 }
4027 BytesVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4028 Ok(())
4029 }
4030}
4031#[derive(Clone, Debug, Default)]
4032pub struct NodeBuilder {
4033 pub(crate) addresses: BytesVec,
4034}
4035impl NodeBuilder {
4036 pub const FIELD_COUNT: usize = 1;
4037 pub fn addresses<T>(mut self, v: T) -> Self
4038 where
4039 T: ::core::convert::Into<BytesVec>,
4040 {
4041 self.addresses = v.into();
4042 self
4043 }
4044}
4045impl molecule::prelude::Builder for NodeBuilder {
4046 type Entity = Node;
4047 const NAME: &'static str = "NodeBuilder";
4048 fn expected_length(&self) -> usize {
4049 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.addresses.as_slice().len()
4050 }
4051 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4052 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4053 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4054 offsets.push(total_size);
4055 total_size += self.addresses.as_slice().len();
4056 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4057 for offset in offsets.into_iter() {
4058 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4059 }
4060 writer.write_all(self.addresses.as_slice())?;
4061 Ok(())
4062 }
4063 fn build(&self) -> Self::Entity {
4064 let mut inner = Vec::with_capacity(self.expected_length());
4065 self.write(&mut inner)
4066 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4067 Node::new_unchecked(inner.into())
4068 }
4069}
4070#[derive(Clone)]
4071pub struct Node2(molecule::bytes::Bytes);
4072impl ::core::fmt::LowerHex for Node2 {
4073 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4074 use molecule::hex_string;
4075 if f.alternate() {
4076 write!(f, "0x")?;
4077 }
4078 write!(f, "{}", hex_string(self.as_slice()))
4079 }
4080}
4081impl ::core::fmt::Debug for Node2 {
4082 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4083 write!(f, "{}({:#x})", Self::NAME, self)
4084 }
4085}
4086impl ::core::fmt::Display for Node2 {
4087 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4088 write!(f, "{} {{ ", Self::NAME)?;
4089 write!(f, "{}: {}", "addresses", self.addresses())?;
4090 write!(f, ", {}: {}", "flags", self.flags())?;
4091 let extra_count = self.count_extra_fields();
4092 if extra_count != 0 {
4093 write!(f, ", .. ({} fields)", extra_count)?;
4094 }
4095 write!(f, " }}")
4096 }
4097}
4098impl ::core::default::Default for Node2 {
4099 fn default() -> Self {
4100 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4101 Node2::new_unchecked(v)
4102 }
4103}
4104impl Node2 {
4105 const DEFAULT_VALUE: [u8; 24] = [
4106 24, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4107 ];
4108 pub const FIELD_COUNT: usize = 2;
4109 pub fn total_size(&self) -> usize {
4110 molecule::unpack_number(self.as_slice()) as usize
4111 }
4112 pub fn field_count(&self) -> usize {
4113 if self.total_size() == molecule::NUMBER_SIZE {
4114 0
4115 } else {
4116 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4117 }
4118 }
4119 pub fn count_extra_fields(&self) -> usize {
4120 self.field_count() - Self::FIELD_COUNT
4121 }
4122 pub fn has_extra_fields(&self) -> bool {
4123 Self::FIELD_COUNT != self.field_count()
4124 }
4125 pub fn addresses(&self) -> BytesVec {
4126 let slice = self.as_slice();
4127 let start = molecule::unpack_number(&slice[4..]) as usize;
4128 let end = molecule::unpack_number(&slice[8..]) as usize;
4129 BytesVec::new_unchecked(self.0.slice(start..end))
4130 }
4131 pub fn flags(&self) -> Uint64 {
4132 let slice = self.as_slice();
4133 let start = molecule::unpack_number(&slice[8..]) as usize;
4134 if self.has_extra_fields() {
4135 let end = molecule::unpack_number(&slice[12..]) as usize;
4136 Uint64::new_unchecked(self.0.slice(start..end))
4137 } else {
4138 Uint64::new_unchecked(self.0.slice(start..))
4139 }
4140 }
4141 pub fn as_reader<'r>(&'r self) -> Node2Reader<'r> {
4142 Node2Reader::new_unchecked(self.as_slice())
4143 }
4144}
4145impl molecule::prelude::Entity for Node2 {
4146 type Builder = Node2Builder;
4147 const NAME: &'static str = "Node2";
4148 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4149 Node2(data)
4150 }
4151 fn as_bytes(&self) -> molecule::bytes::Bytes {
4152 self.0.clone()
4153 }
4154 fn as_slice(&self) -> &[u8] {
4155 &self.0[..]
4156 }
4157 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4158 Node2Reader::from_slice(slice).map(|reader| reader.to_entity())
4159 }
4160 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4161 Node2Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4162 }
4163 fn new_builder() -> Self::Builder {
4164 ::core::default::Default::default()
4165 }
4166 fn as_builder(self) -> Self::Builder {
4167 Self::new_builder()
4168 .addresses(self.addresses())
4169 .flags(self.flags())
4170 }
4171}
4172#[derive(Clone, Copy)]
4173pub struct Node2Reader<'r>(&'r [u8]);
4174impl<'r> ::core::fmt::LowerHex for Node2Reader<'r> {
4175 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4176 use molecule::hex_string;
4177 if f.alternate() {
4178 write!(f, "0x")?;
4179 }
4180 write!(f, "{}", hex_string(self.as_slice()))
4181 }
4182}
4183impl<'r> ::core::fmt::Debug for Node2Reader<'r> {
4184 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4185 write!(f, "{}({:#x})", Self::NAME, self)
4186 }
4187}
4188impl<'r> ::core::fmt::Display for Node2Reader<'r> {
4189 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4190 write!(f, "{} {{ ", Self::NAME)?;
4191 write!(f, "{}: {}", "addresses", self.addresses())?;
4192 write!(f, ", {}: {}", "flags", self.flags())?;
4193 let extra_count = self.count_extra_fields();
4194 if extra_count != 0 {
4195 write!(f, ", .. ({} fields)", extra_count)?;
4196 }
4197 write!(f, " }}")
4198 }
4199}
4200impl<'r> Node2Reader<'r> {
4201 pub const FIELD_COUNT: usize = 2;
4202 pub fn total_size(&self) -> usize {
4203 molecule::unpack_number(self.as_slice()) as usize
4204 }
4205 pub fn field_count(&self) -> usize {
4206 if self.total_size() == molecule::NUMBER_SIZE {
4207 0
4208 } else {
4209 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4210 }
4211 }
4212 pub fn count_extra_fields(&self) -> usize {
4213 self.field_count() - Self::FIELD_COUNT
4214 }
4215 pub fn has_extra_fields(&self) -> bool {
4216 Self::FIELD_COUNT != self.field_count()
4217 }
4218 pub fn addresses(&self) -> BytesVecReader<'r> {
4219 let slice = self.as_slice();
4220 let start = molecule::unpack_number(&slice[4..]) as usize;
4221 let end = molecule::unpack_number(&slice[8..]) as usize;
4222 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
4223 }
4224 pub fn flags(&self) -> Uint64Reader<'r> {
4225 let slice = self.as_slice();
4226 let start = molecule::unpack_number(&slice[8..]) as usize;
4227 if self.has_extra_fields() {
4228 let end = molecule::unpack_number(&slice[12..]) as usize;
4229 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
4230 } else {
4231 Uint64Reader::new_unchecked(&self.as_slice()[start..])
4232 }
4233 }
4234}
4235impl<'r> molecule::prelude::Reader<'r> for Node2Reader<'r> {
4236 type Entity = Node2;
4237 const NAME: &'static str = "Node2Reader";
4238 fn to_entity(&self) -> Self::Entity {
4239 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4240 }
4241 fn new_unchecked(slice: &'r [u8]) -> Self {
4242 Node2Reader(slice)
4243 }
4244 fn as_slice(&self) -> &'r [u8] {
4245 self.0
4246 }
4247 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4248 use molecule::verification_error as ve;
4249 let slice_len = slice.len();
4250 if slice_len < molecule::NUMBER_SIZE {
4251 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4252 }
4253 let total_size = molecule::unpack_number(slice) as usize;
4254 if slice_len != total_size {
4255 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4256 }
4257 if slice_len < molecule::NUMBER_SIZE * 2 {
4258 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4259 }
4260 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4261 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4262 return ve!(Self, OffsetsNotMatch);
4263 }
4264 if slice_len < offset_first {
4265 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4266 }
4267 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4268 if field_count < Self::FIELD_COUNT {
4269 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4270 } else if !compatible && field_count > Self::FIELD_COUNT {
4271 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4272 };
4273 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4274 .chunks_exact(molecule::NUMBER_SIZE)
4275 .map(|x| molecule::unpack_number(x) as usize)
4276 .collect();
4277 offsets.push(total_size);
4278 if offsets.windows(2).any(|i| i[0] > i[1]) {
4279 return ve!(Self, OffsetsNotMatch);
4280 }
4281 BytesVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4282 Uint64Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
4283 Ok(())
4284 }
4285}
4286#[derive(Clone, Debug, Default)]
4287pub struct Node2Builder {
4288 pub(crate) addresses: BytesVec,
4289 pub(crate) flags: Uint64,
4290}
4291impl Node2Builder {
4292 pub const FIELD_COUNT: usize = 2;
4293 pub fn addresses<T>(mut self, v: T) -> Self
4294 where
4295 T: ::core::convert::Into<BytesVec>,
4296 {
4297 self.addresses = v.into();
4298 self
4299 }
4300 pub fn flags<T>(mut self, v: T) -> Self
4301 where
4302 T: ::core::convert::Into<Uint64>,
4303 {
4304 self.flags = v.into();
4305 self
4306 }
4307}
4308impl molecule::prelude::Builder for Node2Builder {
4309 type Entity = Node2;
4310 const NAME: &'static str = "Node2Builder";
4311 fn expected_length(&self) -> usize {
4312 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
4313 + self.addresses.as_slice().len()
4314 + self.flags.as_slice().len()
4315 }
4316 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4317 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4318 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4319 offsets.push(total_size);
4320 total_size += self.addresses.as_slice().len();
4321 offsets.push(total_size);
4322 total_size += self.flags.as_slice().len();
4323 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4324 for offset in offsets.into_iter() {
4325 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4326 }
4327 writer.write_all(self.addresses.as_slice())?;
4328 writer.write_all(self.flags.as_slice())?;
4329 Ok(())
4330 }
4331 fn build(&self) -> Self::Entity {
4332 let mut inner = Vec::with_capacity(self.expected_length());
4333 self.write(&mut inner)
4334 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4335 Node2::new_unchecked(inner.into())
4336 }
4337}
4338#[derive(Clone)]
4339pub struct AddressVec(molecule::bytes::Bytes);
4340impl ::core::fmt::LowerHex for AddressVec {
4341 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4342 use molecule::hex_string;
4343 if f.alternate() {
4344 write!(f, "0x")?;
4345 }
4346 write!(f, "{}", hex_string(self.as_slice()))
4347 }
4348}
4349impl ::core::fmt::Debug for AddressVec {
4350 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4351 write!(f, "{}({:#x})", Self::NAME, self)
4352 }
4353}
4354impl ::core::fmt::Display for AddressVec {
4355 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4356 write!(f, "{} [", Self::NAME)?;
4357 for i in 0..self.len() {
4358 if i == 0 {
4359 write!(f, "{}", self.get_unchecked(i))?;
4360 } else {
4361 write!(f, ", {}", self.get_unchecked(i))?;
4362 }
4363 }
4364 write!(f, "]")
4365 }
4366}
4367impl ::core::default::Default for AddressVec {
4368 fn default() -> Self {
4369 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4370 AddressVec::new_unchecked(v)
4371 }
4372}
4373impl AddressVec {
4374 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
4375 pub fn total_size(&self) -> usize {
4376 molecule::unpack_number(self.as_slice()) as usize
4377 }
4378 pub fn item_count(&self) -> usize {
4379 if self.total_size() == molecule::NUMBER_SIZE {
4380 0
4381 } else {
4382 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4383 }
4384 }
4385 pub fn len(&self) -> usize {
4386 self.item_count()
4387 }
4388 pub fn is_empty(&self) -> bool {
4389 self.len() == 0
4390 }
4391 pub fn get(&self, idx: usize) -> Option<Address> {
4392 if idx >= self.len() {
4393 None
4394 } else {
4395 Some(self.get_unchecked(idx))
4396 }
4397 }
4398 pub fn get_unchecked(&self, idx: usize) -> Address {
4399 let slice = self.as_slice();
4400 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4401 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4402 if idx == self.len() - 1 {
4403 Address::new_unchecked(self.0.slice(start..))
4404 } else {
4405 let end_idx = start_idx + molecule::NUMBER_SIZE;
4406 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4407 Address::new_unchecked(self.0.slice(start..end))
4408 }
4409 }
4410 pub fn as_reader<'r>(&'r self) -> AddressVecReader<'r> {
4411 AddressVecReader::new_unchecked(self.as_slice())
4412 }
4413}
4414impl molecule::prelude::Entity for AddressVec {
4415 type Builder = AddressVecBuilder;
4416 const NAME: &'static str = "AddressVec";
4417 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4418 AddressVec(data)
4419 }
4420 fn as_bytes(&self) -> molecule::bytes::Bytes {
4421 self.0.clone()
4422 }
4423 fn as_slice(&self) -> &[u8] {
4424 &self.0[..]
4425 }
4426 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4427 AddressVecReader::from_slice(slice).map(|reader| reader.to_entity())
4428 }
4429 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4430 AddressVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4431 }
4432 fn new_builder() -> Self::Builder {
4433 ::core::default::Default::default()
4434 }
4435 fn as_builder(self) -> Self::Builder {
4436 Self::new_builder().extend(self.into_iter())
4437 }
4438}
4439#[derive(Clone, Copy)]
4440pub struct AddressVecReader<'r>(&'r [u8]);
4441impl<'r> ::core::fmt::LowerHex for AddressVecReader<'r> {
4442 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4443 use molecule::hex_string;
4444 if f.alternate() {
4445 write!(f, "0x")?;
4446 }
4447 write!(f, "{}", hex_string(self.as_slice()))
4448 }
4449}
4450impl<'r> ::core::fmt::Debug for AddressVecReader<'r> {
4451 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4452 write!(f, "{}({:#x})", Self::NAME, self)
4453 }
4454}
4455impl<'r> ::core::fmt::Display for AddressVecReader<'r> {
4456 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4457 write!(f, "{} [", Self::NAME)?;
4458 for i in 0..self.len() {
4459 if i == 0 {
4460 write!(f, "{}", self.get_unchecked(i))?;
4461 } else {
4462 write!(f, ", {}", self.get_unchecked(i))?;
4463 }
4464 }
4465 write!(f, "]")
4466 }
4467}
4468impl<'r> AddressVecReader<'r> {
4469 pub fn total_size(&self) -> usize {
4470 molecule::unpack_number(self.as_slice()) as usize
4471 }
4472 pub fn item_count(&self) -> usize {
4473 if self.total_size() == molecule::NUMBER_SIZE {
4474 0
4475 } else {
4476 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4477 }
4478 }
4479 pub fn len(&self) -> usize {
4480 self.item_count()
4481 }
4482 pub fn is_empty(&self) -> bool {
4483 self.len() == 0
4484 }
4485 pub fn get(&self, idx: usize) -> Option<AddressReader<'r>> {
4486 if idx >= self.len() {
4487 None
4488 } else {
4489 Some(self.get_unchecked(idx))
4490 }
4491 }
4492 pub fn get_unchecked(&self, idx: usize) -> AddressReader<'r> {
4493 let slice = self.as_slice();
4494 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4495 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4496 if idx == self.len() - 1 {
4497 AddressReader::new_unchecked(&self.as_slice()[start..])
4498 } else {
4499 let end_idx = start_idx + molecule::NUMBER_SIZE;
4500 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4501 AddressReader::new_unchecked(&self.as_slice()[start..end])
4502 }
4503 }
4504}
4505impl<'r> molecule::prelude::Reader<'r> for AddressVecReader<'r> {
4506 type Entity = AddressVec;
4507 const NAME: &'static str = "AddressVecReader";
4508 fn to_entity(&self) -> Self::Entity {
4509 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4510 }
4511 fn new_unchecked(slice: &'r [u8]) -> Self {
4512 AddressVecReader(slice)
4513 }
4514 fn as_slice(&self) -> &'r [u8] {
4515 self.0
4516 }
4517 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4518 use molecule::verification_error as ve;
4519 let slice_len = slice.len();
4520 if slice_len < molecule::NUMBER_SIZE {
4521 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4522 }
4523 let total_size = molecule::unpack_number(slice) as usize;
4524 if slice_len != total_size {
4525 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4526 }
4527 if slice_len == molecule::NUMBER_SIZE {
4528 return Ok(());
4529 }
4530 if slice_len < molecule::NUMBER_SIZE * 2 {
4531 return ve!(
4532 Self,
4533 TotalSizeNotMatch,
4534 molecule::NUMBER_SIZE * 2,
4535 slice_len
4536 );
4537 }
4538 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4539 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4540 return ve!(Self, OffsetsNotMatch);
4541 }
4542 if slice_len < offset_first {
4543 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4544 }
4545 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4546 .chunks_exact(molecule::NUMBER_SIZE)
4547 .map(|x| molecule::unpack_number(x) as usize)
4548 .collect();
4549 offsets.push(total_size);
4550 if offsets.windows(2).any(|i| i[0] > i[1]) {
4551 return ve!(Self, OffsetsNotMatch);
4552 }
4553 for pair in offsets.windows(2) {
4554 let start = pair[0];
4555 let end = pair[1];
4556 AddressReader::verify(&slice[start..end], compatible)?;
4557 }
4558 Ok(())
4559 }
4560}
4561#[derive(Clone, Debug, Default)]
4562pub struct AddressVecBuilder(pub(crate) Vec<Address>);
4563impl AddressVecBuilder {
4564 pub fn set(mut self, v: Vec<Address>) -> Self {
4565 self.0 = v;
4566 self
4567 }
4568 pub fn push<T>(mut self, v: T) -> Self
4569 where
4570 T: ::core::convert::Into<Address>,
4571 {
4572 self.0.push(v.into());
4573 self
4574 }
4575 pub fn extend<T: ::core::iter::IntoIterator<Item = Address>>(mut self, iter: T) -> Self {
4576 self.0.extend(iter);
4577 self
4578 }
4579 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Address>
4580 where
4581 T: ::core::convert::Into<Address>,
4582 {
4583 self.0
4584 .get_mut(index)
4585 .map(|item| ::core::mem::replace(item, v.into()))
4586 }
4587}
4588impl molecule::prelude::Builder for AddressVecBuilder {
4589 type Entity = AddressVec;
4590 const NAME: &'static str = "AddressVecBuilder";
4591 fn expected_length(&self) -> usize {
4592 molecule::NUMBER_SIZE * (self.0.len() + 1)
4593 + self
4594 .0
4595 .iter()
4596 .map(|inner| inner.as_slice().len())
4597 .sum::<usize>()
4598 }
4599 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4600 let item_count = self.0.len();
4601 if item_count == 0 {
4602 writer.write_all(&molecule::pack_number(
4603 molecule::NUMBER_SIZE as molecule::Number,
4604 ))?;
4605 } else {
4606 let (total_size, offsets) = self.0.iter().fold(
4607 (
4608 molecule::NUMBER_SIZE * (item_count + 1),
4609 Vec::with_capacity(item_count),
4610 ),
4611 |(start, mut offsets), inner| {
4612 offsets.push(start);
4613 (start + inner.as_slice().len(), offsets)
4614 },
4615 );
4616 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4617 for offset in offsets.into_iter() {
4618 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4619 }
4620 for inner in self.0.iter() {
4621 writer.write_all(inner.as_slice())?;
4622 }
4623 }
4624 Ok(())
4625 }
4626 fn build(&self) -> Self::Entity {
4627 let mut inner = Vec::with_capacity(self.expected_length());
4628 self.write(&mut inner)
4629 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4630 AddressVec::new_unchecked(inner.into())
4631 }
4632}
4633pub struct AddressVecIterator(AddressVec, usize, usize);
4634impl ::core::iter::Iterator for AddressVecIterator {
4635 type Item = Address;
4636 fn next(&mut self) -> Option<Self::Item> {
4637 if self.1 >= self.2 {
4638 None
4639 } else {
4640 let ret = self.0.get_unchecked(self.1);
4641 self.1 += 1;
4642 Some(ret)
4643 }
4644 }
4645}
4646impl ::core::iter::ExactSizeIterator for AddressVecIterator {
4647 fn len(&self) -> usize {
4648 self.2 - self.1
4649 }
4650}
4651impl ::core::iter::IntoIterator for AddressVec {
4652 type Item = Address;
4653 type IntoIter = AddressVecIterator;
4654 fn into_iter(self) -> Self::IntoIter {
4655 let len = self.len();
4656 AddressVecIterator(self, 0, len)
4657 }
4658}
4659impl<'r> AddressVecReader<'r> {
4660 pub fn iter<'t>(&'t self) -> AddressVecReaderIterator<'t, 'r> {
4661 AddressVecReaderIterator(&self, 0, self.len())
4662 }
4663}
4664pub struct AddressVecReaderIterator<'t, 'r>(&'t AddressVecReader<'r>, usize, usize);
4665impl<'t: 'r, 'r> ::core::iter::Iterator for AddressVecReaderIterator<'t, 'r> {
4666 type Item = AddressReader<'t>;
4667 fn next(&mut self) -> Option<Self::Item> {
4668 if self.1 >= self.2 {
4669 None
4670 } else {
4671 let ret = self.0.get_unchecked(self.1);
4672 self.1 += 1;
4673 Some(ret)
4674 }
4675 }
4676}
4677impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for AddressVecReaderIterator<'t, 'r> {
4678 fn len(&self) -> usize {
4679 self.2 - self.1
4680 }
4681}
4682impl ::core::iter::FromIterator<Address> for AddressVec {
4683 fn from_iter<T: IntoIterator<Item = Address>>(iter: T) -> Self {
4684 Self::new_builder().extend(iter).build()
4685 }
4686}
4687impl From<Vec<Address>> for AddressVec {
4688 fn from(v: Vec<Address>) -> Self {
4689 Self::new_builder().set(v).build()
4690 }
4691}
4692#[derive(Clone)]
4693pub struct Address(molecule::bytes::Bytes);
4694impl ::core::fmt::LowerHex for Address {
4695 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4696 use molecule::hex_string;
4697 if f.alternate() {
4698 write!(f, "0x")?;
4699 }
4700 write!(f, "{}", hex_string(self.as_slice()))
4701 }
4702}
4703impl ::core::fmt::Debug for Address {
4704 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4705 write!(f, "{}({:#x})", Self::NAME, self)
4706 }
4707}
4708impl ::core::fmt::Display for Address {
4709 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4710 write!(f, "{} {{ ", Self::NAME)?;
4711 write!(f, "{}: {}", "bytes", self.bytes())?;
4712 let extra_count = self.count_extra_fields();
4713 if extra_count != 0 {
4714 write!(f, ", .. ({} fields)", extra_count)?;
4715 }
4716 write!(f, " }}")
4717 }
4718}
4719impl ::core::default::Default for Address {
4720 fn default() -> Self {
4721 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4722 Address::new_unchecked(v)
4723 }
4724}
4725impl Address {
4726 const DEFAULT_VALUE: [u8; 12] = [12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0];
4727 pub const FIELD_COUNT: usize = 1;
4728 pub fn total_size(&self) -> usize {
4729 molecule::unpack_number(self.as_slice()) as usize
4730 }
4731 pub fn field_count(&self) -> usize {
4732 if self.total_size() == molecule::NUMBER_SIZE {
4733 0
4734 } else {
4735 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4736 }
4737 }
4738 pub fn count_extra_fields(&self) -> usize {
4739 self.field_count() - Self::FIELD_COUNT
4740 }
4741 pub fn has_extra_fields(&self) -> bool {
4742 Self::FIELD_COUNT != self.field_count()
4743 }
4744 pub fn bytes(&self) -> Bytes {
4745 let slice = self.as_slice();
4746 let start = molecule::unpack_number(&slice[4..]) as usize;
4747 if self.has_extra_fields() {
4748 let end = molecule::unpack_number(&slice[8..]) as usize;
4749 Bytes::new_unchecked(self.0.slice(start..end))
4750 } else {
4751 Bytes::new_unchecked(self.0.slice(start..))
4752 }
4753 }
4754 pub fn as_reader<'r>(&'r self) -> AddressReader<'r> {
4755 AddressReader::new_unchecked(self.as_slice())
4756 }
4757}
4758impl molecule::prelude::Entity for Address {
4759 type Builder = AddressBuilder;
4760 const NAME: &'static str = "Address";
4761 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4762 Address(data)
4763 }
4764 fn as_bytes(&self) -> molecule::bytes::Bytes {
4765 self.0.clone()
4766 }
4767 fn as_slice(&self) -> &[u8] {
4768 &self.0[..]
4769 }
4770 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4771 AddressReader::from_slice(slice).map(|reader| reader.to_entity())
4772 }
4773 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4774 AddressReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4775 }
4776 fn new_builder() -> Self::Builder {
4777 ::core::default::Default::default()
4778 }
4779 fn as_builder(self) -> Self::Builder {
4780 Self::new_builder().bytes(self.bytes())
4781 }
4782}
4783#[derive(Clone, Copy)]
4784pub struct AddressReader<'r>(&'r [u8]);
4785impl<'r> ::core::fmt::LowerHex for AddressReader<'r> {
4786 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4787 use molecule::hex_string;
4788 if f.alternate() {
4789 write!(f, "0x")?;
4790 }
4791 write!(f, "{}", hex_string(self.as_slice()))
4792 }
4793}
4794impl<'r> ::core::fmt::Debug for AddressReader<'r> {
4795 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4796 write!(f, "{}({:#x})", Self::NAME, self)
4797 }
4798}
4799impl<'r> ::core::fmt::Display for AddressReader<'r> {
4800 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4801 write!(f, "{} {{ ", Self::NAME)?;
4802 write!(f, "{}: {}", "bytes", self.bytes())?;
4803 let extra_count = self.count_extra_fields();
4804 if extra_count != 0 {
4805 write!(f, ", .. ({} fields)", extra_count)?;
4806 }
4807 write!(f, " }}")
4808 }
4809}
4810impl<'r> AddressReader<'r> {
4811 pub const FIELD_COUNT: usize = 1;
4812 pub fn total_size(&self) -> usize {
4813 molecule::unpack_number(self.as_slice()) as usize
4814 }
4815 pub fn field_count(&self) -> usize {
4816 if self.total_size() == molecule::NUMBER_SIZE {
4817 0
4818 } else {
4819 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4820 }
4821 }
4822 pub fn count_extra_fields(&self) -> usize {
4823 self.field_count() - Self::FIELD_COUNT
4824 }
4825 pub fn has_extra_fields(&self) -> bool {
4826 Self::FIELD_COUNT != self.field_count()
4827 }
4828 pub fn bytes(&self) -> BytesReader<'r> {
4829 let slice = self.as_slice();
4830 let start = molecule::unpack_number(&slice[4..]) as usize;
4831 if self.has_extra_fields() {
4832 let end = molecule::unpack_number(&slice[8..]) as usize;
4833 BytesReader::new_unchecked(&self.as_slice()[start..end])
4834 } else {
4835 BytesReader::new_unchecked(&self.as_slice()[start..])
4836 }
4837 }
4838}
4839impl<'r> molecule::prelude::Reader<'r> for AddressReader<'r> {
4840 type Entity = Address;
4841 const NAME: &'static str = "AddressReader";
4842 fn to_entity(&self) -> Self::Entity {
4843 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4844 }
4845 fn new_unchecked(slice: &'r [u8]) -> Self {
4846 AddressReader(slice)
4847 }
4848 fn as_slice(&self) -> &'r [u8] {
4849 self.0
4850 }
4851 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4852 use molecule::verification_error as ve;
4853 let slice_len = slice.len();
4854 if slice_len < molecule::NUMBER_SIZE {
4855 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4856 }
4857 let total_size = molecule::unpack_number(slice) as usize;
4858 if slice_len != total_size {
4859 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4860 }
4861 if slice_len < molecule::NUMBER_SIZE * 2 {
4862 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
4863 }
4864 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4865 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4866 return ve!(Self, OffsetsNotMatch);
4867 }
4868 if slice_len < offset_first {
4869 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4870 }
4871 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
4872 if field_count < Self::FIELD_COUNT {
4873 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4874 } else if !compatible && field_count > Self::FIELD_COUNT {
4875 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
4876 };
4877 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4878 .chunks_exact(molecule::NUMBER_SIZE)
4879 .map(|x| molecule::unpack_number(x) as usize)
4880 .collect();
4881 offsets.push(total_size);
4882 if offsets.windows(2).any(|i| i[0] > i[1]) {
4883 return ve!(Self, OffsetsNotMatch);
4884 }
4885 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
4886 Ok(())
4887 }
4888}
4889#[derive(Clone, Debug, Default)]
4890pub struct AddressBuilder {
4891 pub(crate) bytes: Bytes,
4892}
4893impl AddressBuilder {
4894 pub const FIELD_COUNT: usize = 1;
4895 pub fn bytes<T>(mut self, v: T) -> Self
4896 where
4897 T: ::core::convert::Into<Bytes>,
4898 {
4899 self.bytes = v.into();
4900 self
4901 }
4902}
4903impl molecule::prelude::Builder for AddressBuilder {
4904 type Entity = Address;
4905 const NAME: &'static str = "AddressBuilder";
4906 fn expected_length(&self) -> usize {
4907 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.bytes.as_slice().len()
4908 }
4909 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4910 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
4911 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
4912 offsets.push(total_size);
4913 total_size += self.bytes.as_slice().len();
4914 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4915 for offset in offsets.into_iter() {
4916 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4917 }
4918 writer.write_all(self.bytes.as_slice())?;
4919 Ok(())
4920 }
4921 fn build(&self) -> Self::Entity {
4922 let mut inner = Vec::with_capacity(self.expected_length());
4923 self.write(&mut inner)
4924 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4925 Address::new_unchecked(inner.into())
4926 }
4927}
4928#[derive(Clone)]
4929pub struct IdentifyMessage(molecule::bytes::Bytes);
4930impl ::core::fmt::LowerHex for IdentifyMessage {
4931 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4932 use molecule::hex_string;
4933 if f.alternate() {
4934 write!(f, "0x")?;
4935 }
4936 write!(f, "{}", hex_string(self.as_slice()))
4937 }
4938}
4939impl ::core::fmt::Debug for IdentifyMessage {
4940 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4941 write!(f, "{}({:#x})", Self::NAME, self)
4942 }
4943}
4944impl ::core::fmt::Display for IdentifyMessage {
4945 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4946 write!(f, "{} {{ ", Self::NAME)?;
4947 write!(f, "{}: {}", "listen_addrs", self.listen_addrs())?;
4948 write!(f, ", {}: {}", "observed_addr", self.observed_addr())?;
4949 write!(f, ", {}: {}", "identify", self.identify())?;
4950 let extra_count = self.count_extra_fields();
4951 if extra_count != 0 {
4952 write!(f, ", .. ({} fields)", extra_count)?;
4953 }
4954 write!(f, " }}")
4955 }
4956}
4957impl ::core::default::Default for IdentifyMessage {
4958 fn default() -> Self {
4959 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4960 IdentifyMessage::new_unchecked(v)
4961 }
4962}
4963impl IdentifyMessage {
4964 const DEFAULT_VALUE: [u8; 36] = [
4965 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,
4966 0, 0, 0, 0, 0, 0, 0,
4967 ];
4968 pub const FIELD_COUNT: usize = 3;
4969 pub fn total_size(&self) -> usize {
4970 molecule::unpack_number(self.as_slice()) as usize
4971 }
4972 pub fn field_count(&self) -> usize {
4973 if self.total_size() == molecule::NUMBER_SIZE {
4974 0
4975 } else {
4976 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4977 }
4978 }
4979 pub fn count_extra_fields(&self) -> usize {
4980 self.field_count() - Self::FIELD_COUNT
4981 }
4982 pub fn has_extra_fields(&self) -> bool {
4983 Self::FIELD_COUNT != self.field_count()
4984 }
4985 pub fn listen_addrs(&self) -> AddressVec {
4986 let slice = self.as_slice();
4987 let start = molecule::unpack_number(&slice[4..]) as usize;
4988 let end = molecule::unpack_number(&slice[8..]) as usize;
4989 AddressVec::new_unchecked(self.0.slice(start..end))
4990 }
4991 pub fn observed_addr(&self) -> Address {
4992 let slice = self.as_slice();
4993 let start = molecule::unpack_number(&slice[8..]) as usize;
4994 let end = molecule::unpack_number(&slice[12..]) as usize;
4995 Address::new_unchecked(self.0.slice(start..end))
4996 }
4997 pub fn identify(&self) -> Bytes {
4998 let slice = self.as_slice();
4999 let start = molecule::unpack_number(&slice[12..]) as usize;
5000 if self.has_extra_fields() {
5001 let end = molecule::unpack_number(&slice[16..]) as usize;
5002 Bytes::new_unchecked(self.0.slice(start..end))
5003 } else {
5004 Bytes::new_unchecked(self.0.slice(start..))
5005 }
5006 }
5007 pub fn as_reader<'r>(&'r self) -> IdentifyMessageReader<'r> {
5008 IdentifyMessageReader::new_unchecked(self.as_slice())
5009 }
5010}
5011impl molecule::prelude::Entity for IdentifyMessage {
5012 type Builder = IdentifyMessageBuilder;
5013 const NAME: &'static str = "IdentifyMessage";
5014 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5015 IdentifyMessage(data)
5016 }
5017 fn as_bytes(&self) -> molecule::bytes::Bytes {
5018 self.0.clone()
5019 }
5020 fn as_slice(&self) -> &[u8] {
5021 &self.0[..]
5022 }
5023 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5024 IdentifyMessageReader::from_slice(slice).map(|reader| reader.to_entity())
5025 }
5026 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5027 IdentifyMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5028 }
5029 fn new_builder() -> Self::Builder {
5030 ::core::default::Default::default()
5031 }
5032 fn as_builder(self) -> Self::Builder {
5033 Self::new_builder()
5034 .listen_addrs(self.listen_addrs())
5035 .observed_addr(self.observed_addr())
5036 .identify(self.identify())
5037 }
5038}
5039#[derive(Clone, Copy)]
5040pub struct IdentifyMessageReader<'r>(&'r [u8]);
5041impl<'r> ::core::fmt::LowerHex for IdentifyMessageReader<'r> {
5042 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5043 use molecule::hex_string;
5044 if f.alternate() {
5045 write!(f, "0x")?;
5046 }
5047 write!(f, "{}", hex_string(self.as_slice()))
5048 }
5049}
5050impl<'r> ::core::fmt::Debug for IdentifyMessageReader<'r> {
5051 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5052 write!(f, "{}({:#x})", Self::NAME, self)
5053 }
5054}
5055impl<'r> ::core::fmt::Display for IdentifyMessageReader<'r> {
5056 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5057 write!(f, "{} {{ ", Self::NAME)?;
5058 write!(f, "{}: {}", "listen_addrs", self.listen_addrs())?;
5059 write!(f, ", {}: {}", "observed_addr", self.observed_addr())?;
5060 write!(f, ", {}: {}", "identify", self.identify())?;
5061 let extra_count = self.count_extra_fields();
5062 if extra_count != 0 {
5063 write!(f, ", .. ({} fields)", extra_count)?;
5064 }
5065 write!(f, " }}")
5066 }
5067}
5068impl<'r> IdentifyMessageReader<'r> {
5069 pub const FIELD_COUNT: usize = 3;
5070 pub fn total_size(&self) -> usize {
5071 molecule::unpack_number(self.as_slice()) as usize
5072 }
5073 pub fn field_count(&self) -> usize {
5074 if self.total_size() == molecule::NUMBER_SIZE {
5075 0
5076 } else {
5077 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5078 }
5079 }
5080 pub fn count_extra_fields(&self) -> usize {
5081 self.field_count() - Self::FIELD_COUNT
5082 }
5083 pub fn has_extra_fields(&self) -> bool {
5084 Self::FIELD_COUNT != self.field_count()
5085 }
5086 pub fn listen_addrs(&self) -> AddressVecReader<'r> {
5087 let slice = self.as_slice();
5088 let start = molecule::unpack_number(&slice[4..]) as usize;
5089 let end = molecule::unpack_number(&slice[8..]) as usize;
5090 AddressVecReader::new_unchecked(&self.as_slice()[start..end])
5091 }
5092 pub fn observed_addr(&self) -> AddressReader<'r> {
5093 let slice = self.as_slice();
5094 let start = molecule::unpack_number(&slice[8..]) as usize;
5095 let end = molecule::unpack_number(&slice[12..]) as usize;
5096 AddressReader::new_unchecked(&self.as_slice()[start..end])
5097 }
5098 pub fn identify(&self) -> BytesReader<'r> {
5099 let slice = self.as_slice();
5100 let start = molecule::unpack_number(&slice[12..]) as usize;
5101 if self.has_extra_fields() {
5102 let end = molecule::unpack_number(&slice[16..]) as usize;
5103 BytesReader::new_unchecked(&self.as_slice()[start..end])
5104 } else {
5105 BytesReader::new_unchecked(&self.as_slice()[start..])
5106 }
5107 }
5108}
5109impl<'r> molecule::prelude::Reader<'r> for IdentifyMessageReader<'r> {
5110 type Entity = IdentifyMessage;
5111 const NAME: &'static str = "IdentifyMessageReader";
5112 fn to_entity(&self) -> Self::Entity {
5113 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5114 }
5115 fn new_unchecked(slice: &'r [u8]) -> Self {
5116 IdentifyMessageReader(slice)
5117 }
5118 fn as_slice(&self) -> &'r [u8] {
5119 self.0
5120 }
5121 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5122 use molecule::verification_error as ve;
5123 let slice_len = slice.len();
5124 if slice_len < molecule::NUMBER_SIZE {
5125 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5126 }
5127 let total_size = molecule::unpack_number(slice) as usize;
5128 if slice_len != total_size {
5129 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5130 }
5131 if slice_len < molecule::NUMBER_SIZE * 2 {
5132 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5133 }
5134 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5135 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5136 return ve!(Self, OffsetsNotMatch);
5137 }
5138 if slice_len < offset_first {
5139 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5140 }
5141 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5142 if field_count < Self::FIELD_COUNT {
5143 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5144 } else if !compatible && field_count > Self::FIELD_COUNT {
5145 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5146 };
5147 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5148 .chunks_exact(molecule::NUMBER_SIZE)
5149 .map(|x| molecule::unpack_number(x) as usize)
5150 .collect();
5151 offsets.push(total_size);
5152 if offsets.windows(2).any(|i| i[0] > i[1]) {
5153 return ve!(Self, OffsetsNotMatch);
5154 }
5155 AddressVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5156 AddressReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5157 BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5158 Ok(())
5159 }
5160}
5161#[derive(Clone, Debug, Default)]
5162pub struct IdentifyMessageBuilder {
5163 pub(crate) listen_addrs: AddressVec,
5164 pub(crate) observed_addr: Address,
5165 pub(crate) identify: Bytes,
5166}
5167impl IdentifyMessageBuilder {
5168 pub const FIELD_COUNT: usize = 3;
5169 pub fn listen_addrs<T>(mut self, v: T) -> Self
5170 where
5171 T: ::core::convert::Into<AddressVec>,
5172 {
5173 self.listen_addrs = v.into();
5174 self
5175 }
5176 pub fn observed_addr<T>(mut self, v: T) -> Self
5177 where
5178 T: ::core::convert::Into<Address>,
5179 {
5180 self.observed_addr = v.into();
5181 self
5182 }
5183 pub fn identify<T>(mut self, v: T) -> Self
5184 where
5185 T: ::core::convert::Into<Bytes>,
5186 {
5187 self.identify = v.into();
5188 self
5189 }
5190}
5191impl molecule::prelude::Builder for IdentifyMessageBuilder {
5192 type Entity = IdentifyMessage;
5193 const NAME: &'static str = "IdentifyMessageBuilder";
5194 fn expected_length(&self) -> usize {
5195 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5196 + self.listen_addrs.as_slice().len()
5197 + self.observed_addr.as_slice().len()
5198 + self.identify.as_slice().len()
5199 }
5200 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5201 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5202 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5203 offsets.push(total_size);
5204 total_size += self.listen_addrs.as_slice().len();
5205 offsets.push(total_size);
5206 total_size += self.observed_addr.as_slice().len();
5207 offsets.push(total_size);
5208 total_size += self.identify.as_slice().len();
5209 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5210 for offset in offsets.into_iter() {
5211 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5212 }
5213 writer.write_all(self.listen_addrs.as_slice())?;
5214 writer.write_all(self.observed_addr.as_slice())?;
5215 writer.write_all(self.identify.as_slice())?;
5216 Ok(())
5217 }
5218 fn build(&self) -> Self::Entity {
5219 let mut inner = Vec::with_capacity(self.expected_length());
5220 self.write(&mut inner)
5221 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5222 IdentifyMessage::new_unchecked(inner.into())
5223 }
5224}
5225#[derive(Clone)]
5226pub struct HolePunchingMessage(molecule::bytes::Bytes);
5227impl ::core::fmt::LowerHex for HolePunchingMessage {
5228 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5229 use molecule::hex_string;
5230 if f.alternate() {
5231 write!(f, "0x")?;
5232 }
5233 write!(f, "{}", hex_string(self.as_slice()))
5234 }
5235}
5236impl ::core::fmt::Debug for HolePunchingMessage {
5237 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5238 write!(f, "{}({:#x})", Self::NAME, self)
5239 }
5240}
5241impl ::core::fmt::Display for HolePunchingMessage {
5242 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5243 write!(f, "{}(", Self::NAME)?;
5244 self.to_enum().display_inner(f)?;
5245 write!(f, ")")
5246 }
5247}
5248impl ::core::default::Default for HolePunchingMessage {
5249 fn default() -> Self {
5250 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5251 HolePunchingMessage::new_unchecked(v)
5252 }
5253}
5254impl HolePunchingMessage {
5255 const DEFAULT_VALUE: [u8; 45] = [
5256 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,
5257 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
5258 ];
5259 pub const ITEMS_COUNT: usize = 3;
5260 pub fn item_id(&self) -> molecule::Number {
5261 molecule::unpack_number(self.as_slice())
5262 }
5263 pub fn to_enum(&self) -> HolePunchingMessageUnion {
5264 let inner = self.0.slice(molecule::NUMBER_SIZE..);
5265 match self.item_id() {
5266 0 => ConnectionRequest::new_unchecked(inner).into(),
5267 1 => ConnectionRequestDelivered::new_unchecked(inner).into(),
5268 2 => ConnectionSync::new_unchecked(inner).into(),
5269 _ => panic!("{}: invalid data", Self::NAME),
5270 }
5271 }
5272 pub fn as_reader<'r>(&'r self) -> HolePunchingMessageReader<'r> {
5273 HolePunchingMessageReader::new_unchecked(self.as_slice())
5274 }
5275}
5276impl molecule::prelude::Entity for HolePunchingMessage {
5277 type Builder = HolePunchingMessageBuilder;
5278 const NAME: &'static str = "HolePunchingMessage";
5279 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5280 HolePunchingMessage(data)
5281 }
5282 fn as_bytes(&self) -> molecule::bytes::Bytes {
5283 self.0.clone()
5284 }
5285 fn as_slice(&self) -> &[u8] {
5286 &self.0[..]
5287 }
5288 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5289 HolePunchingMessageReader::from_slice(slice).map(|reader| reader.to_entity())
5290 }
5291 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5292 HolePunchingMessageReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5293 }
5294 fn new_builder() -> Self::Builder {
5295 ::core::default::Default::default()
5296 }
5297 fn as_builder(self) -> Self::Builder {
5298 Self::new_builder().set(self.to_enum())
5299 }
5300}
5301#[derive(Clone, Copy)]
5302pub struct HolePunchingMessageReader<'r>(&'r [u8]);
5303impl<'r> ::core::fmt::LowerHex for HolePunchingMessageReader<'r> {
5304 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5305 use molecule::hex_string;
5306 if f.alternate() {
5307 write!(f, "0x")?;
5308 }
5309 write!(f, "{}", hex_string(self.as_slice()))
5310 }
5311}
5312impl<'r> ::core::fmt::Debug for HolePunchingMessageReader<'r> {
5313 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5314 write!(f, "{}({:#x})", Self::NAME, self)
5315 }
5316}
5317impl<'r> ::core::fmt::Display for HolePunchingMessageReader<'r> {
5318 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5319 write!(f, "{}(", Self::NAME)?;
5320 self.to_enum().display_inner(f)?;
5321 write!(f, ")")
5322 }
5323}
5324impl<'r> HolePunchingMessageReader<'r> {
5325 pub const ITEMS_COUNT: usize = 3;
5326 pub fn item_id(&self) -> molecule::Number {
5327 molecule::unpack_number(self.as_slice())
5328 }
5329 pub fn to_enum(&self) -> HolePunchingMessageUnionReader<'r> {
5330 let inner = &self.as_slice()[molecule::NUMBER_SIZE..];
5331 match self.item_id() {
5332 0 => ConnectionRequestReader::new_unchecked(inner).into(),
5333 1 => ConnectionRequestDeliveredReader::new_unchecked(inner).into(),
5334 2 => ConnectionSyncReader::new_unchecked(inner).into(),
5335 _ => panic!("{}: invalid data", Self::NAME),
5336 }
5337 }
5338}
5339impl<'r> molecule::prelude::Reader<'r> for HolePunchingMessageReader<'r> {
5340 type Entity = HolePunchingMessage;
5341 const NAME: &'static str = "HolePunchingMessageReader";
5342 fn to_entity(&self) -> Self::Entity {
5343 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5344 }
5345 fn new_unchecked(slice: &'r [u8]) -> Self {
5346 HolePunchingMessageReader(slice)
5347 }
5348 fn as_slice(&self) -> &'r [u8] {
5349 self.0
5350 }
5351 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5352 use molecule::verification_error as ve;
5353 let slice_len = slice.len();
5354 if slice_len < molecule::NUMBER_SIZE {
5355 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5356 }
5357 let item_id = molecule::unpack_number(slice);
5358 let inner_slice = &slice[molecule::NUMBER_SIZE..];
5359 match item_id {
5360 0 => ConnectionRequestReader::verify(inner_slice, compatible),
5361 1 => ConnectionRequestDeliveredReader::verify(inner_slice, compatible),
5362 2 => ConnectionSyncReader::verify(inner_slice, compatible),
5363 _ => ve!(Self, UnknownItem, Self::ITEMS_COUNT, item_id),
5364 }?;
5365 Ok(())
5366 }
5367}
5368#[derive(Clone, Debug, Default)]
5369pub struct HolePunchingMessageBuilder(pub(crate) HolePunchingMessageUnion);
5370impl HolePunchingMessageBuilder {
5371 pub const ITEMS_COUNT: usize = 3;
5372 pub fn set<I>(mut self, v: I) -> Self
5373 where
5374 I: ::core::convert::Into<HolePunchingMessageUnion>,
5375 {
5376 self.0 = v.into();
5377 self
5378 }
5379}
5380impl molecule::prelude::Builder for HolePunchingMessageBuilder {
5381 type Entity = HolePunchingMessage;
5382 const NAME: &'static str = "HolePunchingMessageBuilder";
5383 fn expected_length(&self) -> usize {
5384 molecule::NUMBER_SIZE + self.0.as_slice().len()
5385 }
5386 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5387 writer.write_all(&molecule::pack_number(self.0.item_id()))?;
5388 writer.write_all(self.0.as_slice())
5389 }
5390 fn build(&self) -> Self::Entity {
5391 let mut inner = Vec::with_capacity(self.expected_length());
5392 self.write(&mut inner)
5393 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5394 HolePunchingMessage::new_unchecked(inner.into())
5395 }
5396}
5397#[derive(Debug, Clone)]
5398pub enum HolePunchingMessageUnion {
5399 ConnectionRequest(ConnectionRequest),
5400 ConnectionRequestDelivered(ConnectionRequestDelivered),
5401 ConnectionSync(ConnectionSync),
5402}
5403#[derive(Debug, Clone, Copy)]
5404pub enum HolePunchingMessageUnionReader<'r> {
5405 ConnectionRequest(ConnectionRequestReader<'r>),
5406 ConnectionRequestDelivered(ConnectionRequestDeliveredReader<'r>),
5407 ConnectionSync(ConnectionSyncReader<'r>),
5408}
5409impl ::core::default::Default for HolePunchingMessageUnion {
5410 fn default() -> Self {
5411 HolePunchingMessageUnion::ConnectionRequest(::core::default::Default::default())
5412 }
5413}
5414impl ::core::fmt::Display for HolePunchingMessageUnion {
5415 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5416 match self {
5417 HolePunchingMessageUnion::ConnectionRequest(ref item) => {
5418 write!(f, "{}::{}({})", Self::NAME, ConnectionRequest::NAME, item)
5419 }
5420 HolePunchingMessageUnion::ConnectionRequestDelivered(ref item) => {
5421 write!(
5422 f,
5423 "{}::{}({})",
5424 Self::NAME,
5425 ConnectionRequestDelivered::NAME,
5426 item
5427 )
5428 }
5429 HolePunchingMessageUnion::ConnectionSync(ref item) => {
5430 write!(f, "{}::{}({})", Self::NAME, ConnectionSync::NAME, item)
5431 }
5432 }
5433 }
5434}
5435impl<'r> ::core::fmt::Display for HolePunchingMessageUnionReader<'r> {
5436 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5437 match self {
5438 HolePunchingMessageUnionReader::ConnectionRequest(ref item) => {
5439 write!(f, "{}::{}({})", Self::NAME, ConnectionRequest::NAME, item)
5440 }
5441 HolePunchingMessageUnionReader::ConnectionRequestDelivered(ref item) => {
5442 write!(
5443 f,
5444 "{}::{}({})",
5445 Self::NAME,
5446 ConnectionRequestDelivered::NAME,
5447 item
5448 )
5449 }
5450 HolePunchingMessageUnionReader::ConnectionSync(ref item) => {
5451 write!(f, "{}::{}({})", Self::NAME, ConnectionSync::NAME, item)
5452 }
5453 }
5454 }
5455}
5456impl HolePunchingMessageUnion {
5457 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5458 match self {
5459 HolePunchingMessageUnion::ConnectionRequest(ref item) => write!(f, "{}", item),
5460 HolePunchingMessageUnion::ConnectionRequestDelivered(ref item) => write!(f, "{}", item),
5461 HolePunchingMessageUnion::ConnectionSync(ref item) => write!(f, "{}", item),
5462 }
5463 }
5464}
5465impl<'r> HolePunchingMessageUnionReader<'r> {
5466 pub(crate) fn display_inner(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5467 match self {
5468 HolePunchingMessageUnionReader::ConnectionRequest(ref item) => write!(f, "{}", item),
5469 HolePunchingMessageUnionReader::ConnectionRequestDelivered(ref item) => {
5470 write!(f, "{}", item)
5471 }
5472 HolePunchingMessageUnionReader::ConnectionSync(ref item) => write!(f, "{}", item),
5473 }
5474 }
5475}
5476impl ::core::convert::From<ConnectionRequest> for HolePunchingMessageUnion {
5477 fn from(item: ConnectionRequest) -> Self {
5478 HolePunchingMessageUnion::ConnectionRequest(item)
5479 }
5480}
5481impl ::core::convert::From<ConnectionRequestDelivered> for HolePunchingMessageUnion {
5482 fn from(item: ConnectionRequestDelivered) -> Self {
5483 HolePunchingMessageUnion::ConnectionRequestDelivered(item)
5484 }
5485}
5486impl ::core::convert::From<ConnectionSync> for HolePunchingMessageUnion {
5487 fn from(item: ConnectionSync) -> Self {
5488 HolePunchingMessageUnion::ConnectionSync(item)
5489 }
5490}
5491impl<'r> ::core::convert::From<ConnectionRequestReader<'r>> for HolePunchingMessageUnionReader<'r> {
5492 fn from(item: ConnectionRequestReader<'r>) -> Self {
5493 HolePunchingMessageUnionReader::ConnectionRequest(item)
5494 }
5495}
5496impl<'r> ::core::convert::From<ConnectionRequestDeliveredReader<'r>>
5497 for HolePunchingMessageUnionReader<'r>
5498{
5499 fn from(item: ConnectionRequestDeliveredReader<'r>) -> Self {
5500 HolePunchingMessageUnionReader::ConnectionRequestDelivered(item)
5501 }
5502}
5503impl<'r> ::core::convert::From<ConnectionSyncReader<'r>> for HolePunchingMessageUnionReader<'r> {
5504 fn from(item: ConnectionSyncReader<'r>) -> Self {
5505 HolePunchingMessageUnionReader::ConnectionSync(item)
5506 }
5507}
5508impl HolePunchingMessageUnion {
5509 pub const NAME: &'static str = "HolePunchingMessageUnion";
5510 pub fn as_bytes(&self) -> molecule::bytes::Bytes {
5511 match self {
5512 HolePunchingMessageUnion::ConnectionRequest(item) => item.as_bytes(),
5513 HolePunchingMessageUnion::ConnectionRequestDelivered(item) => item.as_bytes(),
5514 HolePunchingMessageUnion::ConnectionSync(item) => item.as_bytes(),
5515 }
5516 }
5517 pub fn as_slice(&self) -> &[u8] {
5518 match self {
5519 HolePunchingMessageUnion::ConnectionRequest(item) => item.as_slice(),
5520 HolePunchingMessageUnion::ConnectionRequestDelivered(item) => item.as_slice(),
5521 HolePunchingMessageUnion::ConnectionSync(item) => item.as_slice(),
5522 }
5523 }
5524 pub fn item_id(&self) -> molecule::Number {
5525 match self {
5526 HolePunchingMessageUnion::ConnectionRequest(_) => 0,
5527 HolePunchingMessageUnion::ConnectionRequestDelivered(_) => 1,
5528 HolePunchingMessageUnion::ConnectionSync(_) => 2,
5529 }
5530 }
5531 pub fn item_name(&self) -> &str {
5532 match self {
5533 HolePunchingMessageUnion::ConnectionRequest(_) => "ConnectionRequest",
5534 HolePunchingMessageUnion::ConnectionRequestDelivered(_) => "ConnectionRequestDelivered",
5535 HolePunchingMessageUnion::ConnectionSync(_) => "ConnectionSync",
5536 }
5537 }
5538 pub fn as_reader<'r>(&'r self) -> HolePunchingMessageUnionReader<'r> {
5539 match self {
5540 HolePunchingMessageUnion::ConnectionRequest(item) => item.as_reader().into(),
5541 HolePunchingMessageUnion::ConnectionRequestDelivered(item) => item.as_reader().into(),
5542 HolePunchingMessageUnion::ConnectionSync(item) => item.as_reader().into(),
5543 }
5544 }
5545}
5546impl<'r> HolePunchingMessageUnionReader<'r> {
5547 pub const NAME: &'r str = "HolePunchingMessageUnionReader";
5548 pub fn as_slice(&self) -> &'r [u8] {
5549 match self {
5550 HolePunchingMessageUnionReader::ConnectionRequest(item) => item.as_slice(),
5551 HolePunchingMessageUnionReader::ConnectionRequestDelivered(item) => item.as_slice(),
5552 HolePunchingMessageUnionReader::ConnectionSync(item) => item.as_slice(),
5553 }
5554 }
5555 pub fn item_id(&self) -> molecule::Number {
5556 match self {
5557 HolePunchingMessageUnionReader::ConnectionRequest(_) => 0,
5558 HolePunchingMessageUnionReader::ConnectionRequestDelivered(_) => 1,
5559 HolePunchingMessageUnionReader::ConnectionSync(_) => 2,
5560 }
5561 }
5562 pub fn item_name(&self) -> &str {
5563 match self {
5564 HolePunchingMessageUnionReader::ConnectionRequest(_) => "ConnectionRequest",
5565 HolePunchingMessageUnionReader::ConnectionRequestDelivered(_) => {
5566 "ConnectionRequestDelivered"
5567 }
5568 HolePunchingMessageUnionReader::ConnectionSync(_) => "ConnectionSync",
5569 }
5570 }
5571}
5572impl From<ConnectionRequest> for HolePunchingMessage {
5573 fn from(value: ConnectionRequest) -> Self {
5574 Self::new_builder().set(value).build()
5575 }
5576}
5577impl From<ConnectionRequestDelivered> for HolePunchingMessage {
5578 fn from(value: ConnectionRequestDelivered) -> Self {
5579 Self::new_builder().set(value).build()
5580 }
5581}
5582impl From<ConnectionSync> for HolePunchingMessage {
5583 fn from(value: ConnectionSync) -> Self {
5584 Self::new_builder().set(value).build()
5585 }
5586}
5587#[derive(Clone)]
5588pub struct ConnectionRequest(molecule::bytes::Bytes);
5589impl ::core::fmt::LowerHex for ConnectionRequest {
5590 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5591 use molecule::hex_string;
5592 if f.alternate() {
5593 write!(f, "0x")?;
5594 }
5595 write!(f, "{}", hex_string(self.as_slice()))
5596 }
5597}
5598impl ::core::fmt::Debug for ConnectionRequest {
5599 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5600 write!(f, "{}({:#x})", Self::NAME, self)
5601 }
5602}
5603impl ::core::fmt::Display for ConnectionRequest {
5604 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5605 write!(f, "{} {{ ", Self::NAME)?;
5606 write!(f, "{}: {}", "from", self.from())?;
5607 write!(f, ", {}: {}", "to", self.to())?;
5608 write!(f, ", {}: {}", "max_hops", self.max_hops())?;
5609 write!(f, ", {}: {}", "route", self.route())?;
5610 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
5611 let extra_count = self.count_extra_fields();
5612 if extra_count != 0 {
5613 write!(f, ", .. ({} fields)", extra_count)?;
5614 }
5615 write!(f, " }}")
5616 }
5617}
5618impl ::core::default::Default for ConnectionRequest {
5619 fn default() -> Self {
5620 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5621 ConnectionRequest::new_unchecked(v)
5622 }
5623}
5624impl ConnectionRequest {
5625 const DEFAULT_VALUE: [u8; 41] = [
5626 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,
5627 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
5628 ];
5629 pub const FIELD_COUNT: usize = 5;
5630 pub fn total_size(&self) -> usize {
5631 molecule::unpack_number(self.as_slice()) as usize
5632 }
5633 pub fn field_count(&self) -> usize {
5634 if self.total_size() == molecule::NUMBER_SIZE {
5635 0
5636 } else {
5637 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5638 }
5639 }
5640 pub fn count_extra_fields(&self) -> usize {
5641 self.field_count() - Self::FIELD_COUNT
5642 }
5643 pub fn has_extra_fields(&self) -> bool {
5644 Self::FIELD_COUNT != self.field_count()
5645 }
5646 pub fn from(&self) -> Bytes {
5647 let slice = self.as_slice();
5648 let start = molecule::unpack_number(&slice[4..]) as usize;
5649 let end = molecule::unpack_number(&slice[8..]) as usize;
5650 Bytes::new_unchecked(self.0.slice(start..end))
5651 }
5652 pub fn to(&self) -> Bytes {
5653 let slice = self.as_slice();
5654 let start = molecule::unpack_number(&slice[8..]) as usize;
5655 let end = molecule::unpack_number(&slice[12..]) as usize;
5656 Bytes::new_unchecked(self.0.slice(start..end))
5657 }
5658 pub fn max_hops(&self) -> Byte {
5659 let slice = self.as_slice();
5660 let start = molecule::unpack_number(&slice[12..]) as usize;
5661 let end = molecule::unpack_number(&slice[16..]) as usize;
5662 Byte::new_unchecked(self.0.slice(start..end))
5663 }
5664 pub fn route(&self) -> BytesVec {
5665 let slice = self.as_slice();
5666 let start = molecule::unpack_number(&slice[16..]) as usize;
5667 let end = molecule::unpack_number(&slice[20..]) as usize;
5668 BytesVec::new_unchecked(self.0.slice(start..end))
5669 }
5670 pub fn listen_addrs(&self) -> AddressVec {
5671 let slice = self.as_slice();
5672 let start = molecule::unpack_number(&slice[20..]) as usize;
5673 if self.has_extra_fields() {
5674 let end = molecule::unpack_number(&slice[24..]) as usize;
5675 AddressVec::new_unchecked(self.0.slice(start..end))
5676 } else {
5677 AddressVec::new_unchecked(self.0.slice(start..))
5678 }
5679 }
5680 pub fn as_reader<'r>(&'r self) -> ConnectionRequestReader<'r> {
5681 ConnectionRequestReader::new_unchecked(self.as_slice())
5682 }
5683}
5684impl molecule::prelude::Entity for ConnectionRequest {
5685 type Builder = ConnectionRequestBuilder;
5686 const NAME: &'static str = "ConnectionRequest";
5687 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5688 ConnectionRequest(data)
5689 }
5690 fn as_bytes(&self) -> molecule::bytes::Bytes {
5691 self.0.clone()
5692 }
5693 fn as_slice(&self) -> &[u8] {
5694 &self.0[..]
5695 }
5696 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5697 ConnectionRequestReader::from_slice(slice).map(|reader| reader.to_entity())
5698 }
5699 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5700 ConnectionRequestReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5701 }
5702 fn new_builder() -> Self::Builder {
5703 ::core::default::Default::default()
5704 }
5705 fn as_builder(self) -> Self::Builder {
5706 Self::new_builder()
5707 .from(self.from())
5708 .to(self.to())
5709 .max_hops(self.max_hops())
5710 .route(self.route())
5711 .listen_addrs(self.listen_addrs())
5712 }
5713}
5714#[derive(Clone, Copy)]
5715pub struct ConnectionRequestReader<'r>(&'r [u8]);
5716impl<'r> ::core::fmt::LowerHex for ConnectionRequestReader<'r> {
5717 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5718 use molecule::hex_string;
5719 if f.alternate() {
5720 write!(f, "0x")?;
5721 }
5722 write!(f, "{}", hex_string(self.as_slice()))
5723 }
5724}
5725impl<'r> ::core::fmt::Debug for ConnectionRequestReader<'r> {
5726 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5727 write!(f, "{}({:#x})", Self::NAME, self)
5728 }
5729}
5730impl<'r> ::core::fmt::Display for ConnectionRequestReader<'r> {
5731 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5732 write!(f, "{} {{ ", Self::NAME)?;
5733 write!(f, "{}: {}", "from", self.from())?;
5734 write!(f, ", {}: {}", "to", self.to())?;
5735 write!(f, ", {}: {}", "max_hops", self.max_hops())?;
5736 write!(f, ", {}: {}", "route", self.route())?;
5737 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
5738 let extra_count = self.count_extra_fields();
5739 if extra_count != 0 {
5740 write!(f, ", .. ({} fields)", extra_count)?;
5741 }
5742 write!(f, " }}")
5743 }
5744}
5745impl<'r> ConnectionRequestReader<'r> {
5746 pub const FIELD_COUNT: usize = 5;
5747 pub fn total_size(&self) -> usize {
5748 molecule::unpack_number(self.as_slice()) as usize
5749 }
5750 pub fn field_count(&self) -> usize {
5751 if self.total_size() == molecule::NUMBER_SIZE {
5752 0
5753 } else {
5754 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5755 }
5756 }
5757 pub fn count_extra_fields(&self) -> usize {
5758 self.field_count() - Self::FIELD_COUNT
5759 }
5760 pub fn has_extra_fields(&self) -> bool {
5761 Self::FIELD_COUNT != self.field_count()
5762 }
5763 pub fn from(&self) -> BytesReader<'r> {
5764 let slice = self.as_slice();
5765 let start = molecule::unpack_number(&slice[4..]) as usize;
5766 let end = molecule::unpack_number(&slice[8..]) as usize;
5767 BytesReader::new_unchecked(&self.as_slice()[start..end])
5768 }
5769 pub fn to(&self) -> BytesReader<'r> {
5770 let slice = self.as_slice();
5771 let start = molecule::unpack_number(&slice[8..]) as usize;
5772 let end = molecule::unpack_number(&slice[12..]) as usize;
5773 BytesReader::new_unchecked(&self.as_slice()[start..end])
5774 }
5775 pub fn max_hops(&self) -> ByteReader<'r> {
5776 let slice = self.as_slice();
5777 let start = molecule::unpack_number(&slice[12..]) as usize;
5778 let end = molecule::unpack_number(&slice[16..]) as usize;
5779 ByteReader::new_unchecked(&self.as_slice()[start..end])
5780 }
5781 pub fn route(&self) -> BytesVecReader<'r> {
5782 let slice = self.as_slice();
5783 let start = molecule::unpack_number(&slice[16..]) as usize;
5784 let end = molecule::unpack_number(&slice[20..]) as usize;
5785 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
5786 }
5787 pub fn listen_addrs(&self) -> AddressVecReader<'r> {
5788 let slice = self.as_slice();
5789 let start = molecule::unpack_number(&slice[20..]) as usize;
5790 if self.has_extra_fields() {
5791 let end = molecule::unpack_number(&slice[24..]) as usize;
5792 AddressVecReader::new_unchecked(&self.as_slice()[start..end])
5793 } else {
5794 AddressVecReader::new_unchecked(&self.as_slice()[start..])
5795 }
5796 }
5797}
5798impl<'r> molecule::prelude::Reader<'r> for ConnectionRequestReader<'r> {
5799 type Entity = ConnectionRequest;
5800 const NAME: &'static str = "ConnectionRequestReader";
5801 fn to_entity(&self) -> Self::Entity {
5802 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5803 }
5804 fn new_unchecked(slice: &'r [u8]) -> Self {
5805 ConnectionRequestReader(slice)
5806 }
5807 fn as_slice(&self) -> &'r [u8] {
5808 self.0
5809 }
5810 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5811 use molecule::verification_error as ve;
5812 let slice_len = slice.len();
5813 if slice_len < molecule::NUMBER_SIZE {
5814 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5815 }
5816 let total_size = molecule::unpack_number(slice) as usize;
5817 if slice_len != total_size {
5818 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5819 }
5820 if slice_len < molecule::NUMBER_SIZE * 2 {
5821 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5822 }
5823 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5824 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5825 return ve!(Self, OffsetsNotMatch);
5826 }
5827 if slice_len < offset_first {
5828 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5829 }
5830 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5831 if field_count < Self::FIELD_COUNT {
5832 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5833 } else if !compatible && field_count > Self::FIELD_COUNT {
5834 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5835 };
5836 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5837 .chunks_exact(molecule::NUMBER_SIZE)
5838 .map(|x| molecule::unpack_number(x) as usize)
5839 .collect();
5840 offsets.push(total_size);
5841 if offsets.windows(2).any(|i| i[0] > i[1]) {
5842 return ve!(Self, OffsetsNotMatch);
5843 }
5844 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5845 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5846 ByteReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5847 BytesVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
5848 AddressVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
5849 Ok(())
5850 }
5851}
5852#[derive(Clone, Debug, Default)]
5853pub struct ConnectionRequestBuilder {
5854 pub(crate) from: Bytes,
5855 pub(crate) to: Bytes,
5856 pub(crate) max_hops: Byte,
5857 pub(crate) route: BytesVec,
5858 pub(crate) listen_addrs: AddressVec,
5859}
5860impl ConnectionRequestBuilder {
5861 pub const FIELD_COUNT: usize = 5;
5862 pub fn from<T>(mut self, v: T) -> Self
5863 where
5864 T: ::core::convert::Into<Bytes>,
5865 {
5866 self.from = v.into();
5867 self
5868 }
5869 pub fn to<T>(mut self, v: T) -> Self
5870 where
5871 T: ::core::convert::Into<Bytes>,
5872 {
5873 self.to = v.into();
5874 self
5875 }
5876 pub fn max_hops<T>(mut self, v: T) -> Self
5877 where
5878 T: ::core::convert::Into<Byte>,
5879 {
5880 self.max_hops = v.into();
5881 self
5882 }
5883 pub fn route<T>(mut self, v: T) -> Self
5884 where
5885 T: ::core::convert::Into<BytesVec>,
5886 {
5887 self.route = v.into();
5888 self
5889 }
5890 pub fn listen_addrs<T>(mut self, v: T) -> Self
5891 where
5892 T: ::core::convert::Into<AddressVec>,
5893 {
5894 self.listen_addrs = v.into();
5895 self
5896 }
5897}
5898impl molecule::prelude::Builder for ConnectionRequestBuilder {
5899 type Entity = ConnectionRequest;
5900 const NAME: &'static str = "ConnectionRequestBuilder";
5901 fn expected_length(&self) -> usize {
5902 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5903 + self.from.as_slice().len()
5904 + self.to.as_slice().len()
5905 + self.max_hops.as_slice().len()
5906 + self.route.as_slice().len()
5907 + self.listen_addrs.as_slice().len()
5908 }
5909 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5910 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5911 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5912 offsets.push(total_size);
5913 total_size += self.from.as_slice().len();
5914 offsets.push(total_size);
5915 total_size += self.to.as_slice().len();
5916 offsets.push(total_size);
5917 total_size += self.max_hops.as_slice().len();
5918 offsets.push(total_size);
5919 total_size += self.route.as_slice().len();
5920 offsets.push(total_size);
5921 total_size += self.listen_addrs.as_slice().len();
5922 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5923 for offset in offsets.into_iter() {
5924 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5925 }
5926 writer.write_all(self.from.as_slice())?;
5927 writer.write_all(self.to.as_slice())?;
5928 writer.write_all(self.max_hops.as_slice())?;
5929 writer.write_all(self.route.as_slice())?;
5930 writer.write_all(self.listen_addrs.as_slice())?;
5931 Ok(())
5932 }
5933 fn build(&self) -> Self::Entity {
5934 let mut inner = Vec::with_capacity(self.expected_length());
5935 self.write(&mut inner)
5936 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5937 ConnectionRequest::new_unchecked(inner.into())
5938 }
5939}
5940#[derive(Clone)]
5941pub struct ConnectionRequestDelivered(molecule::bytes::Bytes);
5942impl ::core::fmt::LowerHex for ConnectionRequestDelivered {
5943 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5944 use molecule::hex_string;
5945 if f.alternate() {
5946 write!(f, "0x")?;
5947 }
5948 write!(f, "{}", hex_string(self.as_slice()))
5949 }
5950}
5951impl ::core::fmt::Debug for ConnectionRequestDelivered {
5952 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5953 write!(f, "{}({:#x})", Self::NAME, self)
5954 }
5955}
5956impl ::core::fmt::Display for ConnectionRequestDelivered {
5957 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5958 write!(f, "{} {{ ", Self::NAME)?;
5959 write!(f, "{}: {}", "from", self.from())?;
5960 write!(f, ", {}: {}", "to", self.to())?;
5961 write!(f, ", {}: {}", "route", self.route())?;
5962 write!(f, ", {}: {}", "sync_route", self.sync_route())?;
5963 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
5964 let extra_count = self.count_extra_fields();
5965 if extra_count != 0 {
5966 write!(f, ", .. ({} fields)", extra_count)?;
5967 }
5968 write!(f, " }}")
5969 }
5970}
5971impl ::core::default::Default for ConnectionRequestDelivered {
5972 fn default() -> Self {
5973 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5974 ConnectionRequestDelivered::new_unchecked(v)
5975 }
5976}
5977impl ConnectionRequestDelivered {
5978 const DEFAULT_VALUE: [u8; 44] = [
5979 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,
5980 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
5981 ];
5982 pub const FIELD_COUNT: usize = 5;
5983 pub fn total_size(&self) -> usize {
5984 molecule::unpack_number(self.as_slice()) as usize
5985 }
5986 pub fn field_count(&self) -> usize {
5987 if self.total_size() == molecule::NUMBER_SIZE {
5988 0
5989 } else {
5990 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5991 }
5992 }
5993 pub fn count_extra_fields(&self) -> usize {
5994 self.field_count() - Self::FIELD_COUNT
5995 }
5996 pub fn has_extra_fields(&self) -> bool {
5997 Self::FIELD_COUNT != self.field_count()
5998 }
5999 pub fn from(&self) -> Bytes {
6000 let slice = self.as_slice();
6001 let start = molecule::unpack_number(&slice[4..]) as usize;
6002 let end = molecule::unpack_number(&slice[8..]) as usize;
6003 Bytes::new_unchecked(self.0.slice(start..end))
6004 }
6005 pub fn to(&self) -> Bytes {
6006 let slice = self.as_slice();
6007 let start = molecule::unpack_number(&slice[8..]) as usize;
6008 let end = molecule::unpack_number(&slice[12..]) as usize;
6009 Bytes::new_unchecked(self.0.slice(start..end))
6010 }
6011 pub fn route(&self) -> BytesVec {
6012 let slice = self.as_slice();
6013 let start = molecule::unpack_number(&slice[12..]) as usize;
6014 let end = molecule::unpack_number(&slice[16..]) as usize;
6015 BytesVec::new_unchecked(self.0.slice(start..end))
6016 }
6017 pub fn sync_route(&self) -> BytesVec {
6018 let slice = self.as_slice();
6019 let start = molecule::unpack_number(&slice[16..]) as usize;
6020 let end = molecule::unpack_number(&slice[20..]) as usize;
6021 BytesVec::new_unchecked(self.0.slice(start..end))
6022 }
6023 pub fn listen_addrs(&self) -> AddressVec {
6024 let slice = self.as_slice();
6025 let start = molecule::unpack_number(&slice[20..]) as usize;
6026 if self.has_extra_fields() {
6027 let end = molecule::unpack_number(&slice[24..]) as usize;
6028 AddressVec::new_unchecked(self.0.slice(start..end))
6029 } else {
6030 AddressVec::new_unchecked(self.0.slice(start..))
6031 }
6032 }
6033 pub fn as_reader<'r>(&'r self) -> ConnectionRequestDeliveredReader<'r> {
6034 ConnectionRequestDeliveredReader::new_unchecked(self.as_slice())
6035 }
6036}
6037impl molecule::prelude::Entity for ConnectionRequestDelivered {
6038 type Builder = ConnectionRequestDeliveredBuilder;
6039 const NAME: &'static str = "ConnectionRequestDelivered";
6040 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6041 ConnectionRequestDelivered(data)
6042 }
6043 fn as_bytes(&self) -> molecule::bytes::Bytes {
6044 self.0.clone()
6045 }
6046 fn as_slice(&self) -> &[u8] {
6047 &self.0[..]
6048 }
6049 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6050 ConnectionRequestDeliveredReader::from_slice(slice).map(|reader| reader.to_entity())
6051 }
6052 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6053 ConnectionRequestDeliveredReader::from_compatible_slice(slice)
6054 .map(|reader| reader.to_entity())
6055 }
6056 fn new_builder() -> Self::Builder {
6057 ::core::default::Default::default()
6058 }
6059 fn as_builder(self) -> Self::Builder {
6060 Self::new_builder()
6061 .from(self.from())
6062 .to(self.to())
6063 .route(self.route())
6064 .sync_route(self.sync_route())
6065 .listen_addrs(self.listen_addrs())
6066 }
6067}
6068#[derive(Clone, Copy)]
6069pub struct ConnectionRequestDeliveredReader<'r>(&'r [u8]);
6070impl<'r> ::core::fmt::LowerHex for ConnectionRequestDeliveredReader<'r> {
6071 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6072 use molecule::hex_string;
6073 if f.alternate() {
6074 write!(f, "0x")?;
6075 }
6076 write!(f, "{}", hex_string(self.as_slice()))
6077 }
6078}
6079impl<'r> ::core::fmt::Debug for ConnectionRequestDeliveredReader<'r> {
6080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6081 write!(f, "{}({:#x})", Self::NAME, self)
6082 }
6083}
6084impl<'r> ::core::fmt::Display for ConnectionRequestDeliveredReader<'r> {
6085 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6086 write!(f, "{} {{ ", Self::NAME)?;
6087 write!(f, "{}: {}", "from", self.from())?;
6088 write!(f, ", {}: {}", "to", self.to())?;
6089 write!(f, ", {}: {}", "route", self.route())?;
6090 write!(f, ", {}: {}", "sync_route", self.sync_route())?;
6091 write!(f, ", {}: {}", "listen_addrs", self.listen_addrs())?;
6092 let extra_count = self.count_extra_fields();
6093 if extra_count != 0 {
6094 write!(f, ", .. ({} fields)", extra_count)?;
6095 }
6096 write!(f, " }}")
6097 }
6098}
6099impl<'r> ConnectionRequestDeliveredReader<'r> {
6100 pub const FIELD_COUNT: usize = 5;
6101 pub fn total_size(&self) -> usize {
6102 molecule::unpack_number(self.as_slice()) as usize
6103 }
6104 pub fn field_count(&self) -> usize {
6105 if self.total_size() == molecule::NUMBER_SIZE {
6106 0
6107 } else {
6108 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6109 }
6110 }
6111 pub fn count_extra_fields(&self) -> usize {
6112 self.field_count() - Self::FIELD_COUNT
6113 }
6114 pub fn has_extra_fields(&self) -> bool {
6115 Self::FIELD_COUNT != self.field_count()
6116 }
6117 pub fn from(&self) -> BytesReader<'r> {
6118 let slice = self.as_slice();
6119 let start = molecule::unpack_number(&slice[4..]) as usize;
6120 let end = molecule::unpack_number(&slice[8..]) as usize;
6121 BytesReader::new_unchecked(&self.as_slice()[start..end])
6122 }
6123 pub fn to(&self) -> BytesReader<'r> {
6124 let slice = self.as_slice();
6125 let start = molecule::unpack_number(&slice[8..]) as usize;
6126 let end = molecule::unpack_number(&slice[12..]) as usize;
6127 BytesReader::new_unchecked(&self.as_slice()[start..end])
6128 }
6129 pub fn route(&self) -> BytesVecReader<'r> {
6130 let slice = self.as_slice();
6131 let start = molecule::unpack_number(&slice[12..]) as usize;
6132 let end = molecule::unpack_number(&slice[16..]) as usize;
6133 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6134 }
6135 pub fn sync_route(&self) -> BytesVecReader<'r> {
6136 let slice = self.as_slice();
6137 let start = molecule::unpack_number(&slice[16..]) as usize;
6138 let end = molecule::unpack_number(&slice[20..]) as usize;
6139 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6140 }
6141 pub fn listen_addrs(&self) -> AddressVecReader<'r> {
6142 let slice = self.as_slice();
6143 let start = molecule::unpack_number(&slice[20..]) as usize;
6144 if self.has_extra_fields() {
6145 let end = molecule::unpack_number(&slice[24..]) as usize;
6146 AddressVecReader::new_unchecked(&self.as_slice()[start..end])
6147 } else {
6148 AddressVecReader::new_unchecked(&self.as_slice()[start..])
6149 }
6150 }
6151}
6152impl<'r> molecule::prelude::Reader<'r> for ConnectionRequestDeliveredReader<'r> {
6153 type Entity = ConnectionRequestDelivered;
6154 const NAME: &'static str = "ConnectionRequestDeliveredReader";
6155 fn to_entity(&self) -> Self::Entity {
6156 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6157 }
6158 fn new_unchecked(slice: &'r [u8]) -> Self {
6159 ConnectionRequestDeliveredReader(slice)
6160 }
6161 fn as_slice(&self) -> &'r [u8] {
6162 self.0
6163 }
6164 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6165 use molecule::verification_error as ve;
6166 let slice_len = slice.len();
6167 if slice_len < molecule::NUMBER_SIZE {
6168 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6169 }
6170 let total_size = molecule::unpack_number(slice) as usize;
6171 if slice_len != total_size {
6172 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6173 }
6174 if slice_len < molecule::NUMBER_SIZE * 2 {
6175 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6176 }
6177 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6178 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6179 return ve!(Self, OffsetsNotMatch);
6180 }
6181 if slice_len < offset_first {
6182 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6183 }
6184 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6185 if field_count < Self::FIELD_COUNT {
6186 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6187 } else if !compatible && field_count > Self::FIELD_COUNT {
6188 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6189 };
6190 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6191 .chunks_exact(molecule::NUMBER_SIZE)
6192 .map(|x| molecule::unpack_number(x) as usize)
6193 .collect();
6194 offsets.push(total_size);
6195 if offsets.windows(2).any(|i| i[0] > i[1]) {
6196 return ve!(Self, OffsetsNotMatch);
6197 }
6198 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6199 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6200 BytesVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6201 BytesVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
6202 AddressVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
6203 Ok(())
6204 }
6205}
6206#[derive(Clone, Debug, Default)]
6207pub struct ConnectionRequestDeliveredBuilder {
6208 pub(crate) from: Bytes,
6209 pub(crate) to: Bytes,
6210 pub(crate) route: BytesVec,
6211 pub(crate) sync_route: BytesVec,
6212 pub(crate) listen_addrs: AddressVec,
6213}
6214impl ConnectionRequestDeliveredBuilder {
6215 pub const FIELD_COUNT: usize = 5;
6216 pub fn from<T>(mut self, v: T) -> Self
6217 where
6218 T: ::core::convert::Into<Bytes>,
6219 {
6220 self.from = v.into();
6221 self
6222 }
6223 pub fn to<T>(mut self, v: T) -> Self
6224 where
6225 T: ::core::convert::Into<Bytes>,
6226 {
6227 self.to = v.into();
6228 self
6229 }
6230 pub fn route<T>(mut self, v: T) -> Self
6231 where
6232 T: ::core::convert::Into<BytesVec>,
6233 {
6234 self.route = v.into();
6235 self
6236 }
6237 pub fn sync_route<T>(mut self, v: T) -> Self
6238 where
6239 T: ::core::convert::Into<BytesVec>,
6240 {
6241 self.sync_route = v.into();
6242 self
6243 }
6244 pub fn listen_addrs<T>(mut self, v: T) -> Self
6245 where
6246 T: ::core::convert::Into<AddressVec>,
6247 {
6248 self.listen_addrs = v.into();
6249 self
6250 }
6251}
6252impl molecule::prelude::Builder for ConnectionRequestDeliveredBuilder {
6253 type Entity = ConnectionRequestDelivered;
6254 const NAME: &'static str = "ConnectionRequestDeliveredBuilder";
6255 fn expected_length(&self) -> usize {
6256 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6257 + self.from.as_slice().len()
6258 + self.to.as_slice().len()
6259 + self.route.as_slice().len()
6260 + self.sync_route.as_slice().len()
6261 + self.listen_addrs.as_slice().len()
6262 }
6263 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6264 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6265 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6266 offsets.push(total_size);
6267 total_size += self.from.as_slice().len();
6268 offsets.push(total_size);
6269 total_size += self.to.as_slice().len();
6270 offsets.push(total_size);
6271 total_size += self.route.as_slice().len();
6272 offsets.push(total_size);
6273 total_size += self.sync_route.as_slice().len();
6274 offsets.push(total_size);
6275 total_size += self.listen_addrs.as_slice().len();
6276 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6277 for offset in offsets.into_iter() {
6278 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6279 }
6280 writer.write_all(self.from.as_slice())?;
6281 writer.write_all(self.to.as_slice())?;
6282 writer.write_all(self.route.as_slice())?;
6283 writer.write_all(self.sync_route.as_slice())?;
6284 writer.write_all(self.listen_addrs.as_slice())?;
6285 Ok(())
6286 }
6287 fn build(&self) -> Self::Entity {
6288 let mut inner = Vec::with_capacity(self.expected_length());
6289 self.write(&mut inner)
6290 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6291 ConnectionRequestDelivered::new_unchecked(inner.into())
6292 }
6293}
6294#[derive(Clone)]
6295pub struct ConnectionSync(molecule::bytes::Bytes);
6296impl ::core::fmt::LowerHex for ConnectionSync {
6297 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6298 use molecule::hex_string;
6299 if f.alternate() {
6300 write!(f, "0x")?;
6301 }
6302 write!(f, "{}", hex_string(self.as_slice()))
6303 }
6304}
6305impl ::core::fmt::Debug for ConnectionSync {
6306 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6307 write!(f, "{}({:#x})", Self::NAME, self)
6308 }
6309}
6310impl ::core::fmt::Display for ConnectionSync {
6311 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6312 write!(f, "{} {{ ", Self::NAME)?;
6313 write!(f, "{}: {}", "from", self.from())?;
6314 write!(f, ", {}: {}", "to", self.to())?;
6315 write!(f, ", {}: {}", "route", self.route())?;
6316 let extra_count = self.count_extra_fields();
6317 if extra_count != 0 {
6318 write!(f, ", .. ({} fields)", extra_count)?;
6319 }
6320 write!(f, " }}")
6321 }
6322}
6323impl ::core::default::Default for ConnectionSync {
6324 fn default() -> Self {
6325 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6326 ConnectionSync::new_unchecked(v)
6327 }
6328}
6329impl ConnectionSync {
6330 const DEFAULT_VALUE: [u8; 28] = [
6331 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,
6332 ];
6333 pub const FIELD_COUNT: usize = 3;
6334 pub fn total_size(&self) -> usize {
6335 molecule::unpack_number(self.as_slice()) as usize
6336 }
6337 pub fn field_count(&self) -> usize {
6338 if self.total_size() == molecule::NUMBER_SIZE {
6339 0
6340 } else {
6341 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6342 }
6343 }
6344 pub fn count_extra_fields(&self) -> usize {
6345 self.field_count() - Self::FIELD_COUNT
6346 }
6347 pub fn has_extra_fields(&self) -> bool {
6348 Self::FIELD_COUNT != self.field_count()
6349 }
6350 pub fn from(&self) -> Bytes {
6351 let slice = self.as_slice();
6352 let start = molecule::unpack_number(&slice[4..]) as usize;
6353 let end = molecule::unpack_number(&slice[8..]) as usize;
6354 Bytes::new_unchecked(self.0.slice(start..end))
6355 }
6356 pub fn to(&self) -> Bytes {
6357 let slice = self.as_slice();
6358 let start = molecule::unpack_number(&slice[8..]) as usize;
6359 let end = molecule::unpack_number(&slice[12..]) as usize;
6360 Bytes::new_unchecked(self.0.slice(start..end))
6361 }
6362 pub fn route(&self) -> BytesVec {
6363 let slice = self.as_slice();
6364 let start = molecule::unpack_number(&slice[12..]) as usize;
6365 if self.has_extra_fields() {
6366 let end = molecule::unpack_number(&slice[16..]) as usize;
6367 BytesVec::new_unchecked(self.0.slice(start..end))
6368 } else {
6369 BytesVec::new_unchecked(self.0.slice(start..))
6370 }
6371 }
6372 pub fn as_reader<'r>(&'r self) -> ConnectionSyncReader<'r> {
6373 ConnectionSyncReader::new_unchecked(self.as_slice())
6374 }
6375}
6376impl molecule::prelude::Entity for ConnectionSync {
6377 type Builder = ConnectionSyncBuilder;
6378 const NAME: &'static str = "ConnectionSync";
6379 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6380 ConnectionSync(data)
6381 }
6382 fn as_bytes(&self) -> molecule::bytes::Bytes {
6383 self.0.clone()
6384 }
6385 fn as_slice(&self) -> &[u8] {
6386 &self.0[..]
6387 }
6388 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6389 ConnectionSyncReader::from_slice(slice).map(|reader| reader.to_entity())
6390 }
6391 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6392 ConnectionSyncReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6393 }
6394 fn new_builder() -> Self::Builder {
6395 ::core::default::Default::default()
6396 }
6397 fn as_builder(self) -> Self::Builder {
6398 Self::new_builder()
6399 .from(self.from())
6400 .to(self.to())
6401 .route(self.route())
6402 }
6403}
6404#[derive(Clone, Copy)]
6405pub struct ConnectionSyncReader<'r>(&'r [u8]);
6406impl<'r> ::core::fmt::LowerHex for ConnectionSyncReader<'r> {
6407 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6408 use molecule::hex_string;
6409 if f.alternate() {
6410 write!(f, "0x")?;
6411 }
6412 write!(f, "{}", hex_string(self.as_slice()))
6413 }
6414}
6415impl<'r> ::core::fmt::Debug for ConnectionSyncReader<'r> {
6416 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6417 write!(f, "{}({:#x})", Self::NAME, self)
6418 }
6419}
6420impl<'r> ::core::fmt::Display for ConnectionSyncReader<'r> {
6421 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6422 write!(f, "{} {{ ", Self::NAME)?;
6423 write!(f, "{}: {}", "from", self.from())?;
6424 write!(f, ", {}: {}", "to", self.to())?;
6425 write!(f, ", {}: {}", "route", self.route())?;
6426 let extra_count = self.count_extra_fields();
6427 if extra_count != 0 {
6428 write!(f, ", .. ({} fields)", extra_count)?;
6429 }
6430 write!(f, " }}")
6431 }
6432}
6433impl<'r> ConnectionSyncReader<'r> {
6434 pub const FIELD_COUNT: usize = 3;
6435 pub fn total_size(&self) -> usize {
6436 molecule::unpack_number(self.as_slice()) as usize
6437 }
6438 pub fn field_count(&self) -> usize {
6439 if self.total_size() == molecule::NUMBER_SIZE {
6440 0
6441 } else {
6442 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6443 }
6444 }
6445 pub fn count_extra_fields(&self) -> usize {
6446 self.field_count() - Self::FIELD_COUNT
6447 }
6448 pub fn has_extra_fields(&self) -> bool {
6449 Self::FIELD_COUNT != self.field_count()
6450 }
6451 pub fn from(&self) -> BytesReader<'r> {
6452 let slice = self.as_slice();
6453 let start = molecule::unpack_number(&slice[4..]) as usize;
6454 let end = molecule::unpack_number(&slice[8..]) as usize;
6455 BytesReader::new_unchecked(&self.as_slice()[start..end])
6456 }
6457 pub fn to(&self) -> BytesReader<'r> {
6458 let slice = self.as_slice();
6459 let start = molecule::unpack_number(&slice[8..]) as usize;
6460 let end = molecule::unpack_number(&slice[12..]) as usize;
6461 BytesReader::new_unchecked(&self.as_slice()[start..end])
6462 }
6463 pub fn route(&self) -> BytesVecReader<'r> {
6464 let slice = self.as_slice();
6465 let start = molecule::unpack_number(&slice[12..]) as usize;
6466 if self.has_extra_fields() {
6467 let end = molecule::unpack_number(&slice[16..]) as usize;
6468 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6469 } else {
6470 BytesVecReader::new_unchecked(&self.as_slice()[start..])
6471 }
6472 }
6473}
6474impl<'r> molecule::prelude::Reader<'r> for ConnectionSyncReader<'r> {
6475 type Entity = ConnectionSync;
6476 const NAME: &'static str = "ConnectionSyncReader";
6477 fn to_entity(&self) -> Self::Entity {
6478 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6479 }
6480 fn new_unchecked(slice: &'r [u8]) -> Self {
6481 ConnectionSyncReader(slice)
6482 }
6483 fn as_slice(&self) -> &'r [u8] {
6484 self.0
6485 }
6486 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6487 use molecule::verification_error as ve;
6488 let slice_len = slice.len();
6489 if slice_len < molecule::NUMBER_SIZE {
6490 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6491 }
6492 let total_size = molecule::unpack_number(slice) as usize;
6493 if slice_len != total_size {
6494 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6495 }
6496 if slice_len < molecule::NUMBER_SIZE * 2 {
6497 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6498 }
6499 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6500 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6501 return ve!(Self, OffsetsNotMatch);
6502 }
6503 if slice_len < offset_first {
6504 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6505 }
6506 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6507 if field_count < Self::FIELD_COUNT {
6508 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6509 } else if !compatible && field_count > Self::FIELD_COUNT {
6510 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6511 };
6512 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6513 .chunks_exact(molecule::NUMBER_SIZE)
6514 .map(|x| molecule::unpack_number(x) as usize)
6515 .collect();
6516 offsets.push(total_size);
6517 if offsets.windows(2).any(|i| i[0] > i[1]) {
6518 return ve!(Self, OffsetsNotMatch);
6519 }
6520 BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6521 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6522 BytesVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6523 Ok(())
6524 }
6525}
6526#[derive(Clone, Debug, Default)]
6527pub struct ConnectionSyncBuilder {
6528 pub(crate) from: Bytes,
6529 pub(crate) to: Bytes,
6530 pub(crate) route: BytesVec,
6531}
6532impl ConnectionSyncBuilder {
6533 pub const FIELD_COUNT: usize = 3;
6534 pub fn from<T>(mut self, v: T) -> Self
6535 where
6536 T: ::core::convert::Into<Bytes>,
6537 {
6538 self.from = v.into();
6539 self
6540 }
6541 pub fn to<T>(mut self, v: T) -> Self
6542 where
6543 T: ::core::convert::Into<Bytes>,
6544 {
6545 self.to = v.into();
6546 self
6547 }
6548 pub fn route<T>(mut self, v: T) -> Self
6549 where
6550 T: ::core::convert::Into<BytesVec>,
6551 {
6552 self.route = v.into();
6553 self
6554 }
6555}
6556impl molecule::prelude::Builder for ConnectionSyncBuilder {
6557 type Entity = ConnectionSync;
6558 const NAME: &'static str = "ConnectionSyncBuilder";
6559 fn expected_length(&self) -> usize {
6560 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6561 + self.from.as_slice().len()
6562 + self.to.as_slice().len()
6563 + self.route.as_slice().len()
6564 }
6565 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6566 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6567 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6568 offsets.push(total_size);
6569 total_size += self.from.as_slice().len();
6570 offsets.push(total_size);
6571 total_size += self.to.as_slice().len();
6572 offsets.push(total_size);
6573 total_size += self.route.as_slice().len();
6574 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6575 for offset in offsets.into_iter() {
6576 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6577 }
6578 writer.write_all(self.from.as_slice())?;
6579 writer.write_all(self.to.as_slice())?;
6580 writer.write_all(self.route.as_slice())?;
6581 Ok(())
6582 }
6583 fn build(&self) -> Self::Entity {
6584 let mut inner = Vec::with_capacity(self.expected_length());
6585 self.write(&mut inner)
6586 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6587 ConnectionSync::new_unchecked(inner.into())
6588 }
6589}