object_rainbow/
lib.rs

1extern crate self as object_rainbow;
2
3use std::{
4    any::{Any, TypeId},
5    cell::Cell,
6    convert::Infallible,
7    future::ready,
8    marker::PhantomData,
9    ops::{Deref, DerefMut},
10    pin::Pin,
11    sync::Arc,
12};
13
14pub use anyhow::anyhow;
15use futures_util::TryFutureExt;
16use generic_array::{ArrayLength, GenericArray};
17pub use object_rainbow_derive::{
18    Enum, Inline, MaybeHasNiche, Object, Parse, ParseAsInline, ParseInline, ReflessInline,
19    ReflessObject, Size, Tagged, ToOutput, Topological,
20};
21use sha2::{Digest, Sha256};
22#[doc(hidden)]
23pub use typenum;
24use typenum::Unsigned;
25
26pub use self::enumkind::Enum;
27pub use self::niche::{
28    AutoEnumNiche, HackNiche, MaybeHasNiche, Niche, NoNiche, SomeNiche, ZeroNiche, ZeroNoNiche,
29};
30#[doc(hidden)]
31pub use self::niche::{MaybeNiche, MnArray, NicheFoldOrArray, NicheOr};
32
33pub mod enumkind;
34mod impls;
35pub mod length_prefixed;
36mod niche;
37pub mod numeric;
38mod sha2_const;
39
40#[macro_export]
41macro_rules! error_parse {
42    ($($t:tt)*) => {
43        $crate::Error::Parse($crate::anyhow!($($t)*))
44    };
45}
46
47#[macro_export]
48macro_rules! error_fetch {
49    ($($t:tt)*) => {
50        $crate::Error::Fetch($crate::anyhow!($($t)*))
51    };
52}
53
54#[derive(Debug, thiserror::Error)]
55pub enum Error {
56    #[error(transparent)]
57    Parse(anyhow::Error),
58    #[error(transparent)]
59    Fetch(anyhow::Error),
60    #[error("extra input left")]
61    ExtraInputLeft,
62    #[error("end of input")]
63    EndOfInput,
64    #[error("address index out of bounds")]
65    AddressOutOfBounds,
66    #[error("hash resolution mismatch")]
67    ResolutionMismatch,
68    #[error("data hash mismatch")]
69    DataMismatch,
70    #[error("discriminant overflow")]
71    DiscriminantOverflow,
72    #[error("zero")]
73    Zero,
74    #[error("out of bounds")]
75    OutOfBounds,
76    #[error("length out of bounds")]
77    LenOutOfBounds,
78    #[error(transparent)]
79    Utf8(std::string::FromUtf8Error),
80    #[error("unknown extension")]
81    UnknownExtension,
82    #[error("wrong extension type")]
83    ExtensionType,
84}
85
86pub type Result<T> = std::result::Result<T, Error>;
87
88pub const HASH_SIZE: usize = sha2_const::Sha256::DIGEST_SIZE;
89
90pub type Hash = [u8; HASH_SIZE];
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
93pub struct Address {
94    pub index: usize,
95    pub hash: Hash,
96}
97
98#[derive(Debug, Clone, Copy)]
99struct OptionalHash(Hash);
100
101impl From<Hash> for OptionalHash {
102    fn from(hash: Hash) -> Self {
103        Self(hash)
104    }
105}
106
107impl OptionalHash {
108    fn get(&self) -> Option<&Hash> {
109        (self.0 != Hash::default()).then_some(&self.0)
110    }
111
112    fn unwrap(&self) -> &Hash {
113        self.get().unwrap()
114    }
115
116    fn clear(&mut self) {
117        self.0 = Hash::default();
118    }
119}
120
121pub type FailFuture<'a, T> = Pin<Box<dyn 'a + Send + Future<Output = Result<T>>>>;
122
123pub type ByteNode = (Vec<u8>, Arc<dyn Resolve>);
124
125pub trait FromInner {
126    type Inner: 'static + Clone;
127
128    fn from_inner(inner: Self::Inner) -> Self;
129}
130
131pub trait AsAny {
132    fn any_ref(&self) -> &dyn Any
133    where
134        Self: 'static;
135    fn any_mut(&mut self) -> &mut dyn Any
136    where
137        Self: 'static;
138    fn any_box(self: Box<Self>) -> Box<dyn Any>
139    where
140        Self: 'static;
141    fn any_arc(self: Arc<Self>) -> Arc<dyn Any>
142    where
143        Self: 'static;
144    fn any_arc_sync(self: Arc<Self>) -> Arc<dyn Send + Sync + Any>
145    where
146        Self: 'static + Send + Sync;
147}
148
149impl<T> AsAny for T {
150    fn any_ref(&self) -> &dyn Any
151    where
152        Self: 'static,
153    {
154        self
155    }
156
157    fn any_mut(&mut self) -> &mut dyn Any
158    where
159        Self: 'static,
160    {
161        self
162    }
163
164    fn any_box(self: Box<Self>) -> Box<dyn Any>
165    where
166        Self: 'static,
167    {
168        self
169    }
170
171    fn any_arc(self: Arc<Self>) -> Arc<dyn Any>
172    where
173        Self: 'static,
174    {
175        self
176    }
177
178    fn any_arc_sync(self: Arc<Self>) -> Arc<dyn Send + Sync + Any>
179    where
180        Self: 'static + Send + Sync,
181    {
182        self
183    }
184}
185
186pub trait Resolve: Send + Sync + AsAny {
187    fn resolve(&'_ self, address: Address) -> FailFuture<'_, ByteNode>;
188    fn resolve_extension(&self, address: Address, typeid: TypeId) -> crate::Result<&dyn Any> {
189        let _ = address;
190        let _ = typeid;
191        Err(Error::UnknownExtension)
192    }
193    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
194        let _ = typeid;
195        Err(Error::UnknownExtension)
196    }
197    fn name(&self) -> &str;
198}
199
200pub trait FetchBytes: AsAny {
201    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode>;
202    fn as_inner(&self) -> Option<&dyn Any> {
203        None
204    }
205    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
206        let _ = typeid;
207        Err(Error::UnknownExtension)
208    }
209}
210
211pub trait Fetch: Send + Sync + FetchBytes {
212    type T;
213    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)>;
214    fn fetch(&'_ self) -> FailFuture<'_, Self::T>;
215    fn get(&self) -> Option<&Self::T> {
216        None
217    }
218    fn get_mut(&mut self) -> Option<&mut Self::T> {
219        None
220    }
221    fn get_mut_finalize(&mut self) {}
222}
223
224pub trait FetchBytesExt: FetchBytes {
225    fn inner_cast<T: FromInner>(&self) -> Option<T> {
226        self.as_inner()?.downcast_ref().cloned().map(T::from_inner)
227    }
228}
229
230impl<T: ?Sized + FetchBytes> FetchBytesExt for T {}
231
232#[derive(Clone, ParseAsInline)]
233pub struct RawPointInner {
234    hash: Hash,
235    origin: Arc<dyn Send + Sync + FetchBytes>,
236}
237
238impl RawPointInner {
239    pub fn cast<T>(self) -> RawPoint<T> {
240        RawPoint {
241            inner: self,
242            _object: PhantomData,
243        }
244    }
245
246    pub fn from_address(address: Address, resolve: Arc<dyn Resolve>) -> Self {
247        Self {
248            hash: address.hash,
249            origin: Arc::new(ByAddressInner { address, resolve }),
250        }
251    }
252}
253
254impl ToOutput for RawPointInner {
255    fn to_output(&self, output: &mut dyn Output) {
256        output.write(&self.hash);
257    }
258}
259
260impl ParseInline<Input<'_>> for RawPointInner {
261    fn parse_inline(input: &mut Input<'_>) -> crate::Result<Self> {
262        input.parse_raw_point_inner()
263    }
264}
265
266impl Singular for RawPointInner {
267    fn hash(&self) -> &Hash {
268        &self.hash
269    }
270}
271
272impl<T> Singular for RawPoint<T> {
273    fn hash(&self) -> &Hash {
274        self.inner.hash()
275    }
276}
277
278#[derive(ToOutput, ParseInline, ParseAsInline)]
279pub struct RawPoint<T = Infallible> {
280    inner: RawPointInner,
281    _object: PhantomData<fn() -> T>,
282}
283
284impl<T> FromInner for RawPoint<T> {
285    type Inner = RawPointInner;
286
287    fn from_inner(inner: Self::Inner) -> Self {
288        Self {
289            inner,
290            _object: PhantomData,
291        }
292    }
293}
294
295impl<T> Clone for RawPoint<T> {
296    fn clone(&self) -> Self {
297        Self {
298            inner: self.inner.clone(),
299            _object: PhantomData,
300        }
301    }
302}
303
304impl<T> Point<T> {
305    pub fn raw(self) -> RawPoint<T> {
306        {
307            if let Some(raw) = self.origin.inner_cast() {
308                return raw;
309            }
310        }
311        RawPoint {
312            inner: RawPointInner {
313                hash: *self.hash.unwrap(),
314                origin: self.origin,
315            },
316            _object: PhantomData,
317        }
318    }
319}
320
321impl<T> RawPoint<T> {
322    pub fn cast<U>(self) -> RawPoint<U> {
323        self.inner.cast()
324    }
325}
326
327impl<T: Object> RawPoint<T> {
328    pub fn point(self) -> Point<T> {
329        Point {
330            hash: self.inner.hash.into(),
331            origin: Arc::new(self),
332        }
333    }
334}
335
336impl FetchBytes for RawPointInner {
337    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
338        self.origin.fetch_bytes()
339    }
340
341    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
342        self.origin.extension(typeid)
343    }
344}
345
346impl<T> FetchBytes for RawPoint<T> {
347    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
348        self.inner.fetch_bytes()
349    }
350
351    fn as_inner(&self) -> Option<&dyn Any> {
352        Some(&self.inner)
353    }
354
355    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
356        self.inner.extension(typeid)
357    }
358}
359
360impl<T: Object> Fetch for RawPoint<T> {
361    type T = T;
362
363    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)> {
364        Box::pin(async {
365            let (data, resolve) = self.inner.origin.fetch_bytes().await?;
366            let object = T::parse_slice(&data, &resolve)?;
367            if self.inner.hash != object.full_hash() {
368                Err(Error::DataMismatch)
369            } else {
370                Ok((object, resolve))
371            }
372        })
373    }
374
375    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
376        Box::pin(async {
377            let (data, resolve) = self.inner.origin.fetch_bytes().await?;
378            let object = T::parse_slice(&data, &resolve)?;
379            if self.inner.hash != object.full_hash() {
380                Err(Error::DataMismatch)
381            } else {
382                Ok(object)
383            }
384        })
385    }
386}
387
388impl<T> Point<T> {
389    pub fn extract_resolve<R: Any>(&self) -> Option<(&Address, &R)> {
390        let ByAddressInner { address, resolve } =
391            self.origin.as_inner()?.downcast_ref::<ByAddressInner>()?;
392        let resolve = resolve.as_ref().any_ref().downcast_ref::<R>()?;
393        Some((address, resolve))
394    }
395}
396
397#[derive(ParseAsInline)]
398pub struct Point<T> {
399    hash: OptionalHash,
400    origin: Arc<dyn Fetch<T = T>>,
401}
402
403impl<T> PartialOrd for Point<T> {
404    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
405        Some(self.cmp(other))
406    }
407}
408
409impl<T> Ord for Point<T> {
410    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
411        self.hash().cmp(other.hash())
412    }
413}
414
415impl<T> Eq for Point<T> {}
416
417impl<T> PartialEq for Point<T> {
418    fn eq(&self, other: &Self) -> bool {
419        self.hash() == other.hash()
420    }
421}
422
423impl<T> Clone for Point<T> {
424    fn clone(&self) -> Self {
425        Self {
426            hash: self.hash,
427            origin: self.origin.clone(),
428        }
429    }
430}
431
432impl<T> Point<T> {
433    fn from_origin(hash: Hash, origin: Arc<dyn Fetch<T = T>>) -> Self {
434        Self {
435            hash: hash.into(),
436            origin,
437        }
438    }
439}
440
441impl<T> Size for Point<T> {
442    const SIZE: usize = HASH_SIZE;
443    type Size = typenum::generic_const_mappings::U<HASH_SIZE>;
444}
445
446impl<T: Object> Point<T> {
447    pub fn from_address(address: Address, resolve: Arc<dyn Resolve>) -> Self {
448        Self::from_origin(
449            address.hash,
450            Arc::new(ByAddress {
451                inner: ByAddressInner { address, resolve },
452                _object: PhantomData,
453            }),
454        )
455    }
456}
457
458#[derive(Clone)]
459struct ByAddressInner {
460    address: Address,
461    resolve: Arc<dyn Resolve>,
462}
463
464impl FetchBytes for ByAddressInner {
465    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
466        self.resolve.resolve(self.address)
467    }
468
469    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
470        self.resolve.resolve_extension(self.address, typeid)
471    }
472}
473
474struct ByAddress<T> {
475    inner: ByAddressInner,
476    _object: PhantomData<fn() -> T>,
477}
478
479impl<T> FromInner for ByAddress<T> {
480    type Inner = ByAddressInner;
481
482    fn from_inner(inner: Self::Inner) -> Self {
483        Self {
484            inner,
485            _object: PhantomData,
486        }
487    }
488}
489
490impl<T> FetchBytes for ByAddress<T> {
491    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
492        self.inner.fetch_bytes()
493    }
494
495    fn as_inner(&self) -> Option<&dyn Any> {
496        Some(&self.inner)
497    }
498
499    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
500        self.inner.extension(typeid)
501    }
502}
503
504impl<T: Object> Fetch for ByAddress<T> {
505    type T = T;
506
507    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)> {
508        Box::pin(async {
509            let (data, resolve) = self.fetch_bytes().await?;
510            let object = T::parse_slice(&data, &resolve)?;
511            if self.inner.address.hash != object.full_hash() {
512                Err(Error::DataMismatch)
513            } else {
514                Ok((object, resolve))
515            }
516        })
517    }
518
519    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
520        Box::pin(async {
521            let (data, resolve) = self.fetch_bytes().await?;
522            let object = T::parse_slice(&data, &resolve)?;
523            if self.inner.address.hash != object.full_hash() {
524                Err(Error::DataMismatch)
525            } else {
526                Ok(object)
527            }
528        })
529    }
530}
531
532pub trait PointVisitor {
533    fn visit<T: Object>(&mut self, point: &Point<T>);
534}
535
536struct HashVisitor<F>(F);
537
538impl<F: FnMut(Hash)> PointVisitor for HashVisitor<F> {
539    fn visit<T: Object>(&mut self, point: &Point<T>) {
540        self.0(*point.hash());
541    }
542}
543
544pub struct ReflessInput<'a> {
545    data: &'a [u8],
546}
547
548pub struct Input<'a> {
549    refless: ReflessInput<'a>,
550    resolve: &'a Arc<dyn Resolve>,
551    index: &'a Cell<usize>,
552}
553
554impl<'a> Deref for Input<'a> {
555    type Target = ReflessInput<'a>;
556
557    fn deref(&self) -> &Self::Target {
558        &self.refless
559    }
560}
561
562impl DerefMut for Input<'_> {
563    fn deref_mut(&mut self) -> &mut Self::Target {
564        &mut self.refless
565    }
566}
567
568impl ParseInput for ReflessInput<'_> {
569    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
570    where
571        Self: 'a,
572    {
573        match self.data.split_first_chunk() {
574            Some((chunk, data)) => {
575                self.data = data;
576                Ok(chunk)
577            }
578            None => Err(Error::EndOfInput),
579        }
580    }
581
582    fn parse_n<'a>(&mut self, n: usize) -> crate::Result<&'a [u8]>
583    where
584        Self: 'a,
585    {
586        match self.data.split_at_checked(n) {
587            Some((chunk, data)) => {
588                self.data = data;
589                Ok(chunk)
590            }
591            None => Err(Error::EndOfInput),
592        }
593    }
594
595    fn parse_ahead<T: Parse<Self>>(&mut self, n: usize) -> crate::Result<T> {
596        let input = ReflessInput {
597            data: self.parse_n(n)?,
598        };
599        T::parse(input)
600    }
601
602    fn parse_compare<T: ParseInline<Self>>(&mut self, n: usize, c: &[u8]) -> Result<Option<T>> {
603        match self.data.split_at_checked(n) {
604            Some((chunk, data)) => {
605                if chunk == c {
606                    self.data = data;
607                    Ok(None)
608                } else {
609                    self.parse_inline().map(Some)
610                }
611            }
612            None => Err(Error::EndOfInput),
613        }
614    }
615
616    fn parse_all<'a>(self) -> &'a [u8]
617    where
618        Self: 'a,
619    {
620        self.data
621    }
622
623    fn empty(self) -> crate::Result<()> {
624        if self.data.is_empty() {
625            Ok(())
626        } else {
627            Err(Error::ExtraInputLeft)
628        }
629    }
630
631    fn non_empty(self) -> Option<Self> {
632        if self.data.is_empty() {
633            None
634        } else {
635            Some(self)
636        }
637    }
638}
639
640impl ParseInput for Input<'_> {
641    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
642    where
643        Self: 'a,
644    {
645        (**self).parse_chunk()
646    }
647
648    fn parse_n<'a>(&mut self, n: usize) -> crate::Result<&'a [u8]>
649    where
650        Self: 'a,
651    {
652        (**self).parse_n(n)
653    }
654
655    fn parse_ahead<T: Parse<Self>>(&mut self, n: usize) -> crate::Result<T> {
656        let input = Input {
657            refless: ReflessInput {
658                data: self.parse_n(n)?,
659            },
660            resolve: self.resolve,
661            index: self.index,
662        };
663        T::parse(input)
664    }
665
666    fn parse_compare<T: ParseInline<Self>>(&mut self, n: usize, c: &[u8]) -> Result<Option<T>> {
667        match self.data.split_at_checked(n) {
668            Some((chunk, data)) => {
669                if chunk == c {
670                    self.data = data;
671                    Ok(None)
672                } else {
673                    self.parse_inline().map(Some)
674                }
675            }
676            None => Err(Error::EndOfInput),
677        }
678    }
679
680    fn parse_all<'a>(self) -> &'a [u8]
681    where
682        Self: 'a,
683    {
684        self.refless.parse_all()
685    }
686
687    fn empty(self) -> crate::Result<()> {
688        self.refless.empty()
689    }
690
691    fn non_empty(mut self) -> Option<Self> {
692        self.refless = self.refless.non_empty()?;
693        Some(self)
694    }
695}
696
697impl Input<'_> {
698    fn parse_address(&mut self) -> crate::Result<Address> {
699        let hash = *self.parse_chunk()?;
700        let index = self.index.get();
701        self.index.set(index + 1);
702        Ok(Address { hash, index })
703    }
704
705    fn parse_point<T: Object>(&mut self) -> crate::Result<Point<T>> {
706        let address = self.parse_address()?;
707        Ok(Point::from_address(address, self.resolve.clone()))
708    }
709
710    fn parse_raw_point_inner(&mut self) -> crate::Result<RawPointInner> {
711        let address = self.parse_address()?;
712        Ok(RawPointInner::from_address(address, self.resolve.clone()))
713    }
714
715    pub fn extension<T: Any>(&self) -> crate::Result<&T> {
716        self.resolve
717            .extension(TypeId::of::<T>())?
718            .downcast_ref()
719            .ok_or(Error::ExtensionType)
720    }
721
722    pub fn resolve(&self) -> Arc<dyn Resolve> {
723        self.resolve.clone()
724    }
725}
726
727pub trait ToOutput {
728    fn to_output(&self, output: &mut dyn Output);
729
730    fn data_hash(&self) -> Hash {
731        let mut output = HashOutput::default();
732        self.to_output(&mut output);
733        output.hash()
734    }
735}
736
737pub trait ToOutputExt: ToOutput {
738    fn output<T: Output + Default>(&self) -> T {
739        let mut output = T::default();
740        self.to_output(&mut output);
741        output
742    }
743}
744
745impl<T: ?Sized + ToOutput> ToOutputExt for T {}
746
747pub trait Topological {
748    fn accept_points(&self, visitor: &mut impl PointVisitor) {
749        let _ = visitor;
750    }
751
752    fn topology_hash(&self) -> Hash {
753        let mut hasher = Sha256::new();
754        self.accept_points(&mut HashVisitor(|hash| hasher.update(hash)));
755        hasher.finalize().into()
756    }
757
758    fn topology(&self) -> TopoVec {
759        let mut topology = TopoVec::new();
760        self.accept_points(&mut topology);
761        topology
762    }
763}
764
765pub trait Tagged {
766    const TAGS: Tags = Tags(&[], &[]);
767
768    const HASH: Hash = const { Self::TAGS.const_hash(sha2_const::Sha256::new()).finalize() };
769}
770
771pub trait ParseSlice: for<'a> Parse<Input<'a>> {
772    fn parse_slice(data: &[u8], resolve: &Arc<dyn Resolve>) -> crate::Result<Self> {
773        let input = Input {
774            refless: ReflessInput { data },
775            resolve,
776            index: &Cell::new(0),
777        };
778        let object = Self::parse(input)?;
779        Ok(object)
780    }
781}
782
783impl<T: for<'a> Parse<Input<'a>>> ParseSlice for T {}
784
785pub trait Object:
786    'static + Sized + Send + Sync + ToOutput + Topological + Tagged + for<'a> Parse<Input<'a>>
787{
788    fn full_hash(&self) -> Hash {
789        let mut output = HashOutput::default();
790        output.hasher.update(Self::HASH);
791        output.hasher.update(self.topology_hash());
792        output.hasher.update(self.data_hash());
793        output.hash()
794    }
795
796    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
797        let _ = typeid;
798        Err(Error::UnknownExtension)
799    }
800}
801
802pub struct Tags(pub &'static [&'static str], pub &'static [&'static Self]);
803
804impl Tags {
805    const fn const_hash(&self, mut hasher: sha2_const::Sha256) -> sha2_const::Sha256 {
806        {
807            let mut i = 0;
808            while i < self.0.len() {
809                hasher = hasher.update(self.0[i].as_bytes());
810                i += 1;
811            }
812        }
813        {
814            let mut i = 0;
815            while i < self.1.len() {
816                hasher = self.1[i].const_hash(hasher);
817                i += 1;
818            }
819        }
820        hasher
821    }
822}
823
824pub trait Inline: Object + for<'a> ParseInline<Input<'a>> {}
825
826impl<T: Object> Topological for Point<T> {
827    fn accept_points(&self, visitor: &mut impl PointVisitor) {
828        visitor.visit(self);
829    }
830}
831
832impl<T: Object> ParseInline<Input<'_>> for Point<T> {
833    fn parse_inline(input: &mut Input<'_>) -> crate::Result<Self> {
834        input.parse_point()
835    }
836}
837
838impl<T: Tagged> Tagged for Point<T> {
839    const TAGS: Tags = T::TAGS;
840}
841
842impl<T: Object> Object for Point<T> {}
843
844impl<T> ToOutput for Point<T> {
845    fn to_output(&self, output: &mut dyn Output) {
846        output.write(self.hash());
847    }
848}
849
850impl<T: Object> Inline for Point<T> {}
851
852pub trait Topology: Send + Sync {
853    fn len(&self) -> usize;
854    fn get(&self, index: usize) -> Option<&Arc<dyn Singular>>;
855
856    fn is_empty(&self) -> bool {
857        self.len() == 0
858    }
859}
860
861pub trait Singular: Send + Sync + FetchBytes {
862    fn hash(&self) -> &Hash;
863}
864
865pub type TopoVec = Vec<Arc<dyn Singular>>;
866
867impl PointVisitor for TopoVec {
868    fn visit<T: Object>(&mut self, point: &Point<T>) {
869        self.push(Arc::new(point.clone()));
870    }
871}
872
873impl<T> FetchBytes for Point<T> {
874    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
875        self.origin.fetch_bytes()
876    }
877
878    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
879        self.origin.extension(typeid)
880    }
881}
882
883impl<T> Singular for Point<T> {
884    fn hash(&self) -> &Hash {
885        self.hash.unwrap()
886    }
887}
888
889impl Topology for TopoVec {
890    fn len(&self) -> usize {
891        self.len()
892    }
893
894    fn get(&self, index: usize) -> Option<&Arc<dyn Singular>> {
895        (**self).get(index)
896    }
897}
898
899pub trait ParseSliceRefless: for<'a> Parse<ReflessInput<'a>> {
900    fn parse_slice_refless(data: &[u8]) -> crate::Result<Self> {
901        let input = ReflessInput { data };
902        let object = Self::parse(input)?;
903        Ok(object)
904    }
905}
906
907impl<T: for<'a> Parse<ReflessInput<'a>>> ParseSliceRefless for T {}
908
909pub trait ReflessObject:
910    'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>
911{
912}
913
914pub trait ReflessInline: ReflessObject + for<'a> ParseInline<ReflessInput<'a>> {
915    fn parse_as_inline(mut input: ReflessInput) -> crate::Result<Self> {
916        let object = Self::parse_inline(&mut input)?;
917        input.empty()?;
918        Ok(object)
919    }
920}
921
922#[derive(Debug, Clone, Copy)]
923pub struct Refless<T>(pub T);
924
925impl<T> Deref for Refless<T> {
926    type Target = T;
927
928    fn deref(&self) -> &Self::Target {
929        &self.0
930    }
931}
932
933impl<T> DerefMut for Refless<T> {
934    fn deref_mut(&mut self) -> &mut Self::Target {
935        &mut self.0
936    }
937}
938
939impl<T: ToOutput> ToOutput for Refless<T> {
940    fn to_output(&self, output: &mut dyn Output) {
941        self.0.to_output(output);
942    }
943}
944
945impl<'a, T: Parse<ReflessInput<'a>>> Parse<Input<'a>> for Refless<T> {
946    fn parse(input: Input<'a>) -> crate::Result<Self> {
947        T::parse(input.refless).map(Self)
948    }
949}
950
951impl<'a, T: ParseInline<ReflessInput<'a>>> ParseInline<Input<'a>> for Refless<T> {
952    fn parse_inline(input: &mut Input<'a>) -> crate::Result<Self> {
953        T::parse_inline(input).map(Self)
954    }
955}
956
957impl<T: Tagged> Tagged for Refless<T> {
958    const TAGS: Tags = T::TAGS;
959}
960
961impl<T> Topological for Refless<T> {}
962impl<T: ReflessObject> Object for Refless<T> {}
963impl<T: ReflessInline> Inline for Refless<T> {}
964
965pub trait Output {
966    fn write(&mut self, data: &[u8]);
967}
968
969impl Output for Vec<u8> {
970    fn write(&mut self, data: &[u8]) {
971        self.extend_from_slice(data);
972    }
973}
974
975impl Output for Vec<&'static str> {
976    fn write(&mut self, data: &[u8]) {
977        let _ = data;
978    }
979}
980
981#[derive(Default)]
982struct HashOutput {
983    hasher: Sha256,
984    at: usize,
985}
986
987impl Output for HashOutput {
988    fn write(&mut self, data: &[u8]) {
989        self.hasher.update(data);
990        self.at += data.len();
991    }
992}
993
994impl HashOutput {
995    fn hash(self) -> Hash {
996        self.hasher.finalize().into()
997    }
998}
999
1000pub struct PointMut<'a, T: Object> {
1001    hash: &'a mut OptionalHash,
1002    origin: &'a mut dyn Fetch<T = T>,
1003}
1004
1005impl<T: Object> Deref for PointMut<'_, T> {
1006    type Target = T;
1007
1008    fn deref(&self) -> &Self::Target {
1009        self.origin.get().unwrap()
1010    }
1011}
1012
1013impl<T: Object> DerefMut for PointMut<'_, T> {
1014    fn deref_mut(&mut self) -> &mut Self::Target {
1015        self.origin.get_mut().unwrap()
1016    }
1017}
1018
1019impl<T: Object> Drop for PointMut<'_, T> {
1020    fn drop(&mut self) {
1021        self.origin.get_mut_finalize();
1022        self.hash.0 = self.full_hash();
1023    }
1024}
1025
1026impl<T: Object + Clone> Point<T> {
1027    pub fn from_object(object: T) -> Self {
1028        Self::from_origin(object.full_hash(), Arc::new(LocalOrigin(object)))
1029    }
1030
1031    fn yolo_mut(&mut self) -> bool {
1032        self.origin.get().is_some()
1033            && Arc::get_mut(&mut self.origin).is_some_and(|origin| origin.get_mut().is_some())
1034    }
1035
1036    pub async fn fetch_mut(&'_ mut self) -> crate::Result<PointMut<'_, T>> {
1037        if !self.yolo_mut() {
1038            let object = self.origin.fetch().await?;
1039            self.origin = Arc::new(LocalOrigin(object));
1040        }
1041        let origin = Arc::get_mut(&mut self.origin).unwrap();
1042        assert!(origin.get_mut().is_some());
1043        self.hash.clear();
1044        Ok(PointMut {
1045            hash: &mut self.hash,
1046            origin,
1047        })
1048    }
1049}
1050
1051struct LocalOrigin<T>(T);
1052
1053impl<T> Deref for LocalOrigin<T> {
1054    type Target = T;
1055
1056    fn deref(&self) -> &Self::Target {
1057        &self.0
1058    }
1059}
1060
1061impl<T> DerefMut for LocalOrigin<T> {
1062    fn deref_mut(&mut self) -> &mut Self::Target {
1063        &mut self.0
1064    }
1065}
1066
1067impl<T: Object + Clone> Fetch for LocalOrigin<T> {
1068    type T = T;
1069
1070    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)> {
1071        let extension = self.0.clone();
1072        Box::pin(ready(Ok((
1073            self.0.clone(),
1074            Arc::new(ByTopology {
1075                topology: self.0.topology(),
1076                extension: Box::new(extension),
1077            }) as _,
1078        ))))
1079    }
1080
1081    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1082        Box::pin(ready(Ok(self.0.clone())))
1083    }
1084
1085    fn get(&self) -> Option<&Self::T> {
1086        Some(self)
1087    }
1088
1089    fn get_mut(&mut self) -> Option<&mut Self::T> {
1090        Some(self)
1091    }
1092}
1093
1094impl<T: Object + Clone> FetchBytes for LocalOrigin<T> {
1095    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
1096        let extension = self.0.clone();
1097        Box::pin(ready(Ok((
1098            self.0.output(),
1099            Arc::new(ByTopology {
1100                topology: self.0.topology(),
1101                extension: Box::new(extension),
1102            }) as _,
1103        ))))
1104    }
1105
1106    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
1107        self.0.extension(typeid)
1108    }
1109}
1110
1111trait AsExtension {
1112    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any>;
1113}
1114
1115impl<T: Object> AsExtension for T {
1116    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
1117        self.extension(typeid)
1118    }
1119}
1120
1121struct ByTopology {
1122    topology: TopoVec,
1123    extension: Box<dyn Send + Sync + AsExtension>,
1124}
1125
1126impl ByTopology {
1127    fn try_resolve(&'_ self, address: Address) -> Result<FailFuture<'_, ByteNode>> {
1128        let point = self
1129            .topology
1130            .get(address.index)
1131            .ok_or(Error::AddressOutOfBounds)?;
1132        if *point.hash() != address.hash {
1133            Err(Error::ResolutionMismatch)
1134        } else {
1135            Ok(point.fetch_bytes())
1136        }
1137    }
1138}
1139
1140impl Resolve for ByTopology {
1141    fn resolve(&'_ self, address: Address) -> FailFuture<'_, ByteNode> {
1142        self.try_resolve(address)
1143            .map_err(Err)
1144            .map_err(ready)
1145            .map_err(Box::pin)
1146            .unwrap_or_else(|x| x)
1147    }
1148
1149    fn resolve_extension(&self, address: Address, typeid: TypeId) -> crate::Result<&dyn Any> {
1150        self.topology
1151            .get(address.index)
1152            .ok_or(Error::AddressOutOfBounds)?
1153            .extension(typeid)
1154    }
1155
1156    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
1157        self.extension.extension(typeid)
1158    }
1159
1160    fn name(&self) -> &str {
1161        "by topology"
1162    }
1163}
1164
1165impl<T: Object> Fetch for Point<T> {
1166    type T = T;
1167
1168    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)> {
1169        self.origin.fetch_full()
1170    }
1171
1172    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1173        self.origin.fetch()
1174    }
1175
1176    fn get(&self) -> Option<&Self::T> {
1177        self.origin.get()
1178    }
1179
1180    fn get_mut(&mut self) -> Option<&mut Self::T> {
1181        self.hash.clear();
1182        Arc::get_mut(&mut self.origin)?.get_mut()
1183    }
1184
1185    fn get_mut_finalize(&mut self) {
1186        let origin = Arc::get_mut(&mut self.origin).unwrap();
1187        origin.get_mut_finalize();
1188        self.hash.0 = origin.get().unwrap().full_hash();
1189    }
1190}
1191
1192pub trait Size {
1193    const SIZE: usize = <Self::Size as Unsigned>::USIZE;
1194    type Size: Unsigned;
1195}
1196
1197pub trait SizeExt: Size<Size: ArrayLength> + ToOutput {
1198    fn to_array(&self) -> GenericArray<u8, Self::Size> {
1199        let mut array = GenericArray::default();
1200        let mut output = ArrayOutput {
1201            data: &mut array,
1202            offset: 0,
1203        };
1204        self.to_output(&mut output);
1205        output.finalize();
1206        array
1207    }
1208}
1209
1210impl<T: Size<Size: ArrayLength> + ToOutput> SizeExt for T {}
1211
1212struct ArrayOutput<'a> {
1213    data: &'a mut [u8],
1214    offset: usize,
1215}
1216
1217impl ArrayOutput<'_> {
1218    fn finalize(self) {
1219        assert_eq!(self.offset, self.data.len());
1220    }
1221}
1222
1223impl Output for ArrayOutput<'_> {
1224    fn write(&mut self, data: &[u8]) {
1225        self.data[self.offset..][..data.len()].copy_from_slice(data);
1226        self.offset += data.len();
1227    }
1228}
1229
1230trait RainbowIterator: Sized + IntoIterator {
1231    fn iter_to_output(self, output: &mut dyn Output)
1232    where
1233        Self::Item: ToOutput,
1234    {
1235        self.into_iter().for_each(|item| item.to_output(output));
1236    }
1237
1238    fn iter_accept_points(self, visitor: &mut impl PointVisitor)
1239    where
1240        Self::Item: Topological,
1241    {
1242        self.into_iter()
1243            .for_each(|item| item.accept_points(visitor));
1244    }
1245}
1246
1247pub trait ParseInput: Sized {
1248    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
1249    where
1250        Self: 'a;
1251    fn parse_n<'a>(&mut self, n: usize) -> crate::Result<&'a [u8]>
1252    where
1253        Self: 'a;
1254    fn parse_ahead<T: Parse<Self>>(&mut self, n: usize) -> crate::Result<T>;
1255    fn parse_compare<T: ParseInline<Self>>(&mut self, n: usize, c: &[u8]) -> Result<Option<T>>;
1256    fn parse_all<'a>(self) -> &'a [u8]
1257    where
1258        Self: 'a;
1259    fn empty(self) -> crate::Result<()>;
1260    fn non_empty(self) -> Option<Self>;
1261
1262    fn consume(self, f: impl FnMut(&mut Self) -> crate::Result<()>) -> crate::Result<()> {
1263        self.collect(f)
1264    }
1265
1266    fn parse_collect<T: ParseInline<Self>, B: FromIterator<T>>(self) -> crate::Result<B> {
1267        self.collect(|input| input.parse_inline())
1268    }
1269
1270    fn collect<T, B: FromIterator<T>>(self, f: impl FnMut(&mut Self) -> T) -> B {
1271        self.iter(f).collect()
1272    }
1273
1274    fn iter<T>(self, mut f: impl FnMut(&mut Self) -> T) -> impl Iterator<Item = T> {
1275        let mut state = Some(self);
1276        std::iter::from_fn(move || {
1277            let mut input = state.take()?.non_empty()?;
1278            let item = f(&mut input);
1279            state = Some(input);
1280            Some(item)
1281        })
1282    }
1283
1284    fn parse_inline<T: ParseInline<Self>>(&mut self) -> crate::Result<T> {
1285        T::parse_inline(self)
1286    }
1287
1288    fn parse<T: Parse<Self>>(self) -> crate::Result<T> {
1289        T::parse(self)
1290    }
1291}
1292
1293impl<T: Sized + IntoIterator> RainbowIterator for T {}
1294
1295pub trait Parse<I: ParseInput>: Sized {
1296    fn parse(input: I) -> crate::Result<Self>;
1297}
1298
1299pub trait ParseInline<I: ParseInput>: Parse<I> {
1300    fn parse_inline(input: &mut I) -> crate::Result<Self>;
1301    fn parse_as_inline(mut input: I) -> crate::Result<Self> {
1302        let object = Self::parse_inline(&mut input)?;
1303        input.empty()?;
1304        Ok(object)
1305    }
1306}
1307
1308pub trait Equivalent<T>: Sized {
1309    fn into_equivalent(self) -> T;
1310    fn from_equivalent(object: T) -> Self;
1311}
1312
1313impl<U: 'static + Equivalent<T>, T: 'static> Equivalent<Point<T>> for Point<U> {
1314    fn into_equivalent(self) -> Point<T> {
1315        Point {
1316            hash: self.hash,
1317            origin: Arc::new(MapEquivalent {
1318                origin: self.origin,
1319                map: U::into_equivalent,
1320            }),
1321        }
1322    }
1323
1324    fn from_equivalent(object: Point<T>) -> Self {
1325        Point {
1326            hash: object.hash,
1327            origin: Arc::new(MapEquivalent {
1328                origin: object.origin,
1329                map: U::from_equivalent,
1330            }),
1331        }
1332    }
1333}
1334
1335struct MapEquivalent<T, F> {
1336    origin: Arc<dyn Fetch<T = T>>,
1337    map: F,
1338}
1339
1340impl<T, F> FetchBytes for MapEquivalent<T, F> {
1341    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
1342        self.origin.fetch_bytes()
1343    }
1344
1345    fn extension(&self, typeid: TypeId) -> crate::Result<&dyn Any> {
1346        self.origin.extension(typeid)
1347    }
1348}
1349
1350trait Map1<T>: Fn(T) -> Self::U {
1351    type U;
1352}
1353
1354impl<T, U, F: Fn(T) -> U> Map1<T> for F {
1355    type U = U;
1356}
1357
1358impl<T, F: 'static + Send + Sync + Map1<T>> Fetch for MapEquivalent<T, F> {
1359    type T = F::U;
1360
1361    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)> {
1362        Box::pin(self.origin.fetch_full().map_ok(|(x, r)| ((self.map)(x), r)))
1363    }
1364
1365    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1366        Box::pin(self.origin.fetch().map_ok(&self.map))
1367    }
1368}
1369
1370impl<T: 'static + ToOutput> Point<T> {
1371    pub fn map<U>(self, f: impl 'static + Send + Sync + Fn(T) -> U) -> Point<U> {
1372        Point {
1373            hash: self.hash,
1374            origin: Arc::new(Map {
1375                origin: self.origin,
1376                map: f,
1377            }),
1378        }
1379    }
1380}
1381
1382struct Map<T, F> {
1383    origin: Arc<dyn Fetch<T = T>>,
1384    map: F,
1385}
1386
1387impl<T: ToOutput, F> FetchBytes for Map<T, F> {
1388    fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
1389        Box::pin(self.origin.fetch_full().map_ok(|(x, r)| (x.output(), r)))
1390    }
1391}
1392
1393impl<T: ToOutput, F: 'static + Send + Sync + Map1<T>> Fetch for Map<T, F> {
1394    type T = F::U;
1395
1396    fn fetch_full(&'_ self) -> FailFuture<'_, (Self::T, Arc<dyn Resolve>)> {
1397        Box::pin(self.origin.fetch_full().map_ok(|(x, r)| ((self.map)(x), r)))
1398    }
1399
1400    fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
1401        Box::pin(self.origin.fetch().map_ok(&self.map))
1402    }
1403}
1404
1405impl<T> MaybeHasNiche for Point<T> {
1406    type MnArray = SomeNiche<ZeroNiche<<Self as Size>::Size>>;
1407}
1408
1409#[test]
1410fn options() {
1411    type T0 = bool;
1412    type T1 = Option<T0>;
1413    type T2 = Option<T1>;
1414    type T3 = Option<T2>;
1415    type T4 = Option<T3>;
1416    type T5 = Option<T4>;
1417    assert_eq!(T0::SIZE, 1);
1418    assert_eq!(T1::SIZE, 1);
1419    assert_eq!(T2::SIZE, 2);
1420    assert_eq!(T3::SIZE, 2);
1421    assert_eq!(T4::SIZE, 3);
1422    assert_eq!(T5::SIZE, 3);
1423    assert_eq!(false.output::<Vec<u8>>(), [0]);
1424    assert_eq!(true.output::<Vec<u8>>(), [1]);
1425    assert_eq!(Some(false).output::<Vec<u8>>(), [0]);
1426    assert_eq!(Some(true).output::<Vec<u8>>(), [1]);
1427    assert_eq!(None::<bool>.output::<Vec<u8>>(), [2]);
1428    assert_eq!(Some(Some(false)).output::<Vec<u8>>(), [0, 0]);
1429    assert_eq!(Some(Some(true)).output::<Vec<u8>>(), [0, 1]);
1430    assert_eq!(Some(None::<bool>).output::<Vec<u8>>(), [0, 2]);
1431    assert_eq!(None::<Option<bool>>.output::<Vec<u8>>(), [1, 0]);
1432    assert_eq!(Some(Some(Some(false))).output::<Vec<u8>>(), [0, 0]);
1433    assert_eq!(Some(Some(Some(true))).output::<Vec<u8>>(), [0, 1]);
1434    assert_eq!(Some(Some(None::<bool>)).output::<Vec<u8>>(), [0, 2]);
1435    assert_eq!(Some(None::<Option<bool>>).output::<Vec<u8>>(), [1, 0]);
1436    assert_eq!(None::<Option<Option<bool>>>.output::<Vec<u8>>(), [2, 0]);
1437    assert_eq!(Option::<Point<()>>::SIZE, HASH_SIZE);
1438    assert_eq!(Some(()).output::<Vec<u8>>(), [0]);
1439    assert_eq!(Some(((), ())).output::<Vec<u8>>(), [0]);
1440    assert_eq!(Some(((), true)).output::<Vec<u8>>(), [1]);
1441    assert_eq!(Some((true, true)).output::<Vec<u8>>(), [1, 1]);
1442    assert_eq!(Some((Some(true), true)).output::<Vec<u8>>(), [1, 1]);
1443    assert_eq!(Some((None::<bool>, true)).output::<Vec<u8>>(), [2, 1]);
1444    assert_eq!(Some((true, None::<bool>)).output::<Vec<u8>>(), [1, 2]);
1445    assert_eq!(None::<(Option<bool>, bool)>.output::<Vec<u8>>(), [0, 2]);
1446    assert_eq!(None::<(bool, Option<bool>)>.output::<Vec<u8>>(), [2, 0]);
1447    assert_eq!(
1448        Some(Some((Some(true), Some(true)))).output::<Vec<u8>>(),
1449        [0, 1, 1],
1450    );
1451}