1#![deny(unsafe_code)]
2
3extern crate self as object_rainbow;
4
5use std::{
6 any::{Any, TypeId},
7 cell::Cell,
8 convert::Infallible,
9 future::ready,
10 marker::PhantomData,
11 ops::{Deref, DerefMut},
12 pin::Pin,
13 sync::Arc,
14};
15
16pub use anyhow::anyhow;
17use generic_array::{ArrayLength, GenericArray};
18pub use object_rainbow_derive::{
19 Enum, InlineOutput, ListHashes, MaybeHasNiche, Parse, ParseAsInline, ParseInline, Size, Tagged,
20 ToOutput, Topological,
21};
22use sha2::{Digest, Sha256};
23#[doc(hidden)]
24pub use typenum;
25use typenum::Unsigned;
26
27pub use self::enumkind::Enum;
28pub use self::error::{Error, Result};
29pub use self::hash::{Hash, OptionalHash};
30pub use self::niche::{
31 AutoEnumNiche, AutoNiche, HackNiche, MaybeHasNiche, Niche, NicheForUnsized, NoNiche, OneNiche,
32 SomeNiche, ZeroNiche, ZeroNoNiche,
33};
34#[doc(hidden)]
35pub use self::niche::{MaybeNiche, MnArray, NicheFoldOrArray, NicheOr};
36
37mod assert_impl;
38pub mod enumkind;
39mod error;
40mod hash;
41pub mod hashed;
42mod impls;
43pub mod length_prefixed;
44mod niche;
45pub mod numeric;
46mod sha2_const;
47pub mod zero_terminated;
48
49pub const HASH_SIZE: usize = sha2_const::Sha256::DIGEST_SIZE;
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, ParseAsInline)]
58pub struct Address {
59 pub index: usize,
61 pub hash: Hash,
63}
64
65impl Address {
66 pub fn from_hash(hash: Hash) -> Self {
69 Self {
70 index: usize::MAX,
71 hash,
72 }
73 }
74}
75
76impl<I: PointInput> ParseInline<I> for Address {
77 fn parse_inline(input: &mut I) -> crate::Result<Self> {
78 Ok(Self {
79 index: input.next_index(),
80 hash: input.parse_inline()?,
81 })
82 }
83}
84
85pub type FailFuture<'a, T> = Pin<Box<dyn 'a + Send + Future<Output = Result<T>>>>;
87
88pub type Node<T> = (T, Arc<dyn Resolve>);
89
90pub type ByteNode = Node<Vec<u8>>;
92
93pub trait AsAny {
96 fn any_ref(&self) -> &dyn Any
98 where
99 Self: 'static;
100 fn any_mut(&mut self) -> &mut dyn Any
102 where
103 Self: 'static;
104 fn any_box(self: Box<Self>) -> Box<dyn Any>
106 where
107 Self: 'static;
108 fn any_arc(self: Arc<Self>) -> Arc<dyn Any>
110 where
111 Self: 'static;
112 fn any_arc_sync(self: Arc<Self>) -> Arc<dyn Send + Sync + Any>
114 where
115 Self: 'static + Send + Sync;
116}
117
118impl<T> AsAny for T {
119 fn any_ref(&self) -> &dyn Any
120 where
121 Self: 'static,
122 {
123 self
124 }
125
126 fn any_mut(&mut self) -> &mut dyn Any
127 where
128 Self: 'static,
129 {
130 self
131 }
132
133 fn any_box(self: Box<Self>) -> Box<dyn Any>
134 where
135 Self: 'static,
136 {
137 self
138 }
139
140 fn any_arc(self: Arc<Self>) -> Arc<dyn Any>
141 where
142 Self: 'static,
143 {
144 self
145 }
146
147 fn any_arc_sync(self: Arc<Self>) -> Arc<dyn Send + Sync + Any>
148 where
149 Self: 'static + Send + Sync,
150 {
151 self
152 }
153}
154
155pub trait Resolve: Send + Sync + AsAny {
157 fn resolve(&'_ self, address: Address) -> FailFuture<'_, ByteNode>;
159 fn resolve_data(&'_ self, address: Address) -> FailFuture<'_, Vec<u8>>;
160 fn try_resolve_local(&self, address: Address) -> Result<Option<ByteNode>> {
161 let _ = address;
162 Ok(None)
163 }
164 fn resolve_extension(&self, address: Address, typeid: TypeId) -> crate::Result<&dyn Any> {
166 let _ = address;
167 let _ = typeid;
168 Err(Error::UnknownExtension)
169 }
170 fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
172 let _ = typeid;
173 Err(Error::UnknownExtension)
174 }
175}
176
177pub trait FetchBytes: AsAny {
178 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode>;
179 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>>;
180 fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
181 Ok(None)
182 }
183 fn fetch_data_local(&self) -> Option<Vec<u8>> {
184 None
185 }
186 fn as_inner(&self) -> Option<&dyn Any> {
187 None
188 }
189}
190
191pub trait Fetch: Send + Sync + FetchBytes {
192 type T;
193 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>>;
194 fn fetch(&'_ self) -> FailFuture<'_, Self::T>;
195 fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
196 Ok(None)
197 }
198 fn fetch_local(&self) -> Option<Self::T> {
199 None
200 }
201 fn get(&self) -> Option<&Self::T> {
202 None
203 }
204 fn get_mut(&mut self) -> Option<&mut Self::T> {
205 None
206 }
207 fn get_mut_finalize(&mut self) {}
208}
209
210#[derive(ToOutput, InlineOutput, ListHashes, Topological, Parse, ParseInline)]
211pub struct ObjectMarker<T: ?Sized> {
212 object: PhantomData<fn() -> T>,
213}
214
215impl<T: ?Sized> Clone for ObjectMarker<T> {
216 fn clone(&self) -> Self {
217 *self
218 }
219}
220
221impl<T: ?Sized> Copy for ObjectMarker<T> {}
222
223impl<T: ?Sized> Default for ObjectMarker<T> {
224 fn default() -> Self {
225 Self {
226 object: Default::default(),
227 }
228 }
229}
230
231impl<T: ?Sized + Tagged> Tagged for ObjectMarker<T> {}
232
233pub trait PointVisitor {
234 fn visit<T: Traversible>(&mut self, point: &(impl 'static + SingularFetch<T = T> + Clone));
235}
236
237pub struct ReflessInput<'d> {
238 data: Option<&'d [u8]>,
239}
240
241pub struct Input<'d, Extra = ()> {
242 refless: ReflessInput<'d>,
243 resolve: &'d Arc<dyn Resolve>,
244 index: &'d Cell<usize>,
245 extra: &'d Extra,
246}
247
248impl<'a, Extra> Deref for Input<'a, Extra> {
249 type Target = ReflessInput<'a>;
250
251 fn deref(&self) -> &Self::Target {
252 &self.refless
253 }
254}
255
256impl<Extra> DerefMut for Input<'_, Extra> {
257 fn deref_mut(&mut self) -> &mut Self::Target {
258 &mut self.refless
259 }
260}
261
262impl<'a, Extra> Input<'a, Extra> {
263 pub fn replace_extra<E>(self, extra: &'a E) -> Input<'a, E> {
264 Input {
265 refless: self.refless,
266 resolve: self.resolve,
267 index: self.index,
268 extra,
269 }
270 }
271}
272
273impl<'a> ReflessInput<'a> {
274 fn data(&self) -> crate::Result<&'a [u8]> {
275 self.data.ok_or(Error::EndOfInput)
276 }
277
278 fn make_error<T>(&mut self, e: crate::Error) -> crate::Result<T> {
279 self.data = None;
280 Err(e)
281 }
282
283 fn end_of_input<T>(&mut self) -> crate::Result<T> {
284 self.make_error(Error::EndOfInput)
285 }
286}
287
288impl<'d> ParseInput for ReflessInput<'d> {
289 type Data = &'d [u8];
290
291 fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
292 where
293 Self: 'a,
294 {
295 match self.data()?.split_first_chunk() {
296 Some((chunk, data)) => {
297 self.data = Some(data);
298 Ok(chunk)
299 }
300 None => self.end_of_input(),
301 }
302 }
303
304 fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data> {
305 match self.data()?.split_at_checked(n) {
306 Some((chunk, data)) => {
307 self.data = Some(data);
308 Ok(chunk)
309 }
310 None => self.end_of_input(),
311 }
312 }
313
314 fn parse_until_zero(&mut self) -> crate::Result<Self::Data> {
315 let data = self.data()?;
316 match data.iter().enumerate().find(|(_, x)| **x == 0) {
317 Some((at, _)) => {
318 let (chunk, data) = data.split_at(at);
319 self.data = Some(&data[1..]);
320 Ok(chunk)
321 }
322 None => self.end_of_input(),
323 }
324 }
325
326 fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T> {
327 let input = Self { data: Some(data) };
328 T::parse(input)
329 }
330
331 fn parse_all(self) -> crate::Result<Self::Data> {
332 self.data()
333 }
334
335 fn empty(self) -> crate::Result<()> {
336 if self.data()?.is_empty() {
337 Ok(())
338 } else {
339 Err(Error::ExtraInputLeft)
340 }
341 }
342
343 fn non_empty(self) -> crate::Result<Option<Self>> {
344 Ok(if self.data()?.is_empty() {
345 None
346 } else {
347 Some(self)
348 })
349 }
350}
351
352impl<'d, Extra> ParseInput for Input<'d, Extra> {
353 type Data = &'d [u8];
354
355 fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
356 where
357 Self: 'a,
358 {
359 (**self).parse_chunk()
360 }
361
362 fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data> {
363 (**self).parse_n(n)
364 }
365
366 fn parse_until_zero(&mut self) -> crate::Result<Self::Data> {
367 (**self).parse_until_zero()
368 }
369
370 fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T> {
371 let input = Self {
372 refless: ReflessInput { data: Some(data) },
373 resolve: self.resolve,
374 index: self.index,
375 extra: self.extra,
376 };
377 T::parse(input)
378 }
379
380 fn parse_all(self) -> crate::Result<Self::Data> {
381 self.refless.parse_all()
382 }
383
384 fn empty(self) -> crate::Result<()> {
385 self.refless.empty()
386 }
387
388 fn non_empty(mut self) -> crate::Result<Option<Self>> {
389 self.refless = match self.refless.non_empty()? {
390 Some(refless) => refless,
391 None => return Ok(None),
392 };
393 Ok(Some(self))
394 }
395}
396
397impl<'d, Extra: 'static + Clone> PointInput for Input<'d, Extra> {
398 type Extra = Extra;
399 type WithExtra<E: 'static + Clone> = Input<'d, E>;
400
401 fn next_index(&mut self) -> usize {
402 let index = self.index.get();
403 self.index.set(index + 1);
404 index
405 }
406
407 fn resolve_arc_ref(&self) -> &Arc<dyn Resolve> {
408 self.resolve
409 }
410
411 fn extra(&self) -> &Self::Extra {
412 self.extra
413 }
414
415 fn map_extra<E: 'static + Clone>(
416 self,
417 f: impl FnOnce(&Self::Extra) -> &E,
418 ) -> Self::WithExtra<E> {
419 let Self {
420 refless,
421 resolve,
422 index,
423 extra,
424 } = self;
425 Input {
426 refless,
427 resolve,
428 index,
429 extra: f(extra),
430 }
431 }
432}
433
434pub trait ToOutput {
435 fn to_output(&self, output: &mut dyn Output);
436
437 fn data_hash(&self) -> Hash {
438 let mut output = HashOutput::default();
439 self.to_output(&mut output);
440 output.hash()
441 }
442
443 fn output<T: Output + Default>(&self) -> T {
444 let mut output = T::default();
445 self.to_output(&mut output);
446 output
447 }
448
449 fn vec(&self) -> Vec<u8> {
450 self.output()
451 }
452}
453
454pub trait InlineOutput: ToOutput {
456 fn slice_to_output(slice: &[Self], output: &mut dyn Output)
457 where
458 Self: Sized,
459 {
460 slice.iter_to_output(output);
461 }
462}
463
464pub trait ListHashes {
465 fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
466 let _ = f;
467 }
468
469 fn topology_hash(&self) -> Hash {
470 let mut hasher = Sha256::new();
471 self.list_hashes(&mut |hash| hasher.update(hash));
472 Hash::from_sha256(hasher.finalize().into())
473 }
474
475 fn point_count(&self) -> usize {
476 let mut count = 0;
477 self.list_hashes(&mut |_| count += 1);
478 count
479 }
480}
481
482pub trait Topological: ListHashes {
483 fn traverse(&self, visitor: &mut impl PointVisitor) {
484 let _ = visitor;
485 }
486
487 fn topology(&self) -> TopoVec {
488 let mut topology = TopoVec::with_capacity(self.point_count());
489 self.traverse(&mut topology);
490 topology
491 }
492}
493
494pub trait Tagged {
495 const TAGS: Tags = Tags(&[], &[]);
496
497 const HASH: Hash =
498 const { Hash::from_sha256(Self::TAGS.const_hash(sha2_const::Sha256::new()).finalize()) };
499}
500
501pub trait ParseSlice: for<'a> Parse<Input<'a>> {
502 fn parse_slice(data: &[u8], resolve: &Arc<dyn Resolve>) -> crate::Result<Self> {
503 Self::parse_slice_extra(data, resolve, &())
504 }
505
506 fn reparse(&self) -> crate::Result<Self>
507 where
508 Self: Traversible,
509 {
510 self.reparse_extra(&())
511 }
512}
513
514impl<T: for<'a> Parse<Input<'a>>> ParseSlice for T {}
515
516pub trait ParseSliceExtra<Extra>: for<'a> Parse<Input<'a, Extra>> {
517 fn parse_slice_extra(
518 data: &[u8],
519 resolve: &Arc<dyn Resolve>,
520 extra: &Extra,
521 ) -> crate::Result<Self> {
522 let input = Input {
523 refless: ReflessInput { data: Some(data) },
524 resolve,
525 index: &Cell::new(0),
526 extra,
527 };
528 let object = Self::parse(input)?;
529 Ok(object)
530 }
531
532 fn reparse_extra(&self, extra: &Extra) -> crate::Result<Self>
533 where
534 Self: Traversible,
535 {
536 Self::parse_slice_extra(&self.vec(), &self.to_resolve(), extra)
537 }
538}
539
540impl<T: for<'a> Parse<Input<'a, Extra>>, Extra> ParseSliceExtra<Extra> for T {}
541
542#[derive(ToOutput)]
543pub struct ObjectHashes {
544 pub tags: Hash,
545 pub topology: Hash,
546 pub data: Hash,
547}
548
549pub trait FullHash: ToOutput + ListHashes + Tagged {
550 fn hashes(&self) -> ObjectHashes {
551 ObjectHashes {
552 tags: Self::HASH,
553 topology: self.topology_hash(),
554 data: self.data_hash(),
555 }
556 }
557
558 fn full_hash(&self) -> Hash {
559 self.hashes().data_hash()
560 }
561}
562
563impl<T: ?Sized + ToOutput + ListHashes + Tagged> FullHash for T {}
564
565pub trait Traversible: 'static + Sized + Send + Sync + FullHash + Topological {
566 fn to_resolve(&self) -> Arc<dyn Resolve> {
567 Arc::new(ByTopology {
568 topology: self.topology(),
569 })
570 }
571}
572
573impl<T: 'static + Send + Sync + FullHash + Topological> Traversible for T {}
574
575pub trait Object<Extra = ()>: Traversible + for<'a> Parse<Input<'a, Extra>> {}
576
577impl<T: Traversible + for<'a> Parse<Input<'a, Extra>>, Extra> Object<Extra> for T {}
578
579pub struct Tags(pub &'static [&'static str], pub &'static [&'static Self]);
580
581impl Tags {
582 const fn const_hash(&self, mut hasher: sha2_const::Sha256) -> sha2_const::Sha256 {
583 {
584 let mut i = 0;
585 while i < self.0.len() {
586 hasher = hasher.update(self.0[i].as_bytes());
587 i += 1;
588 }
589 }
590 {
591 let mut i = 0;
592 while i < self.1.len() {
593 hasher = self.1[i].const_hash(hasher);
594 i += 1;
595 }
596 }
597 hasher
598 }
599}
600
601pub trait Inline<Extra = ()>:
602 Object<Extra> + InlineOutput + for<'a> ParseInline<Input<'a, Extra>>
603{
604}
605
606impl<T: Object<Extra> + InlineOutput + for<'a> ParseInline<Input<'a, Extra>>, Extra> Inline<Extra>
607 for T
608{
609}
610
611pub trait Topology: Send + Sync {
612 fn len(&self) -> usize;
613 fn get(&self, index: usize) -> Option<&Arc<dyn Singular>>;
614
615 fn is_empty(&self) -> bool {
616 self.len() == 0
617 }
618}
619
620pub trait Singular: Send + Sync + FetchBytes {
621 fn hash(&self) -> Hash;
622}
623
624pub trait SingularFetch: Singular + Fetch {}
625
626impl<T: ?Sized + Singular + Fetch> SingularFetch for T {}
627
628pub type TopoVec = Vec<Arc<dyn Singular>>;
629
630impl PointVisitor for TopoVec {
631 fn visit<T: Traversible>(&mut self, point: &(impl 'static + SingularFetch<T = T> + Clone)) {
632 self.push(Arc::new(point.clone()));
633 }
634}
635
636impl Topology for TopoVec {
637 fn len(&self) -> usize {
638 self.len()
639 }
640
641 fn get(&self, index: usize) -> Option<&Arc<dyn Singular>> {
642 (**self).get(index)
643 }
644}
645
646pub trait ParseSliceRefless: for<'a> Parse<ReflessInput<'a>> {
647 fn parse_slice_refless(data: &[u8]) -> crate::Result<Self> {
648 let input = ReflessInput { data: Some(data) };
649 let object = Self::parse(input)?;
650 Ok(object)
651 }
652}
653
654impl<T: for<'a> Parse<ReflessInput<'a>>> ParseSliceRefless for T {}
655
656pub trait ReflessObject:
657 'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>
658{
659}
660
661impl<T: 'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>>
662 ReflessObject for T
663{
664}
665
666pub trait ReflessInline:
667 ReflessObject + InlineOutput + for<'a> ParseInline<ReflessInput<'a>>
668{
669}
670
671impl<T: ReflessObject + InlineOutput + for<'a> ParseInline<ReflessInput<'a>>> ReflessInline for T {}
672
673pub trait Output {
674 fn write(&mut self, data: &[u8]);
675}
676
677impl Output for Vec<u8> {
678 fn write(&mut self, data: &[u8]) {
679 self.extend_from_slice(data);
680 }
681}
682
683#[derive(Default)]
684struct HashOutput {
685 hasher: Sha256,
686 at: usize,
687}
688
689impl Output for HashOutput {
690 fn write(&mut self, data: &[u8]) {
691 self.hasher.update(data);
692 self.at += data.len();
693 }
694}
695
696impl HashOutput {
697 fn hash(self) -> Hash {
698 Hash::from_sha256(self.hasher.finalize().into())
699 }
700}
701
702struct ByTopology {
703 topology: TopoVec,
704}
705
706impl ByTopology {
707 fn try_resolve(&'_ self, address: Address) -> Result<FailFuture<'_, ByteNode>> {
708 let point = self
709 .topology
710 .get(address.index)
711 .ok_or(Error::AddressOutOfBounds)?;
712 if point.hash() != address.hash {
713 Err(Error::ResolutionMismatch)
714 } else {
715 Ok(point.fetch_bytes())
716 }
717 }
718
719 fn try_resolve_data(&'_ self, address: Address) -> Result<FailFuture<'_, Vec<u8>>> {
720 let point = self
721 .topology
722 .get(address.index)
723 .ok_or(Error::AddressOutOfBounds)?;
724 if point.hash() != address.hash {
725 Err(Error::ResolutionMismatch)
726 } else {
727 Ok(point.fetch_data())
728 }
729 }
730}
731
732impl Resolve for ByTopology {
733 fn resolve(&'_ self, address: Address) -> FailFuture<'_, ByteNode> {
734 self.try_resolve(address)
735 .map_err(Err)
736 .map_err(ready)
737 .map_err(Box::pin)
738 .unwrap_or_else(|x| x)
739 }
740
741 fn resolve_data(&'_ self, address: Address) -> FailFuture<'_, Vec<u8>> {
742 self.try_resolve_data(address)
743 .map_err(Err)
744 .map_err(ready)
745 .map_err(Box::pin)
746 .unwrap_or_else(|x| x)
747 }
748
749 fn try_resolve_local(&self, address: Address) -> Result<Option<ByteNode>> {
750 let point = self
751 .topology
752 .get(address.index)
753 .ok_or(Error::AddressOutOfBounds)?;
754 if point.hash() != address.hash {
755 Err(Error::ResolutionMismatch)
756 } else {
757 point.fetch_bytes_local()
758 }
759 }
760}
761
762pub trait Size {
763 const SIZE: usize = <Self::Size as Unsigned>::USIZE;
764 type Size: Unsigned;
765}
766
767pub trait SizeExt: Size<Size: ArrayLength> + ToOutput {
768 fn to_array(&self) -> GenericArray<u8, Self::Size> {
769 let mut array = GenericArray::default();
770 let mut output = ArrayOutput {
771 data: &mut array,
772 offset: 0,
773 };
774 self.to_output(&mut output);
775 output.finalize();
776 array
777 }
778}
779
780impl<T: Size<Size: ArrayLength> + ToOutput> SizeExt for T {}
781
782struct ArrayOutput<'a> {
783 data: &'a mut [u8],
784 offset: usize,
785}
786
787impl ArrayOutput<'_> {
788 fn finalize(self) {
789 assert_eq!(self.offset, self.data.len());
790 }
791}
792
793impl Output for ArrayOutput<'_> {
794 fn write(&mut self, data: &[u8]) {
795 self.data[self.offset..][..data.len()].copy_from_slice(data);
796 self.offset += data.len();
797 }
798}
799
800trait RainbowIterator: Sized + IntoIterator {
801 fn iter_to_output(self, output: &mut dyn Output)
802 where
803 Self::Item: InlineOutput,
804 {
805 self.into_iter().for_each(|item| item.to_output(output));
806 }
807
808 fn iter_list_hashes(self, f: &mut impl FnMut(Hash))
809 where
810 Self::Item: ListHashes,
811 {
812 self.into_iter().for_each(|item| item.list_hashes(f));
813 }
814
815 fn iter_traverse(self, visitor: &mut impl PointVisitor)
816 where
817 Self::Item: Topological,
818 {
819 self.into_iter().for_each(|item| item.traverse(visitor));
820 }
821}
822
823pub trait ParseInput: Sized {
824 type Data: AsRef<[u8]> + Deref<Target = [u8]> + Into<Vec<u8>> + Copy;
825 fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
826 where
827 Self: 'a;
828 fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data>;
829 fn parse_until_zero(&mut self) -> crate::Result<Self::Data>;
830 fn parse_n_compare(&mut self, n: usize, c: &[u8]) -> crate::Result<Option<Self::Data>> {
831 let data = self.parse_n(n)?;
832 if *data == *c {
833 Ok(None)
834 } else {
835 Ok(Some(data))
836 }
837 }
838 fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T>;
839 fn parse_ahead<T: Parse<Self>>(&mut self, n: usize) -> crate::Result<T> {
840 let data = self.parse_n(n)?;
841 self.reparse(data)
842 }
843 fn parse_zero_terminated<T: Parse<Self>>(&mut self) -> crate::Result<T> {
844 let data = self.parse_until_zero()?;
845 self.reparse(data)
846 }
847 fn parse_compare<T: Parse<Self>>(&mut self, n: usize, c: &[u8]) -> Result<Option<T>> {
848 self.parse_n_compare(n, c)?
849 .map(|data| self.reparse(data))
850 .transpose()
851 }
852 fn parse_all(self) -> crate::Result<Self::Data>;
853 fn empty(self) -> crate::Result<()>;
854 fn non_empty(self) -> crate::Result<Option<Self>>;
855
856 fn consume(self, f: impl FnMut(&mut Self) -> crate::Result<()>) -> crate::Result<()> {
857 self.collect(f)
858 }
859
860 fn parse_collect<T: ParseInline<Self>, B: FromIterator<T>>(self) -> crate::Result<B> {
861 self.collect(|input| input.parse_inline())
862 }
863
864 fn collect<T, B: FromIterator<T>>(
865 self,
866 f: impl FnMut(&mut Self) -> crate::Result<T>,
867 ) -> crate::Result<B> {
868 self.iter(f).collect()
869 }
870
871 fn iter<T>(
872 self,
873 mut f: impl FnMut(&mut Self) -> crate::Result<T>,
874 ) -> impl Iterator<Item = crate::Result<T>> {
875 let mut state = Some(self);
876 std::iter::from_fn(move || {
877 let mut input = match state.take()?.non_empty() {
878 Ok(input) => input?,
879 Err(e) => return Some(Err(e)),
880 };
881 let item = f(&mut input);
882 state = Some(input);
883 Some(item)
884 })
885 }
886
887 fn parse_inline<T: ParseInline<Self>>(&mut self) -> crate::Result<T> {
888 T::parse_inline(self)
889 }
890
891 fn parse<T: Parse<Self>>(self) -> crate::Result<T> {
892 T::parse(self)
893 }
894
895 fn parse_vec<T: ParseInline<Self>>(self) -> crate::Result<Vec<T>> {
896 T::parse_vec(self)
897 }
898
899 fn parse_vec_n<T: ParseInline<Self>>(&mut self, n: usize) -> crate::Result<Vec<T>> {
900 T::parse_vec_n(self, n)
901 }
902}
903
904pub trait PointInput: ParseInput {
905 type Extra: 'static + Clone;
906 type WithExtra<E: 'static + Clone>: PointInput<Extra = E, WithExtra<Self::Extra> = Self>;
907 fn next_index(&mut self) -> usize;
908 fn resolve_arc_ref(&self) -> &Arc<dyn Resolve>;
909 fn resolve(&self) -> Arc<dyn Resolve> {
910 self.resolve_arc_ref().clone()
911 }
912 fn resolve_ref(&self) -> &dyn Resolve {
913 self.resolve_arc_ref().as_ref()
914 }
915 fn extension<T: Any>(&self) -> crate::Result<&T> {
916 self.resolve_ref()
917 .extension(TypeId::of::<T>())?
918 .downcast_ref()
919 .ok_or(Error::ExtensionType)
920 }
921 fn extra(&self) -> &Self::Extra;
922 fn map_extra<E: 'static + Clone>(
923 self,
924 f: impl FnOnce(&Self::Extra) -> &E,
925 ) -> Self::WithExtra<E>;
926}
927
928impl<T: Sized + IntoIterator> RainbowIterator for T {}
929
930pub trait Parse<I: ParseInput>: Sized {
931 fn parse(input: I) -> crate::Result<Self>;
932}
933
934pub trait ParseInline<I: ParseInput>: Parse<I> {
935 fn parse_inline(input: &mut I) -> crate::Result<Self>;
936 fn parse_as_inline(mut input: I) -> crate::Result<Self> {
937 let object = Self::parse_inline(&mut input)?;
938 input.empty()?;
939 Ok(object)
940 }
941 fn parse_vec(input: I) -> crate::Result<Vec<Self>> {
942 input.parse_collect()
943 }
944 fn parse_vec_n(input: &mut I, n: usize) -> crate::Result<Vec<Self>> {
945 (0..n).map(|_| input.parse_inline()).collect()
946 }
947}
948
949pub trait Equivalent<T>: Sized {
956 fn into_equivalent(self) -> T;
958 fn from_equivalent(object: T) -> Self;
960}
961
962pub trait ExtraFor<T> {
963 fn parse(&self, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T>;
964
965 fn parse_checked(&self, hash: Hash, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T>
966 where
967 T: FullHash,
968 {
969 let object = self.parse(data, resolve)?;
970 if object.full_hash() != hash {
971 Err(Error::FullHashMismatch)
972 } else {
973 Ok(object)
974 }
975 }
976}
977
978impl<T: for<'a> Parse<Input<'a, Extra>>, Extra> ExtraFor<T> for Extra {
979 fn parse(&self, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T> {
980 T::parse_slice_extra(data, resolve, self)
981 }
982}
983
984pub trait BoundPair: Sized {
985 type T;
986 type E;
987}
988
989impl<T, E> BoundPair for (T, E) {
990 type T = T;
991 type E = E;
992}
993
994#[test]
995fn options() {
996 type T0 = ();
997 type T1 = Option<T0>;
998 type T2 = Option<T1>;
999 type T3 = Option<T2>;
1000 type T4 = Option<T3>;
1001 type T5 = Option<T4>;
1002 assert_eq!(T0::SIZE, 0);
1003 assert_eq!(T1::SIZE, 1);
1004 assert_eq!(T2::SIZE, 1);
1005 assert_eq!(T3::SIZE, 1);
1006 assert_eq!(T4::SIZE, 1);
1007 assert_eq!(T5::SIZE, 1);
1008 assert_eq!(Some(Some(Some(()))).vec(), [0]);
1009 assert_eq!(Some(Some(None::<()>)).vec(), [1]);
1010 assert_eq!(Some(None::<Option<()>>).vec(), [2]);
1011 assert_eq!(None::<Option<Option<()>>>.vec(), [3]);
1012
1013 assert_eq!(false.vec(), [0]);
1014 assert_eq!(true.vec(), [1]);
1015 assert_eq!(Some(false).vec(), [0]);
1016 assert_eq!(Some(true).vec(), [1]);
1017 assert_eq!(None::<bool>.vec(), [2]);
1018 assert_eq!(Some(Some(false)).vec(), [0]);
1019 assert_eq!(Some(Some(true)).vec(), [1]);
1020 assert_eq!(Some(None::<bool>).vec(), [2]);
1021 assert_eq!(None::<Option<bool>>.vec(), [3]);
1022 assert_eq!(Some(Some(Some(false))).vec(), [0]);
1023 assert_eq!(Some(Some(Some(true))).vec(), [1]);
1024 assert_eq!(Some(Some(None::<bool>)).vec(), [2]);
1025 assert_eq!(Some(None::<Option<bool>>).vec(), [3]);
1026 assert_eq!(None::<Option<Option<bool>>>.vec(), [4]);
1027 assert_eq!(Option::<Hash>::SIZE, HASH_SIZE);
1028 assert_eq!(Some(()).vec(), [0]);
1029 assert_eq!(Some(((), ())).vec(), [0]);
1030 assert_eq!(Some(((), true)).vec(), [1]);
1031 assert_eq!(Some((true, true)).vec(), [1, 1]);
1032 assert_eq!(Some((Some(true), true)).vec(), [1, 1]);
1033 assert_eq!(Some((None::<bool>, true)).vec(), [2, 1]);
1034 assert_eq!(Some((true, None::<bool>)).vec(), [1, 2]);
1035 assert_eq!(None::<(Option<bool>, bool)>.vec(), [3, 2]);
1036 assert_eq!(None::<(bool, Option<bool>)>.vec(), [2, 3]);
1037 assert_eq!(Some(Some((Some(true), Some(true)))).vec(), [1, 1],);
1038 assert_eq!(Option::<Hash>::SIZE, HASH_SIZE);
1039 assert_eq!(Option::<Option<Hash>>::SIZE, HASH_SIZE);
1040 assert_eq!(Option::<Option<Option<Hash>>>::SIZE, HASH_SIZE);
1041}