Skip to main content

object_rainbow/
lib.rs

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