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