object_rainbow/
lib.rs

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 futures_util::TryFutureExt;
18use generic_array::{ArrayLength, GenericArray};
19pub use object_rainbow_derive::{
20    Enum, InlineOutput, ListHashes, MaybeHasNiche, Parse, ParseAsInline, ParseInline, Size, Tagged,
21    ToOutput, Topological,
22};
23use sha2::{Digest, Sha256};
24#[doc(hidden)]
25pub use typenum;
26use typenum::Unsigned;
27
28pub use self::enumkind::Enum;
29pub use self::error::{Error, Result};
30pub use self::hash::{Hash, OptionalHash};
31pub use self::niche::{
32    AutoEnumNiche, AutoNiche, HackNiche, MaybeHasNiche, Niche, NicheForUnsized, NoNiche, OneNiche,
33    SomeNiche, ZeroNiche, ZeroNoNiche,
34};
35#[doc(hidden)]
36pub use self::niche::{MaybeNiche, MnArray, NicheFoldOrArray, NicheOr};
37
38pub mod enumkind;
39mod error;
40mod hash;
41pub mod hashed;
42mod impls;
43pub mod length_prefixed;
44mod niche;
45pub mod numeric;
46#[cfg(feature = "serde")]
47mod point_deserialize;
48#[cfg(feature = "point-serialize")]
49mod point_serialize;
50mod sha2_const;
51pub mod zero_terminated;
52
53/// SHA-256 hash size in bytes.
54pub const HASH_SIZE: usize = sha2_const::Sha256::DIGEST_SIZE;
55
56/// Address within a [`PointInput`].
57///
58/// This was introduced:
59/// - to avoid using a [`Hash`]-only map
60/// - to differentiate between separate [`Point`]s within a context
61#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, ParseAsInline)]
62pub struct Address {
63    /// Monotonically incremented index. This is not present at all in the actual format.
64    pub index: usize,
65    /// Only this part is part of the parsed/generated input.
66    pub hash: Hash,
67}
68
69impl Address {
70    /// Construct an address which is invalid within parsing context, but can be used in map-based
71    /// [`Resolve`]s.
72    pub fn from_hash(hash: Hash) -> Self {
73        Self {
74            index: usize::MAX,
75            hash,
76        }
77    }
78}
79
80impl<I: PointInput> ParseInline<I> for Address {
81    fn parse_inline(input: &mut I) -> crate::Result<Self> {
82        Ok(Self {
83            index: input.next_index(),
84            hash: input.parse_inline()?,
85        })
86    }
87}
88
89/// Fallible future type yielding either `T` or [`Error`].
90pub type FailFuture<'a, T> = Pin<Box<dyn 'a + Send + Future<Output = Result<T>>>>;
91
92pub type Node<T> = (T, Arc<dyn Resolve>);
93
94/// Returned by [`Resolve`] and [`FetchBytes`]. Represents traversal through the object graph.
95pub type ByteNode = Node<Vec<u8>>;
96
97trait FromInner {
98    type Inner: 'static + Clone;
99    type Extra: 'static + Clone;
100
101    fn from_inner(inner: Self::Inner, extra: Self::Extra) -> Self;
102}
103
104/// Trait for contextually using [`Any`]. Can itself be implemented for non-`'static` and `?Sized`
105/// types, and is `dyn`-compatible.
106pub trait AsAny {
107    /// Get a shared RTTI reference.
108    fn any_ref(&self) -> &dyn Any
109    where
110        Self: 'static;
111    /// Get an exclusive RTTI reference.
112    fn any_mut(&mut self) -> &mut dyn Any
113    where
114        Self: 'static;
115    /// Get an RTTI [`Box`].
116    fn any_box(self: Box<Self>) -> Box<dyn Any>
117    where
118        Self: 'static;
119    /// Get an RTTI [`Arc`].
120    fn any_arc(self: Arc<Self>) -> Arc<dyn Any>
121    where
122        Self: 'static;
123    /// Get an RTTI [`Arc`] which is also [`Send`].
124    fn any_arc_sync(self: Arc<Self>) -> Arc<dyn Send + Sync + Any>
125    where
126        Self: 'static + Send + Sync;
127}
128
129impl<T> AsAny for T {
130    fn any_ref(&self) -> &dyn Any
131    where
132        Self: 'static,
133    {
134        self
135    }
136
137    fn any_mut(&mut self) -> &mut dyn Any
138    where
139        Self: 'static,
140    {
141        self
142    }
143
144    fn any_box(self: Box<Self>) -> Box<dyn Any>
145    where
146        Self: 'static,
147    {
148        self
149    }
150
151    fn any_arc(self: Arc<Self>) -> Arc<dyn Any>
152    where
153        Self: 'static,
154    {
155        self
156    }
157
158    fn any_arc_sync(self: Arc<Self>) -> Arc<dyn Send + Sync + Any>
159    where
160        Self: 'static + Send + Sync,
161    {
162        self
163    }
164}
165
166/// Something that resolve [`Address`]es to [`ByteNode`]s.
167pub trait Resolve: Send + Sync + AsAny {
168    /// Resolve the address. For an [`Object`], this is what gets used as [`PointInput`].
169    fn resolve(&'_ self, address: Address) -> FailFuture<'_, ByteNode>;
170    fn resolve_data(&'_ self, address: Address) -> FailFuture<'_, Vec<u8>>;
171    fn try_resolve_local(&self, address: Address) -> Result<Option<ByteNode>> {
172        let _ = address;
173        Ok(None)
174    }
175    /// Get a dynamic extension for a specific [`Address`].
176    fn resolve_extension(&self, address: Address, typeid: TypeId) -> crate::Result<&dyn Any> {
177        let _ = address;
178        let _ = typeid;
179        Err(Error::UnknownExtension)
180    }
181    /// Get a dynamic extension.
182    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
183        let _ = typeid;
184        Err(Error::UnknownExtension)
185    }
186}
187
188pub trait FetchBytes: AsAny {
189    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode>;
190    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>>;
191    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
192        Ok(None)
193    }
194    fn fetch_data_local(&self) -> Option<Vec<u8>> {
195        None
196    }
197    fn as_inner(&self) -> Option<&dyn Any> {
198        None
199    }
200}
201
202pub trait Fetch: Send + Sync + FetchBytes {
203    type T;
204    fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>>;
205    fn fetch(&'_ self) -> FailFuture<'_, Self::T>;
206    fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
207        Ok(None)
208    }
209    fn fetch_local(&self) -> Option<Self::T> {
210        None
211    }
212    fn get(&self) -> Option<&Self::T> {
213        None
214    }
215    fn get_mut(&mut self) -> Option<&mut Self::T> {
216        None
217    }
218    fn get_mut_finalize(&mut self) {}
219}
220
221trait InnerCast: FetchBytes {
222    fn inner_cast<T: FromInner>(&self, extra: &T::Extra) -> Option<T> {
223        self.as_inner()?
224            .downcast_ref()
225            .cloned()
226            .map(|inner| T::from_inner(inner, extra.clone()))
227    }
228}
229
230impl<T: ?Sized + FetchBytes> InnerCast for T {}
231
232pub trait ExtractResolve: FetchBytes {
233    fn extract_resolve<R: Any>(&self) -> Option<(&Address, &R)> {
234        let ByAddressInner { address, resolve } =
235            self.as_inner()?.downcast_ref::<ByAddressInner>()?;
236        let resolve = resolve.as_ref().any_ref().downcast_ref::<R>()?;
237        Some((address, resolve))
238    }
239}
240
241impl<T: ?Sized + FetchBytes> ExtractResolve for T {}
242
243#[derive(Clone, ParseAsInline)]
244pub struct RawPointInner {
245    hash: Hash,
246    fetch: Arc<dyn Send + Sync + FetchBytes>,
247}
248
249impl RawPointInner {
250    pub fn cast<T, Extra: 'static + Clone>(self, extra: Extra) -> RawPoint<T, Extra> {
251        RawPoint::from_inner(self, extra)
252    }
253
254    pub fn from_address(address: Address, resolve: Arc<dyn Resolve>) -> Self {
255        Self {
256            hash: address.hash,
257            fetch: Arc::new(ByAddressInner { address, resolve }),
258        }
259    }
260
261    pub fn from_singular(singular: impl 'static + Singular) -> Self {
262        Self {
263            hash: singular.hash(),
264            fetch: Arc::new(singular),
265        }
266    }
267}
268
269impl ToOutput for RawPointInner {
270    fn to_output(&self, output: &mut dyn Output) {
271        self.hash.to_output(output);
272    }
273}
274
275impl InlineOutput for RawPointInner {}
276
277impl<I: PointInput> ParseInline<I> for RawPointInner {
278    fn parse_inline(input: &mut I) -> crate::Result<Self> {
279        Ok(Self::from_address(input.parse_inline()?, input.resolve()))
280    }
281}
282
283impl Tagged for RawPointInner {}
284
285impl Singular for RawPointInner {
286    fn hash(&self) -> Hash {
287        self.hash
288    }
289}
290
291impl<T, Extra: Send + Sync> Singular for RawPoint<T, Extra> {
292    fn hash(&self) -> Hash {
293        self.inner.hash()
294    }
295}
296
297#[derive(ToOutput, InlineOutput, ListHashes, Topological, Parse, ParseInline)]
298pub struct ObjectMarker<T: ?Sized> {
299    object: PhantomData<fn() -> T>,
300}
301
302impl<T: ?Sized> Clone for ObjectMarker<T> {
303    fn clone(&self) -> Self {
304        *self
305    }
306}
307
308impl<T: ?Sized> Copy for ObjectMarker<T> {}
309
310impl<T: ?Sized> Default for ObjectMarker<T> {
311    fn default() -> Self {
312        Self {
313            object: Default::default(),
314        }
315    }
316}
317
318impl<T: ?Sized + Tagged> Tagged for ObjectMarker<T> {}
319
320#[derive(Clone, ParseAsInline)]
321struct Extras<Extra>(Extra);
322
323impl<Extra> Deref for Extras<Extra> {
324    type Target = Extra;
325
326    fn deref(&self) -> &Self::Target {
327        &self.0
328    }
329}
330
331impl<Extra> ToOutput for Extras<Extra> {
332    fn to_output(&self, _: &mut dyn Output) {}
333}
334
335impl<Extra> InlineOutput for Extras<Extra> {}
336
337impl<I: PointInput> ParseInline<I> for Extras<I::Extra> {
338    fn parse_inline(input: &mut I) -> crate::Result<Self> {
339        Ok(Self(input.extra().clone()))
340    }
341}
342
343impl<Extra> Tagged for Extras<Extra> {}
344impl<Extra> ListHashes for Extras<Extra> {}
345impl<Extra> Topological for Extras<Extra> {}
346
347#[derive(ToOutput, InlineOutput, Tagged, Parse, ParseInline)]
348pub struct RawPoint<T = Infallible, Extra = ()> {
349    inner: RawPointInner,
350    extra: Extras<Extra>,
351    object: ObjectMarker<T>,
352}
353
354impl ListHashes for RawPointInner {
355    fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
356        f(self.hash)
357    }
358
359    fn point_count(&self) -> usize {
360        1
361    }
362}
363
364impl<T, Extra> ListHashes for RawPoint<T, Extra> {
365    fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
366        self.inner.list_hashes(f);
367    }
368
369    fn topology_hash(&self) -> Hash {
370        self.inner.topology_hash()
371    }
372
373    fn point_count(&self) -> usize {
374        self.inner.point_count()
375    }
376}
377
378impl<T: 'static + Traversible, Extra: 'static + Send + Sync + Clone + ExtraFor<T>> Topological
379    for RawPoint<T, Extra>
380{
381    fn traverse(&self, visitor: &mut impl PointVisitor) {
382        visitor.visit(self);
383    }
384}
385
386impl<T, Extra: 'static + Clone> FromInner for RawPoint<T, Extra> {
387    type Inner = RawPointInner;
388    type Extra = Extra;
389
390    fn from_inner(inner: Self::Inner, extra: Self::Extra) -> Self {
391        RawPoint {
392            inner,
393            extra: Extras(extra),
394            object: Default::default(),
395        }
396    }
397}
398
399impl<T, Extra: Clone> Clone for RawPoint<T, Extra> {
400    fn clone(&self) -> Self {
401        Self {
402            inner: self.inner.clone(),
403            extra: self.extra.clone(),
404            object: Default::default(),
405        }
406    }
407}
408
409impl<T> Point<T> {
410    pub fn raw<Extra: 'static + Clone>(self, extra: Extra) -> RawPoint<T, Extra> {
411        {
412            if let Some(raw) = self.fetch.inner_cast(&extra) {
413                return raw;
414            }
415        }
416        RawPointInner {
417            hash: self.hash.unwrap(),
418            fetch: self.fetch,
419        }
420        .cast(extra)
421    }
422}
423
424impl<T, Extra: 'static + Clone> RawPoint<T, Extra> {
425    pub fn cast<U>(self) -> RawPoint<U, Extra> {
426        self.inner.cast(self.extra.0)
427    }
428}
429
430impl<T: 'static + FullHash, Extra: 'static + Send + Sync + ExtraFor<T>> RawPoint<T, Extra> {
431    pub fn into_point(self) -> Point<T> {
432        Point::from_fetch(self.inner.hash, Arc::new(self))
433    }
434}
435
436impl FetchBytes for RawPointInner {
437    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
438        self.fetch.fetch_bytes()
439    }
440
441    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
442        self.fetch.fetch_data()
443    }
444
445    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
446        self.fetch.fetch_bytes_local()
447    }
448
449    fn fetch_data_local(&self) -> Option<Vec<u8>> {
450        self.fetch.fetch_data_local()
451    }
452}
453
454impl<T, Extra> FetchBytes for RawPoint<T, Extra> {
455    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
456        self.inner.fetch_bytes()
457    }
458
459    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
460        self.inner.fetch_data()
461    }
462
463    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
464        self.inner.fetch_bytes_local()
465    }
466
467    fn fetch_data_local(&self) -> Option<Vec<u8>> {
468        self.inner.fetch_data_local()
469    }
470
471    fn as_inner(&self) -> Option<&dyn Any> {
472        Some(&self.inner)
473    }
474}
475
476impl<T: FullHash, Extra: Send + Sync + ExtraFor<T>> Fetch for RawPoint<T, Extra> {
477    type T = T;
478
479    fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
480        Box::pin(async {
481            let (data, resolve) = self.inner.fetch.fetch_bytes().await?;
482            let object = self.extra.0.parse(&data, &resolve)?;
483            if self.inner.hash != object.full_hash() {
484                Err(Error::FullHashMismatch)
485            } else {
486                Ok((object, resolve))
487            }
488        })
489    }
490
491    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
492        Box::pin(async {
493            let (data, resolve) = self.inner.fetch.fetch_bytes().await?;
494            let object = self.extra.0.parse(&data, &resolve)?;
495            if self.inner.hash != object.full_hash() {
496                Err(Error::FullHashMismatch)
497            } else {
498                Ok(object)
499            }
500        })
501    }
502
503    fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
504        let Some((data, resolve)) = self.inner.fetch.fetch_bytes_local()? else {
505            return Ok(None);
506        };
507        let object = self.extra.0.parse(&data, &resolve)?;
508        if self.inner.hash != object.full_hash() {
509            Err(Error::FullHashMismatch)
510        } else {
511            Ok(Some((object, resolve)))
512        }
513    }
514}
515
516#[derive(ParseAsInline)]
517#[must_use]
518pub struct Point<T> {
519    hash: OptionalHash,
520    fetch: Arc<dyn Fetch<T = T>>,
521}
522
523impl<T> PartialOrd for Point<T> {
524    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
525        Some(self.cmp(other))
526    }
527}
528
529impl<T> Ord for Point<T> {
530    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
531        self.hash().cmp(&other.hash())
532    }
533}
534
535impl<T> Eq for Point<T> {}
536
537impl<T> PartialEq for Point<T> {
538    fn eq(&self, other: &Self) -> bool {
539        self.hash() == other.hash()
540    }
541}
542
543impl<T> Clone for Point<T> {
544    fn clone(&self) -> Self {
545        Self {
546            hash: self.hash,
547            fetch: self.fetch.clone(),
548        }
549    }
550}
551
552impl<T> Point<T> {
553    pub fn from_fetch(hash: Hash, fetch: Arc<dyn Fetch<T = T>>) -> Self {
554        Self {
555            hash: hash.into(),
556            fetch,
557        }
558    }
559
560    fn map_fetch<U>(
561        self,
562        f: impl FnOnce(Arc<dyn Fetch<T = T>>) -> Arc<dyn Fetch<T = U>>,
563    ) -> Point<U> {
564        Point {
565            hash: self.hash,
566            fetch: f(self.fetch),
567        }
568    }
569}
570
571impl<T> Size for Point<T> {
572    const SIZE: usize = HASH_SIZE;
573    type Size = typenum::generic_const_mappings::U<HASH_SIZE>;
574}
575
576impl<T: 'static + FullHash> Point<T>
577where
578    (): ExtraFor<T>,
579{
580    pub fn from_address(address: Address, resolve: Arc<dyn Resolve>) -> Self {
581        Self::from_address_extra(address, resolve, ())
582    }
583}
584
585impl<T: 'static + FullHash> Point<T> {
586    pub fn from_address_extra<Extra: 'static + Send + Sync + Clone + ExtraFor<T>>(
587        address: Address,
588        resolve: Arc<dyn Resolve>,
589        extra: Extra,
590    ) -> Self {
591        Self::from_fetch(
592            address.hash,
593            Arc::new(ByAddress::from_inner(
594                ByAddressInner { address, resolve },
595                extra,
596            )),
597        )
598    }
599
600    pub fn with_resolve<Extra: 'static + Send + Sync + Clone + ExtraFor<T>>(
601        &self,
602        resolve: Arc<dyn Resolve>,
603        extra: Extra,
604    ) -> Self {
605        Self::from_address_extra(Address::from_hash(self.hash()), resolve, extra)
606    }
607}
608
609#[derive(Clone)]
610struct ByAddressInner {
611    address: Address,
612    resolve: Arc<dyn Resolve>,
613}
614
615impl FetchBytes for ByAddressInner {
616    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
617        self.resolve.resolve(self.address)
618    }
619
620    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
621        self.resolve.resolve_data(self.address)
622    }
623
624    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
625        self.resolve.try_resolve_local(self.address)
626    }
627}
628
629impl Singular for ByAddressInner {
630    fn hash(&self) -> Hash {
631        self.address.hash
632    }
633}
634
635struct ByAddress<T, Extra> {
636    inner: ByAddressInner,
637    extra: Extra,
638    _object: PhantomData<fn() -> T>,
639}
640
641impl<T, Extra> ByAddress<T, Extra> {
642    fn from_inner(inner: ByAddressInner, extra: Extra) -> Self {
643        Self {
644            inner,
645            extra,
646            _object: PhantomData,
647        }
648    }
649}
650
651impl<T, Extra> FetchBytes for ByAddress<T, Extra> {
652    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
653        self.inner.fetch_bytes()
654    }
655
656    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
657        self.inner.fetch_data()
658    }
659
660    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
661        self.inner.fetch_bytes_local()
662    }
663
664    fn as_inner(&self) -> Option<&dyn Any> {
665        Some(&self.inner)
666    }
667}
668
669impl<T, Extra: Send + Sync> Singular for ByAddress<T, Extra> {
670    fn hash(&self) -> Hash {
671        self.inner.hash()
672    }
673}
674
675impl<T: FullHash, Extra: Send + Sync + ExtraFor<T>> Fetch for ByAddress<T, Extra> {
676    type T = T;
677
678    fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
679        Box::pin(async {
680            let (data, resolve) = self.fetch_bytes().await?;
681            let object = self.extra.parse(&data, &resolve)?;
682            if self.inner.address.hash != object.full_hash() {
683                Err(Error::FullHashMismatch)
684            } else {
685                Ok((object, resolve))
686            }
687        })
688    }
689
690    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
691        Box::pin(async {
692            let (data, resolve) = self.fetch_bytes().await?;
693            let object = self.extra.parse(&data, &resolve)?;
694            if self.inner.address.hash != object.full_hash() {
695                Err(Error::FullHashMismatch)
696            } else {
697                Ok(object)
698            }
699        })
700    }
701
702    fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
703        let Some((data, resolve)) = self.fetch_bytes_local()? else {
704            return Ok(None);
705        };
706        let object = self.extra.parse(&data, &resolve)?;
707        if self.inner.address.hash != object.full_hash() {
708            Err(Error::FullHashMismatch)
709        } else {
710            Ok(Some((object, resolve)))
711        }
712    }
713}
714
715pub trait PointVisitor {
716    fn visit<T: Traversible>(&mut self, point: &(impl 'static + SingularFetch<T = T> + Clone));
717}
718
719pub struct ReflessInput<'d> {
720    data: Option<&'d [u8]>,
721}
722
723pub struct Input<'d, Extra = ()> {
724    refless: ReflessInput<'d>,
725    resolve: &'d Arc<dyn Resolve>,
726    index: &'d Cell<usize>,
727    extra: &'d Extra,
728}
729
730impl<'a, Extra> Deref for Input<'a, Extra> {
731    type Target = ReflessInput<'a>;
732
733    fn deref(&self) -> &Self::Target {
734        &self.refless
735    }
736}
737
738impl<Extra> DerefMut for Input<'_, Extra> {
739    fn deref_mut(&mut self) -> &mut Self::Target {
740        &mut self.refless
741    }
742}
743
744impl<'a, Extra> Input<'a, Extra> {
745    pub fn replace_extra<E>(self, extra: &'a E) -> Input<'a, E> {
746        Input {
747            refless: self.refless,
748            resolve: self.resolve,
749            index: self.index,
750            extra,
751        }
752    }
753}
754
755impl<'a> ReflessInput<'a> {
756    fn data(&self) -> crate::Result<&'a [u8]> {
757        self.data.ok_or(Error::EndOfInput)
758    }
759
760    fn make_error<T>(&mut self, e: crate::Error) -> crate::Result<T> {
761        self.data = None;
762        Err(e)
763    }
764
765    fn end_of_input<T>(&mut self) -> crate::Result<T> {
766        self.make_error(Error::EndOfInput)
767    }
768}
769
770impl<'d> ParseInput for ReflessInput<'d> {
771    type Data = &'d [u8];
772
773    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
774    where
775        Self: 'a,
776    {
777        match self.data()?.split_first_chunk() {
778            Some((chunk, data)) => {
779                self.data = Some(data);
780                Ok(chunk)
781            }
782            None => self.end_of_input(),
783        }
784    }
785
786    fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data> {
787        match self.data()?.split_at_checked(n) {
788            Some((chunk, data)) => {
789                self.data = Some(data);
790                Ok(chunk)
791            }
792            None => self.end_of_input(),
793        }
794    }
795
796    fn parse_until_zero(&mut self) -> crate::Result<Self::Data> {
797        let data = self.data()?;
798        match data.iter().enumerate().find(|(_, x)| **x == 0) {
799            Some((at, _)) => {
800                let (chunk, data) = data.split_at(at);
801                self.data = Some(&data[1..]);
802                Ok(chunk)
803            }
804            None => self.end_of_input(),
805        }
806    }
807
808    fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T> {
809        let input = Self { data: Some(data) };
810        T::parse(input)
811    }
812
813    fn parse_all(self) -> crate::Result<Self::Data> {
814        self.data()
815    }
816
817    fn empty(self) -> crate::Result<()> {
818        if self.data()?.is_empty() {
819            Ok(())
820        } else {
821            Err(Error::ExtraInputLeft)
822        }
823    }
824
825    fn non_empty(self) -> crate::Result<Option<Self>> {
826        Ok(if self.data()?.is_empty() {
827            None
828        } else {
829            Some(self)
830        })
831    }
832}
833
834impl<'d, Extra> ParseInput for Input<'d, Extra> {
835    type Data = &'d [u8];
836
837    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
838    where
839        Self: 'a,
840    {
841        (**self).parse_chunk()
842    }
843
844    fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data> {
845        (**self).parse_n(n)
846    }
847
848    fn parse_until_zero(&mut self) -> crate::Result<Self::Data> {
849        (**self).parse_until_zero()
850    }
851
852    fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T> {
853        let input = Self {
854            refless: ReflessInput { data: Some(data) },
855            resolve: self.resolve,
856            index: self.index,
857            extra: self.extra,
858        };
859        T::parse(input)
860    }
861
862    fn parse_all(self) -> crate::Result<Self::Data> {
863        self.refless.parse_all()
864    }
865
866    fn empty(self) -> crate::Result<()> {
867        self.refless.empty()
868    }
869
870    fn non_empty(mut self) -> crate::Result<Option<Self>> {
871        self.refless = match self.refless.non_empty()? {
872            Some(refless) => refless,
873            None => return Ok(None),
874        };
875        Ok(Some(self))
876    }
877}
878
879impl<'d, Extra: 'static + Clone> PointInput for Input<'d, Extra> {
880    type Extra = Extra;
881    type WithExtra<E: 'static + Clone> = Input<'d, E>;
882
883    fn next_index(&mut self) -> usize {
884        let index = self.index.get();
885        self.index.set(index + 1);
886        index
887    }
888
889    fn resolve_arc_ref(&self) -> &Arc<dyn Resolve> {
890        self.resolve
891    }
892
893    fn extra(&self) -> &Self::Extra {
894        self.extra
895    }
896
897    fn map_extra<E: 'static + Clone>(
898        self,
899        f: impl FnOnce(&Self::Extra) -> &E,
900    ) -> Self::WithExtra<E> {
901        let Self {
902            refless,
903            resolve,
904            index,
905            extra,
906        } = self;
907        Input {
908            refless,
909            resolve,
910            index,
911            extra: f(extra),
912        }
913    }
914}
915
916pub trait ToOutput {
917    fn to_output(&self, output: &mut dyn Output);
918
919    fn data_hash(&self) -> Hash {
920        let mut output = HashOutput::default();
921        self.to_output(&mut output);
922        output.hash()
923    }
924
925    fn output<T: Output + Default>(&self) -> T {
926        let mut output = T::default();
927        self.to_output(&mut output);
928        output
929    }
930
931    fn vec(&self) -> Vec<u8> {
932        self.output()
933    }
934}
935
936/// Marker trait indicating that [`ToOutput`] result cannot be extended.
937pub trait InlineOutput: ToOutput {
938    fn slice_to_output(slice: &[Self], output: &mut dyn Output)
939    where
940        Self: Sized,
941    {
942        slice.iter_to_output(output);
943    }
944}
945
946pub trait ListHashes {
947    fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
948        let _ = f;
949    }
950
951    fn topology_hash(&self) -> Hash {
952        let mut hasher = Sha256::new();
953        self.list_hashes(&mut |hash| hasher.update(hash));
954        Hash::from_sha256(hasher.finalize().into())
955    }
956
957    fn point_count(&self) -> usize {
958        let mut count = 0;
959        self.list_hashes(&mut |_| count += 1);
960        count
961    }
962}
963
964pub trait Topological: ListHashes {
965    fn traverse(&self, visitor: &mut impl PointVisitor) {
966        let _ = visitor;
967    }
968
969    fn topology(&self) -> TopoVec {
970        let mut topology = TopoVec::with_capacity(self.point_count());
971        self.traverse(&mut topology);
972        topology
973    }
974}
975
976pub trait Tagged {
977    const TAGS: Tags = Tags(&[], &[]);
978
979    const HASH: Hash =
980        const { Hash::from_sha256(Self::TAGS.const_hash(sha2_const::Sha256::new()).finalize()) };
981}
982
983pub trait ParseSlice: for<'a> Parse<Input<'a>> {
984    fn parse_slice(data: &[u8], resolve: &Arc<dyn Resolve>) -> crate::Result<Self> {
985        Self::parse_slice_extra(data, resolve, &())
986    }
987
988    fn reparse(&self) -> crate::Result<Self>
989    where
990        Self: Traversible,
991    {
992        self.reparse_extra(&())
993    }
994}
995
996impl<T: for<'a> Parse<Input<'a>>> ParseSlice for T {}
997
998pub trait ParseSliceExtra<Extra>: for<'a> Parse<Input<'a, Extra>> {
999    fn parse_slice_extra(
1000        data: &[u8],
1001        resolve: &Arc<dyn Resolve>,
1002        extra: &Extra,
1003    ) -> crate::Result<Self> {
1004        let input = Input {
1005            refless: ReflessInput { data: Some(data) },
1006            resolve,
1007            index: &Cell::new(0),
1008            extra,
1009        };
1010        let object = Self::parse(input)?;
1011        Ok(object)
1012    }
1013
1014    fn reparse_extra(&self, extra: &Extra) -> crate::Result<Self>
1015    where
1016        Self: Traversible,
1017    {
1018        Self::parse_slice_extra(&self.vec(), &self.to_resolve(), extra)
1019    }
1020}
1021
1022impl<T: for<'a> Parse<Input<'a, Extra>>, Extra> ParseSliceExtra<Extra> for T {}
1023
1024#[derive(ToOutput)]
1025pub struct ObjectHashes {
1026    pub tags: Hash,
1027    pub topology: Hash,
1028    pub data: Hash,
1029}
1030
1031pub trait FullHash: ToOutput + ListHashes + Tagged {
1032    fn hashes(&self) -> ObjectHashes {
1033        ObjectHashes {
1034            tags: Self::HASH,
1035            topology: self.topology_hash(),
1036            data: self.data_hash(),
1037        }
1038    }
1039
1040    fn full_hash(&self) -> Hash {
1041        self.hashes().data_hash()
1042    }
1043}
1044
1045impl<T: ?Sized + ToOutput + ListHashes + Tagged> FullHash for T {}
1046
1047pub trait Traversible: 'static + Sized + Send + Sync + FullHash + Topological {
1048    fn to_resolve(&self) -> Arc<dyn Resolve> {
1049        Arc::new(ByTopology {
1050            topology: self.topology(),
1051        })
1052    }
1053
1054    fn point(self) -> Point<Self>
1055    where
1056        Self: Clone,
1057    {
1058        Point::from_object(self)
1059    }
1060}
1061
1062impl<T: 'static + Send + Sync + FullHash + Topological> Traversible for T {}
1063
1064pub trait Object<Extra = ()>: Traversible + for<'a> Parse<Input<'a, Extra>> {}
1065
1066impl<T: Traversible + for<'a> Parse<Input<'a, Extra>>, Extra> Object<Extra> for T {}
1067
1068pub struct Tags(pub &'static [&'static str], pub &'static [&'static Self]);
1069
1070impl Tags {
1071    const fn const_hash(&self, mut hasher: sha2_const::Sha256) -> sha2_const::Sha256 {
1072        {
1073            let mut i = 0;
1074            while i < self.0.len() {
1075                hasher = hasher.update(self.0[i].as_bytes());
1076                i += 1;
1077            }
1078        }
1079        {
1080            let mut i = 0;
1081            while i < self.1.len() {
1082                hasher = self.1[i].const_hash(hasher);
1083                i += 1;
1084            }
1085        }
1086        hasher
1087    }
1088}
1089
1090pub trait Inline<Extra = ()>:
1091    Object<Extra> + InlineOutput + for<'a> ParseInline<Input<'a, Extra>>
1092{
1093}
1094
1095impl<T: Object<Extra> + InlineOutput + for<'a> ParseInline<Input<'a, Extra>>, Extra> Inline<Extra>
1096    for T
1097{
1098}
1099
1100impl<T> ListHashes for Point<T> {
1101    fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
1102        f(self.hash());
1103    }
1104
1105    fn point_count(&self) -> usize {
1106        1
1107    }
1108}
1109
1110impl<T: Traversible> Topological for Point<T> {
1111    fn traverse(&self, visitor: &mut impl PointVisitor) {
1112        visitor.visit(self);
1113    }
1114}
1115
1116impl<T: 'static + FullHash, I: PointInput<Extra: Send + Sync + ExtraFor<T>>> ParseInline<I>
1117    for Point<T>
1118{
1119    fn parse_inline(input: &mut I) -> crate::Result<Self> {
1120        Ok(Self::from_address_extra(
1121            input.parse_inline()?,
1122            input.resolve(),
1123            input.extra().clone(),
1124        ))
1125    }
1126}
1127
1128impl<T: Tagged> Tagged for Point<T> {
1129    const TAGS: Tags = T::TAGS;
1130}
1131
1132impl<T> ToOutput for Point<T> {
1133    fn to_output(&self, output: &mut dyn Output) {
1134        self.hash().to_output(output);
1135    }
1136}
1137
1138impl<T> InlineOutput for Point<T> {}
1139
1140pub trait Topology: Send + Sync {
1141    fn len(&self) -> usize;
1142    fn get(&self, index: usize) -> Option<&Arc<dyn Singular>>;
1143
1144    fn is_empty(&self) -> bool {
1145        self.len() == 0
1146    }
1147}
1148
1149pub trait Singular: Send + Sync + FetchBytes {
1150    fn hash(&self) -> Hash;
1151}
1152
1153pub trait SingularFetch: Singular + Fetch {}
1154
1155impl<T: ?Sized + Singular + Fetch> SingularFetch for T {}
1156
1157pub type TopoVec = Vec<Arc<dyn Singular>>;
1158
1159impl PointVisitor for TopoVec {
1160    fn visit<T: Traversible>(&mut self, point: &(impl 'static + SingularFetch<T = T> + Clone)) {
1161        self.push(Arc::new(point.clone()));
1162    }
1163}
1164
1165impl<T> FetchBytes for Point<T> {
1166    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
1167        self.fetch.fetch_bytes()
1168    }
1169
1170    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
1171        self.fetch.fetch_data()
1172    }
1173
1174    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
1175        self.fetch.fetch_bytes_local()
1176    }
1177
1178    fn fetch_data_local(&self) -> Option<Vec<u8>> {
1179        self.fetch.fetch_data_local()
1180    }
1181
1182    fn as_inner(&self) -> Option<&dyn Any> {
1183        self.fetch.as_inner()
1184    }
1185}
1186
1187impl<T> Singular for Point<T> {
1188    fn hash(&self) -> Hash {
1189        self.hash.unwrap()
1190    }
1191}
1192
1193impl Topology for TopoVec {
1194    fn len(&self) -> usize {
1195        self.len()
1196    }
1197
1198    fn get(&self, index: usize) -> Option<&Arc<dyn Singular>> {
1199        (**self).get(index)
1200    }
1201}
1202
1203pub trait ParseSliceRefless: for<'a> Parse<ReflessInput<'a>> {
1204    fn parse_slice_refless(data: &[u8]) -> crate::Result<Self> {
1205        let input = ReflessInput { data: Some(data) };
1206        let object = Self::parse(input)?;
1207        Ok(object)
1208    }
1209}
1210
1211impl<T: for<'a> Parse<ReflessInput<'a>>> ParseSliceRefless for T {}
1212
1213pub trait ReflessObject:
1214    'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>
1215{
1216}
1217
1218impl<T: 'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>>
1219    ReflessObject for T
1220{
1221}
1222
1223pub trait ReflessInline:
1224    ReflessObject + InlineOutput + for<'a> ParseInline<ReflessInput<'a>>
1225{
1226}
1227
1228impl<T: ReflessObject + InlineOutput + for<'a> ParseInline<ReflessInput<'a>>> ReflessInline for T {}
1229
1230pub trait Output {
1231    fn write(&mut self, data: &[u8]);
1232}
1233
1234impl Output for Vec<u8> {
1235    fn write(&mut self, data: &[u8]) {
1236        self.extend_from_slice(data);
1237    }
1238}
1239
1240#[derive(Default)]
1241struct HashOutput {
1242    hasher: Sha256,
1243    at: usize,
1244}
1245
1246impl Output for HashOutput {
1247    fn write(&mut self, data: &[u8]) {
1248        self.hasher.update(data);
1249        self.at += data.len();
1250    }
1251}
1252
1253impl HashOutput {
1254    fn hash(self) -> Hash {
1255        Hash::from_sha256(self.hasher.finalize().into())
1256    }
1257}
1258
1259pub struct PointMut<'a, T: FullHash> {
1260    hash: &'a mut OptionalHash,
1261    fetch: &'a mut dyn Fetch<T = T>,
1262}
1263
1264impl<T: FullHash> Deref for PointMut<'_, T> {
1265    type Target = T;
1266
1267    fn deref(&self) -> &Self::Target {
1268        self.fetch.get().unwrap()
1269    }
1270}
1271
1272impl<T: FullHash> DerefMut for PointMut<'_, T> {
1273    fn deref_mut(&mut self) -> &mut Self::Target {
1274        self.fetch.get_mut().unwrap()
1275    }
1276}
1277
1278impl<T: FullHash> Drop for PointMut<'_, T> {
1279    fn drop(&mut self) {
1280        self.finalize();
1281    }
1282}
1283
1284impl<'a, T: FullHash> PointMut<'a, T> {
1285    fn finalize(&mut self) {
1286        self.fetch.get_mut_finalize();
1287        *self.hash = self.full_hash().into();
1288    }
1289}
1290
1291impl<T> Point<T> {
1292    pub fn get(&self) -> Option<&T> {
1293        self.fetch.get()
1294    }
1295
1296    pub fn try_fetch_local(&self) -> Result<Option<Node<T>>> {
1297        self.fetch.try_fetch_local()
1298    }
1299}
1300
1301impl<T: Traversible + Clone> Point<T> {
1302    pub fn from_object(object: T) -> Self {
1303        Self::from_fetch(object.full_hash(), Arc::new(LocalFetch { object }))
1304    }
1305
1306    fn yolo_mut(&mut self) -> bool {
1307        self.fetch.get().is_some()
1308            && Arc::get_mut(&mut self.fetch).is_some_and(|fetch| fetch.get_mut().is_some())
1309    }
1310
1311    async fn prepare_yolo_fetch(&mut self) -> crate::Result<()> {
1312        if !self.yolo_mut() {
1313            let object = self.fetch.fetch().await?;
1314            self.fetch = Arc::new(LocalFetch { object });
1315        }
1316        Ok(())
1317    }
1318
1319    pub async fn fetch_mut(&'_ mut self) -> crate::Result<PointMut<'_, T>> {
1320        self.prepare_yolo_fetch().await?;
1321        let fetch = Arc::get_mut(&mut self.fetch).unwrap();
1322        assert!(fetch.get_mut().is_some());
1323        self.hash.clear();
1324        Ok(PointMut {
1325            hash: &mut self.hash,
1326            fetch,
1327        })
1328    }
1329
1330    pub async fn fetch_ref(&mut self) -> crate::Result<&T> {
1331        self.prepare_yolo_fetch().await?;
1332        Ok(self.fetch.get().unwrap())
1333    }
1334}
1335
1336struct LocalFetch<T> {
1337    object: T,
1338}
1339
1340impl<T> Deref for LocalFetch<T> {
1341    type Target = T;
1342
1343    fn deref(&self) -> &Self::Target {
1344        &self.object
1345    }
1346}
1347
1348impl<T> DerefMut for LocalFetch<T> {
1349    fn deref_mut(&mut self) -> &mut Self::Target {
1350        &mut self.object
1351    }
1352}
1353
1354impl<T: Traversible + Clone> Fetch for LocalFetch<T> {
1355    type T = T;
1356
1357    fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
1358        Box::pin(ready(Ok((self.object.clone(), self.object.to_resolve()))))
1359    }
1360
1361    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1362        Box::pin(ready(Ok(self.object.clone())))
1363    }
1364
1365    fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
1366        Ok(Some((self.object.clone(), self.object.to_resolve())))
1367    }
1368
1369    fn fetch_local(&self) -> Option<Self::T> {
1370        Some(self.object.clone())
1371    }
1372
1373    fn get(&self) -> Option<&Self::T> {
1374        Some(self)
1375    }
1376
1377    fn get_mut(&mut self) -> Option<&mut Self::T> {
1378        Some(self)
1379    }
1380}
1381
1382impl<T: Traversible + Clone> FetchBytes for LocalFetch<T> {
1383    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
1384        Box::pin(ready(Ok((self.object.output(), self.object.to_resolve()))))
1385    }
1386
1387    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
1388        Box::pin(ready(Ok(self.object.output())))
1389    }
1390
1391    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
1392        Ok(Some((self.object.output(), self.object.to_resolve())))
1393    }
1394
1395    fn fetch_data_local(&self) -> Option<Vec<u8>> {
1396        Some(self.object.output())
1397    }
1398}
1399
1400impl<T: Traversible + Clone> Singular for LocalFetch<T> {
1401    fn hash(&self) -> Hash {
1402        self.object.full_hash()
1403    }
1404}
1405
1406struct ByTopology {
1407    topology: TopoVec,
1408}
1409
1410impl ByTopology {
1411    fn try_resolve(&'_ self, address: Address) -> Result<FailFuture<'_, ByteNode>> {
1412        let point = self
1413            .topology
1414            .get(address.index)
1415            .ok_or(Error::AddressOutOfBounds)?;
1416        if point.hash() != address.hash {
1417            Err(Error::ResolutionMismatch)
1418        } else {
1419            Ok(point.fetch_bytes())
1420        }
1421    }
1422
1423    fn try_resolve_data(&'_ self, address: Address) -> Result<FailFuture<'_, Vec<u8>>> {
1424        let point = self
1425            .topology
1426            .get(address.index)
1427            .ok_or(Error::AddressOutOfBounds)?;
1428        if point.hash() != address.hash {
1429            Err(Error::ResolutionMismatch)
1430        } else {
1431            Ok(point.fetch_data())
1432        }
1433    }
1434}
1435
1436impl Resolve for ByTopology {
1437    fn resolve(&'_ self, address: Address) -> FailFuture<'_, ByteNode> {
1438        self.try_resolve(address)
1439            .map_err(Err)
1440            .map_err(ready)
1441            .map_err(Box::pin)
1442            .unwrap_or_else(|x| x)
1443    }
1444
1445    fn resolve_data(&'_ self, address: Address) -> FailFuture<'_, Vec<u8>> {
1446        self.try_resolve_data(address)
1447            .map_err(Err)
1448            .map_err(ready)
1449            .map_err(Box::pin)
1450            .unwrap_or_else(|x| x)
1451    }
1452
1453    fn try_resolve_local(&self, address: Address) -> Result<Option<ByteNode>> {
1454        let point = self
1455            .topology
1456            .get(address.index)
1457            .ok_or(Error::AddressOutOfBounds)?;
1458        if point.hash() != address.hash {
1459            Err(Error::ResolutionMismatch)
1460        } else {
1461            point.fetch_bytes_local()
1462        }
1463    }
1464}
1465
1466impl<T: FullHash> Fetch for Point<T> {
1467    type T = T;
1468
1469    fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
1470        self.fetch.fetch_full()
1471    }
1472
1473    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1474        self.fetch.fetch()
1475    }
1476
1477    fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
1478        self.fetch.try_fetch_local()
1479    }
1480
1481    fn fetch_local(&self) -> Option<Self::T> {
1482        self.fetch.fetch_local()
1483    }
1484
1485    fn get(&self) -> Option<&Self::T> {
1486        self.fetch.get()
1487    }
1488
1489    fn get_mut(&mut self) -> Option<&mut Self::T> {
1490        self.hash.clear();
1491        Arc::get_mut(&mut self.fetch)?.get_mut()
1492    }
1493
1494    fn get_mut_finalize(&mut self) {
1495        let fetch = Arc::get_mut(&mut self.fetch).unwrap();
1496        fetch.get_mut_finalize();
1497        self.hash = fetch.get().unwrap().full_hash().into();
1498    }
1499}
1500
1501pub trait Size {
1502    const SIZE: usize = <Self::Size as Unsigned>::USIZE;
1503    type Size: Unsigned;
1504}
1505
1506pub trait SizeExt: Size<Size: ArrayLength> + ToOutput {
1507    fn to_array(&self) -> GenericArray<u8, Self::Size> {
1508        let mut array = GenericArray::default();
1509        let mut output = ArrayOutput {
1510            data: &mut array,
1511            offset: 0,
1512        };
1513        self.to_output(&mut output);
1514        output.finalize();
1515        array
1516    }
1517}
1518
1519impl<T: Size<Size: ArrayLength> + ToOutput> SizeExt for T {}
1520
1521struct ArrayOutput<'a> {
1522    data: &'a mut [u8],
1523    offset: usize,
1524}
1525
1526impl ArrayOutput<'_> {
1527    fn finalize(self) {
1528        assert_eq!(self.offset, self.data.len());
1529    }
1530}
1531
1532impl Output for ArrayOutput<'_> {
1533    fn write(&mut self, data: &[u8]) {
1534        self.data[self.offset..][..data.len()].copy_from_slice(data);
1535        self.offset += data.len();
1536    }
1537}
1538
1539trait RainbowIterator: Sized + IntoIterator {
1540    fn iter_to_output(self, output: &mut dyn Output)
1541    where
1542        Self::Item: InlineOutput,
1543    {
1544        self.into_iter().for_each(|item| item.to_output(output));
1545    }
1546
1547    fn iter_list_hashes(self, f: &mut impl FnMut(Hash))
1548    where
1549        Self::Item: ListHashes,
1550    {
1551        self.into_iter().for_each(|item| item.list_hashes(f));
1552    }
1553
1554    fn iter_traverse(self, visitor: &mut impl PointVisitor)
1555    where
1556        Self::Item: Topological,
1557    {
1558        self.into_iter().for_each(|item| item.traverse(visitor));
1559    }
1560}
1561
1562pub trait ParseInput: Sized {
1563    type Data: AsRef<[u8]> + Deref<Target = [u8]> + Into<Vec<u8>> + Copy;
1564    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
1565    where
1566        Self: 'a;
1567    fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data>;
1568    fn parse_until_zero(&mut self) -> crate::Result<Self::Data>;
1569    fn parse_n_compare(&mut self, n: usize, c: &[u8]) -> crate::Result<Option<Self::Data>> {
1570        let data = self.parse_n(n)?;
1571        if *data == *c {
1572            Ok(None)
1573        } else {
1574            Ok(Some(data))
1575        }
1576    }
1577    fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T>;
1578    fn parse_ahead<T: Parse<Self>>(&mut self, n: usize) -> crate::Result<T> {
1579        let data = self.parse_n(n)?;
1580        self.reparse(data)
1581    }
1582    fn parse_zero_terminated<T: Parse<Self>>(&mut self) -> crate::Result<T> {
1583        let data = self.parse_until_zero()?;
1584        self.reparse(data)
1585    }
1586    fn parse_compare<T: Parse<Self>>(&mut self, n: usize, c: &[u8]) -> Result<Option<T>> {
1587        self.parse_n_compare(n, c)?
1588            .map(|data| self.reparse(data))
1589            .transpose()
1590    }
1591    fn parse_all(self) -> crate::Result<Self::Data>;
1592    fn empty(self) -> crate::Result<()>;
1593    fn non_empty(self) -> crate::Result<Option<Self>>;
1594
1595    fn consume(self, f: impl FnMut(&mut Self) -> crate::Result<()>) -> crate::Result<()> {
1596        self.collect(f)
1597    }
1598
1599    fn parse_collect<T: ParseInline<Self>, B: FromIterator<T>>(self) -> crate::Result<B> {
1600        self.collect(|input| input.parse_inline())
1601    }
1602
1603    fn collect<T, B: FromIterator<T>>(
1604        self,
1605        f: impl FnMut(&mut Self) -> crate::Result<T>,
1606    ) -> crate::Result<B> {
1607        self.iter(f).collect()
1608    }
1609
1610    fn iter<T>(
1611        self,
1612        mut f: impl FnMut(&mut Self) -> crate::Result<T>,
1613    ) -> impl Iterator<Item = crate::Result<T>> {
1614        let mut state = Some(self);
1615        std::iter::from_fn(move || {
1616            let mut input = match state.take()?.non_empty() {
1617                Ok(input) => input?,
1618                Err(e) => return Some(Err(e)),
1619            };
1620            let item = f(&mut input);
1621            state = Some(input);
1622            Some(item)
1623        })
1624    }
1625
1626    fn parse_inline<T: ParseInline<Self>>(&mut self) -> crate::Result<T> {
1627        T::parse_inline(self)
1628    }
1629
1630    fn parse<T: Parse<Self>>(self) -> crate::Result<T> {
1631        T::parse(self)
1632    }
1633
1634    fn parse_vec<T: ParseInline<Self>>(self) -> crate::Result<Vec<T>> {
1635        T::parse_vec(self)
1636    }
1637}
1638
1639pub trait PointInput: ParseInput {
1640    type Extra: 'static + Clone;
1641    type WithExtra<E: 'static + Clone>: PointInput<Extra = E, WithExtra<Self::Extra> = Self>;
1642    fn next_index(&mut self) -> usize;
1643    fn resolve_arc_ref(&self) -> &Arc<dyn Resolve>;
1644    fn resolve(&self) -> Arc<dyn Resolve> {
1645        self.resolve_arc_ref().clone()
1646    }
1647    fn resolve_ref(&self) -> &dyn Resolve {
1648        self.resolve_arc_ref().as_ref()
1649    }
1650    fn extension<T: Any>(&self) -> crate::Result<&T> {
1651        self.resolve_ref()
1652            .extension(TypeId::of::<T>())?
1653            .downcast_ref()
1654            .ok_or(Error::ExtensionType)
1655    }
1656    fn extra(&self) -> &Self::Extra;
1657    fn map_extra<E: 'static + Clone>(
1658        self,
1659        f: impl FnOnce(&Self::Extra) -> &E,
1660    ) -> Self::WithExtra<E>;
1661}
1662
1663impl<T: Sized + IntoIterator> RainbowIterator for T {}
1664
1665pub trait Parse<I: ParseInput>: Sized {
1666    fn parse(input: I) -> crate::Result<Self>;
1667}
1668
1669pub trait ParseInline<I: ParseInput>: Parse<I> {
1670    fn parse_inline(input: &mut I) -> crate::Result<Self>;
1671    fn parse_as_inline(mut input: I) -> crate::Result<Self> {
1672        let object = Self::parse_inline(&mut input)?;
1673        input.empty()?;
1674        Ok(object)
1675    }
1676    fn parse_vec(input: I) -> crate::Result<Vec<Self>> {
1677        input.parse_collect()
1678    }
1679}
1680
1681/// Implemented if both types have the exact same layout.
1682/// This implies having the same [`MaybeHasNiche::MnArray`].
1683///
1684/// This is represented as two-way conversion for two reasons:
1685/// - to highlight that the conversion is actual equivalence
1686/// - to increase flexibility (mostly to go around the orphan rule)
1687pub trait Equivalent<T>: Sized {
1688    /// Inverse of [`Equivalent::from_equivalent`].
1689    fn into_equivalent(self) -> T;
1690    /// Inverse of [`Equivalent::into_equivalent`].
1691    fn from_equivalent(object: T) -> Self;
1692}
1693
1694/// This implementation is the main goal of [`Equivalent`]: we assume transmuting the pointer is
1695/// safe.
1696impl<U: 'static + Equivalent<T>, T: 'static> Equivalent<Point<T>> for Point<U> {
1697    fn into_equivalent(self) -> Point<T> {
1698        self.map_fetch(|fetch| {
1699            Arc::new(MapEquivalent {
1700                fetch,
1701                map: U::into_equivalent,
1702            })
1703        })
1704    }
1705
1706    fn from_equivalent(point: Point<T>) -> Self {
1707        point.map_fetch(|fetch| {
1708            Arc::new(MapEquivalent {
1709                fetch,
1710                map: U::from_equivalent,
1711            })
1712        })
1713    }
1714}
1715
1716struct MapEquivalent<T, F> {
1717    fetch: Arc<dyn Fetch<T = T>>,
1718    map: F,
1719}
1720
1721impl<T, F> FetchBytes for MapEquivalent<T, F> {
1722    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
1723        self.fetch.fetch_bytes()
1724    }
1725
1726    fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
1727        self.fetch.fetch_data()
1728    }
1729
1730    fn fetch_bytes_local(&self) -> Result<Option<ByteNode>> {
1731        self.fetch.fetch_bytes_local()
1732    }
1733
1734    fn fetch_data_local(&self) -> Option<Vec<u8>> {
1735        self.fetch.fetch_data_local()
1736    }
1737}
1738
1739trait Map1<T>: Fn(T) -> Self::U {
1740    type U;
1741}
1742
1743impl<T, U, F: Fn(T) -> U> Map1<T> for F {
1744    type U = U;
1745}
1746
1747impl<T, F: Send + Sync + Map1<T>> Fetch for MapEquivalent<T, F> {
1748    type T = F::U;
1749
1750    fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
1751        Box::pin(self.fetch.fetch_full().map_ok(|(x, r)| ((self.map)(x), r)))
1752    }
1753
1754    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1755        Box::pin(self.fetch.fetch().map_ok(&self.map))
1756    }
1757
1758    fn try_fetch_local(&self) -> Result<Option<Node<Self::T>>> {
1759        let Some((object, resolve)) = self.fetch.try_fetch_local()? else {
1760            return Ok(None);
1761        };
1762        let object = (self.map)(object);
1763        Ok(Some((object, resolve)))
1764    }
1765
1766    fn fetch_local(&self) -> Option<Self::T> {
1767        self.fetch.fetch_local().map(&self.map)
1768    }
1769}
1770
1771impl<T> MaybeHasNiche for Point<T> {
1772    type MnArray = <Hash as MaybeHasNiche>::MnArray;
1773}
1774
1775impl<T: FullHash + Default> Point<T> {
1776    pub fn is_default(&self) -> bool {
1777        self.hash() == T::default().full_hash()
1778    }
1779}
1780
1781impl<T: Default + Traversible + Clone> Default for Point<T> {
1782    fn default() -> Self {
1783        T::default().point()
1784    }
1785}
1786
1787pub trait ExtraFor<T> {
1788    fn parse(&self, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T>;
1789}
1790
1791impl<T: for<'a> Parse<Input<'a, Extra>>, Extra> ExtraFor<T> for Extra {
1792    fn parse(&self, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T> {
1793        T::parse_slice_extra(data, resolve, self)
1794    }
1795}
1796
1797#[test]
1798fn options() {
1799    type T0 = ();
1800    type T1 = Option<T0>;
1801    type T2 = Option<T1>;
1802    type T3 = Option<T2>;
1803    type T4 = Option<T3>;
1804    type T5 = Option<T4>;
1805    assert_eq!(T0::SIZE, 0);
1806    assert_eq!(T1::SIZE, 1);
1807    assert_eq!(T2::SIZE, 1);
1808    assert_eq!(T3::SIZE, 1);
1809    assert_eq!(T4::SIZE, 1);
1810    assert_eq!(T5::SIZE, 1);
1811    assert_eq!(Some(Some(Some(()))).vec(), [0]);
1812    assert_eq!(Some(Some(None::<()>)).vec(), [1]);
1813    assert_eq!(Some(None::<Option<()>>).vec(), [2]);
1814    assert_eq!(None::<Option<Option<()>>>.vec(), [3]);
1815
1816    assert_eq!(false.vec(), [0]);
1817    assert_eq!(true.vec(), [1]);
1818    assert_eq!(Some(false).vec(), [0]);
1819    assert_eq!(Some(true).vec(), [1]);
1820    assert_eq!(None::<bool>.vec(), [2]);
1821    assert_eq!(Some(Some(false)).vec(), [0]);
1822    assert_eq!(Some(Some(true)).vec(), [1]);
1823    assert_eq!(Some(None::<bool>).vec(), [2]);
1824    assert_eq!(None::<Option<bool>>.vec(), [3]);
1825    assert_eq!(Some(Some(Some(false))).vec(), [0]);
1826    assert_eq!(Some(Some(Some(true))).vec(), [1]);
1827    assert_eq!(Some(Some(None::<bool>)).vec(), [2]);
1828    assert_eq!(Some(None::<Option<bool>>).vec(), [3]);
1829    assert_eq!(None::<Option<Option<bool>>>.vec(), [4]);
1830    assert_eq!(Option::<Point<()>>::SIZE, HASH_SIZE);
1831    assert_eq!(Some(()).vec(), [0]);
1832    assert_eq!(Some(((), ())).vec(), [0]);
1833    assert_eq!(Some(((), true)).vec(), [1]);
1834    assert_eq!(Some((true, true)).vec(), [1, 1]);
1835    assert_eq!(Some((Some(true), true)).vec(), [1, 1]);
1836    assert_eq!(Some((None::<bool>, true)).vec(), [2, 1]);
1837    assert_eq!(Some((true, None::<bool>)).vec(), [1, 2]);
1838    assert_eq!(None::<(Option<bool>, bool)>.vec(), [3, 2]);
1839    assert_eq!(None::<(bool, Option<bool>)>.vec(), [2, 3]);
1840    assert_eq!(Some(Some((Some(true), Some(true)))).vec(), [1, 1],);
1841    assert_eq!(Option::<Point<()>>::SIZE, HASH_SIZE);
1842    assert_eq!(Option::<Option<Point<()>>>::SIZE, HASH_SIZE);
1843    assert_eq!(Option::<Option<Option<Point<()>>>>::SIZE, HASH_SIZE);
1844}