Skip to main content

object_rainbow/
lib.rs

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