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: Resolve {
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 Resolve for TopoVec {
850    fn resolve<'a>(
851        &'a self,
852        address: Address,
853        _: &'a Arc<dyn Resolve>,
854    ) -> FailFuture<'a, ByteNode> {
855        Box::pin(async move {
856            let singular = self.get(address.index).ok_or(Error::AddressOutOfBounds)?;
857            if singular.hash() != address.hash {
858                Err(Error::FullHashMismatch)
859            } else {
860                singular.fetch_bytes().await
861            }
862        })
863    }
864
865    fn resolve_data(&'_ self, address: Address) -> FailFuture<'_, Vec<u8>> {
866        Box::pin(async move {
867            let singular = self.get(address.index).ok_or(Error::AddressOutOfBounds)?;
868            if singular.hash() != address.hash {
869                Err(Error::FullHashMismatch)
870            } else {
871                singular.fetch_data().await
872            }
873        })
874    }
875
876    fn try_resolve_local(
877        &self,
878        address: Address,
879        _: &Arc<dyn Resolve>,
880    ) -> Result<Option<ByteNode>> {
881        let singular = self.get(address.index).ok_or(Error::AddressOutOfBounds)?;
882        if singular.hash() != address.hash {
883            Err(Error::FullHashMismatch)
884        } else {
885            singular.fetch_bytes_local()
886        }
887    }
888
889    fn topology_hash(&self) -> Option<Hash> {
890        Some(self.data_hash())
891    }
892
893    fn into_topovec(self: Arc<Self>) -> Option<TopoVec> {
894        Some((*self).clone())
895    }
896}
897
898impl Topology for TopoVec {
899    fn len(&self) -> usize {
900        self.len()
901    }
902
903    fn get(&self, index: usize) -> Option<&Arc<dyn Singular>> {
904        (**self).get(index)
905    }
906}
907
908pub trait ParseSliceRefless: for<'a> Parse<ReflessInput<'a>> {
909    fn parse_slice_refless(data: &[u8]) -> crate::Result<Self> {
910        let input = ReflessInput { data: Some(data) };
911        let object = Self::parse(input)?;
912        Ok(object)
913    }
914}
915
916impl<T: for<'a> Parse<ReflessInput<'a>>> ParseSliceRefless for T {}
917
918pub trait ReflessObject:
919    'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>
920{
921}
922
923impl<T: 'static + Sized + Send + Sync + ToOutput + Tagged + for<'a> Parse<ReflessInput<'a>>>
924    ReflessObject for T
925{
926}
927
928pub trait ReflessInline:
929    ReflessObject + InlineOutput + for<'a> ParseInline<ReflessInput<'a>>
930{
931}
932
933impl<T: ReflessObject + InlineOutput + for<'a> ParseInline<ReflessInput<'a>>> ReflessInline for T {}
934
935pub trait Output {
936    fn write(&mut self, data: &[u8]);
937    fn is_mangling(&self) -> bool {
938        false
939    }
940    fn is_real(&self) -> bool {
941        !self.is_mangling()
942    }
943}
944
945impl Output for Vec<u8> {
946    fn write(&mut self, data: &[u8]) {
947        self.extend_from_slice(data);
948    }
949}
950
951struct MangleOutput<'a, T: ?Sized>(&'a mut T);
952
953impl<'a, T: Output> MangleOutput<'a, T> {
954    fn new(output: &'a mut T) -> Self {
955        assert!(output.is_real());
956        assert!(!output.is_mangling());
957        Self(output)
958    }
959}
960
961impl<T: ?Sized + Output> Output for MangleOutput<'_, T> {
962    fn write(&mut self, data: &[u8]) {
963        self.0.write(data);
964    }
965
966    fn is_mangling(&self) -> bool {
967        true
968    }
969}
970
971pub struct Mangled<T: ?Sized>(T);
972
973impl<T: ?Sized + ToOutput> ToOutput for Mangled<T> {
974    fn to_output(&self, output: &mut impl Output) {
975        self.0.to_output(&mut MangleOutput::new(output));
976    }
977}
978
979#[derive(Default)]
980struct HashOutput {
981    hasher: Sha256,
982    at: usize,
983}
984
985impl Output for HashOutput {
986    fn write(&mut self, data: &[u8]) {
987        self.hasher.update(data);
988        self.at += data.len();
989    }
990}
991
992impl HashOutput {
993    fn hash(self) -> Hash {
994        Hash::from_sha256(self.hasher.finalize().into())
995    }
996}
997
998struct ByTopology {
999    topology: TopoVec,
1000    topology_hash: Hash,
1001}
1002
1003impl Drop for ByTopology {
1004    fn drop(&mut self) {
1005        while let Some(singular) = self.topology.pop() {
1006            if let Some(resolve) = singular.try_unwrap_resolve()
1007                && let Some(topology) = &mut resolve.into_topovec()
1008            {
1009                self.topology.append(topology);
1010            }
1011        }
1012    }
1013}
1014
1015impl ByTopology {
1016    fn try_resolve(&'_ self, address: Address) -> Result<FailFuture<'_, ByteNode>> {
1017        let point = self
1018            .topology
1019            .get(address.index)
1020            .ok_or(Error::AddressOutOfBounds)?;
1021        if point.hash() != address.hash {
1022            Err(Error::ResolutionMismatch)
1023        } else {
1024            Ok(point.fetch_bytes())
1025        }
1026    }
1027
1028    fn try_resolve_data(&'_ self, address: Address) -> Result<FailFuture<'_, Vec<u8>>> {
1029        let point = self
1030            .topology
1031            .get(address.index)
1032            .ok_or(Error::AddressOutOfBounds)?;
1033        if point.hash() != address.hash {
1034            Err(Error::ResolutionMismatch)
1035        } else {
1036            Ok(point.fetch_data())
1037        }
1038    }
1039}
1040
1041impl Resolve for ByTopology {
1042    fn resolve(&'_ self, address: Address, _: &Arc<dyn Resolve>) -> FailFuture<'_, ByteNode> {
1043        self.try_resolve(address)
1044            .map_err(Err)
1045            .map_err(ready)
1046            .map_err(Box::pin)
1047            .unwrap_or_else(|x| x)
1048    }
1049
1050    fn resolve_data(&'_ self, address: Address) -> FailFuture<'_, Vec<u8>> {
1051        self.try_resolve_data(address)
1052            .map_err(Err)
1053            .map_err(ready)
1054            .map_err(Box::pin)
1055            .unwrap_or_else(|x| x)
1056    }
1057
1058    fn try_resolve_local(
1059        &self,
1060        address: Address,
1061        _: &Arc<dyn Resolve>,
1062    ) -> Result<Option<ByteNode>> {
1063        let point = self
1064            .topology
1065            .get(address.index)
1066            .ok_or(Error::AddressOutOfBounds)?;
1067        if point.hash() != address.hash {
1068            Err(Error::ResolutionMismatch)
1069        } else {
1070            point.fetch_bytes_local()
1071        }
1072    }
1073
1074    fn topology_hash(&self) -> Option<Hash> {
1075        Some(self.topology_hash)
1076    }
1077
1078    fn into_topovec(self: Arc<Self>) -> Option<TopoVec> {
1079        Arc::try_unwrap(self)
1080            .ok()
1081            .as_mut()
1082            .map(|Self { topology, .. }| std::mem::take(topology))
1083    }
1084}
1085
1086pub trait Size {
1087    const SIZE: usize = <Self::Size as Unsigned>::USIZE;
1088    type Size: Unsigned;
1089}
1090
1091pub trait SizeExt: Size<Size: ArrayLength> + ToOutput {
1092    fn to_array(&self) -> GenericArray<u8, Self::Size> {
1093        let mut array = GenericArray::default();
1094        let mut output = ArrayOutput {
1095            data: &mut array,
1096            offset: 0,
1097        };
1098        self.to_output(&mut output);
1099        output.finalize();
1100        array
1101    }
1102
1103    fn reinterpret<T: FromSized<Size = Self::Size>>(&self) -> T {
1104        T::from_sized(&self.to_array())
1105    }
1106}
1107
1108impl<T: Size<Size: ArrayLength> + ToOutput> SizeExt for T {}
1109
1110struct ArrayOutput<'a> {
1111    data: &'a mut [u8],
1112    offset: usize,
1113}
1114
1115impl ArrayOutput<'_> {
1116    fn finalize(self) {
1117        assert_eq!(self.offset, self.data.len());
1118    }
1119}
1120
1121impl Output for ArrayOutput<'_> {
1122    fn write(&mut self, data: &[u8]) {
1123        self.data[self.offset..][..data.len()].copy_from_slice(data);
1124        self.offset += data.len();
1125    }
1126}
1127
1128pub trait FromSized: Size<Size: ArrayLength> {
1129    fn from_sized(data: &GenericArray<u8, Self::Size>) -> Self;
1130}
1131
1132impl<
1133    A: FromSized<Size = An>,
1134    B: FromSized<Size = Bn>,
1135    An,
1136    Bn: Add<An, Output: ArrayLength + Sub<An, Output = Bn>>,
1137> FromSized for (A, B)
1138{
1139    fn from_sized(data: &GenericArray<u8, Self::Size>) -> Self {
1140        let (a, b) = data.split();
1141        (A::from_sized(a), B::from_sized(b))
1142    }
1143}
1144
1145pub trait RainbowIterator: Sized + IntoIterator {
1146    fn iter_to_output(self, output: &mut impl Output)
1147    where
1148        Self::Item: InlineOutput,
1149    {
1150        self.into_iter().for_each(|item| item.to_output(output));
1151    }
1152
1153    fn iter_list_hashes(self, f: &mut impl FnMut(Hash))
1154    where
1155        Self::Item: ListHashes,
1156    {
1157        self.into_iter().for_each(|item| item.list_hashes(f));
1158    }
1159
1160    fn iter_traverse(self, visitor: &mut impl PointVisitor)
1161    where
1162        Self::Item: Topological,
1163    {
1164        self.into_iter().for_each(|item| item.traverse(visitor));
1165    }
1166}
1167
1168pub trait ParseInput: Sized {
1169    type Data: AsRef<[u8]> + Deref<Target = [u8]> + Into<Vec<u8>> + Copy;
1170    fn parse_chunk<'a, const N: usize>(&mut self) -> crate::Result<&'a [u8; N]>
1171    where
1172        Self: 'a;
1173    fn parse_n(&mut self, n: usize) -> crate::Result<Self::Data>;
1174    fn parse_until_zero(&mut self) -> crate::Result<Self::Data>;
1175    fn parse_n_compare(&mut self, n: usize, c: &[u8]) -> crate::Result<Option<Self::Data>> {
1176        let data = self.parse_n(n)?;
1177        if *data == *c {
1178            Ok(None)
1179        } else {
1180            Ok(Some(data))
1181        }
1182    }
1183    fn reparse<T: Parse<Self>>(&mut self, data: Self::Data) -> crate::Result<T>;
1184    fn parse_ahead<T: Parse<Self>>(&mut self, n: usize) -> crate::Result<T> {
1185        let data = self.parse_n(n)?;
1186        self.reparse(data)
1187    }
1188    fn parse_zero_terminated<T: Parse<Self>>(&mut self) -> crate::Result<T> {
1189        let data = self.parse_until_zero()?;
1190        self.reparse(data)
1191    }
1192    fn parse_compare<T: Parse<Self>>(&mut self, n: usize, c: &[u8]) -> Result<Option<T>> {
1193        self.parse_n_compare(n, c)?
1194            .map(|data| self.reparse(data))
1195            .transpose()
1196    }
1197    fn parse_all(self) -> crate::Result<Self::Data>;
1198    fn empty(self) -> crate::Result<()>;
1199    fn non_empty(self) -> crate::Result<Option<Self>>;
1200
1201    fn consume(self, f: impl FnMut(&mut Self) -> crate::Result<()>) -> crate::Result<()> {
1202        self.collect(f)
1203    }
1204
1205    fn parse_collect<T: ParseInline<Self>, B: FromIterator<T>>(self) -> crate::Result<B> {
1206        self.collect(|input| input.parse_inline())
1207    }
1208
1209    fn collect<T, B: FromIterator<T>>(
1210        self,
1211        f: impl FnMut(&mut Self) -> crate::Result<T>,
1212    ) -> crate::Result<B> {
1213        self.iter(f).collect()
1214    }
1215
1216    fn iter<T>(
1217        self,
1218        mut f: impl FnMut(&mut Self) -> crate::Result<T>,
1219    ) -> impl Iterator<Item = crate::Result<T>> {
1220        let mut state = Some(self);
1221        std::iter::from_fn(move || {
1222            let mut input = match state.take()?.non_empty() {
1223                Ok(input) => input?,
1224                Err(e) => return Some(Err(e)),
1225            };
1226            let item = f(&mut input);
1227            state = Some(input);
1228            Some(item)
1229        })
1230    }
1231
1232    fn parse_inline<T: ParseInline<Self>>(&mut self) -> crate::Result<T> {
1233        T::parse_inline(self)
1234    }
1235
1236    fn parse<T: Parse<Self>>(self) -> crate::Result<T> {
1237        T::parse(self)
1238    }
1239
1240    fn parse_vec<T: ParseInline<Self>>(self) -> crate::Result<Vec<T>> {
1241        T::parse_vec(self)
1242    }
1243
1244    fn parse_vec_n<T: ParseInline<Self>>(&mut self, n: usize) -> crate::Result<Vec<T>> {
1245        T::parse_vec_n(self, n)
1246    }
1247
1248    fn parse_array<T: ParseInline<Self>, const N: usize>(&mut self) -> crate::Result<[T; N]> {
1249        T::parse_array(self)
1250    }
1251
1252    fn parse_generic_array<T: ParseInline<Self>, N: ArrayLength>(
1253        &mut self,
1254    ) -> crate::Result<GenericArray<T, N>> {
1255        T::parse_generic_array(self)
1256    }
1257}
1258
1259pub trait PointInput: ParseInput {
1260    type Extra: 'static + Clone;
1261    type WithExtra<E: 'static + Clone>: PointInput<Extra = E, WithExtra<Self::Extra> = Self>;
1262    fn next_index(&mut self) -> usize;
1263    fn resolve_arc_ref(&self) -> &Arc<dyn Resolve>;
1264    fn resolve(&self) -> Arc<dyn Resolve> {
1265        self.resolve_arc_ref().clone()
1266    }
1267    fn resolve_ref(&self) -> &dyn Resolve {
1268        self.resolve_arc_ref().as_ref()
1269    }
1270    /// Get [`Self::Extra`].
1271    fn extra(&self) -> &Self::Extra;
1272    /// Project the `Extra`. Under some circumstances, prevents an extra [`Clone::clone`].
1273    fn map_extra<E: 'static + Clone>(
1274        self,
1275        f: impl FnOnce(&Self::Extra) -> &E,
1276    ) -> Self::WithExtra<E>;
1277    /// Return the old [`Self::Extra`], give a new [`PointInput`] with `E` as `Extra`.
1278    fn replace_extra<E: 'static + Clone>(self, extra: E) -> (Self::Extra, Self::WithExtra<E>);
1279    /// [`Self::replace_extra`] but discarding [`Self::Extra`].
1280    fn with_extra<E: 'static + Clone>(self, extra: E) -> Self::WithExtra<E> {
1281        self.replace_extra(extra).1
1282    }
1283    /// [`ParseInput::parse`] with a different `Extra`.
1284    fn parse_extra<E: 'static + Clone, T: Parse<Self::WithExtra<E>>>(
1285        self,
1286        extra: E,
1287    ) -> crate::Result<T> {
1288        self.with_extra(extra).parse()
1289    }
1290    /// [`ParseInput::parse_inline`] with a different `Extra`.
1291    fn parse_inline_extra<E: 'static + Clone, T: ParseInline<Self::WithExtra<E>>>(
1292        &mut self,
1293        extra: E,
1294    ) -> crate::Result<T>;
1295}
1296
1297impl<T: Sized + IntoIterator> RainbowIterator for T {}
1298
1299/// This can be parsed by consuming the whole rest of the input.
1300///
1301/// Nothing can be parsed after this. It's implementation's responsibility to ensure there are no
1302/// leftover bytes.
1303pub trait Parse<I: ParseInput>: Sized {
1304    /// Parse consuming the whole stream.
1305    fn parse(input: I) -> crate::Result<Self>;
1306}
1307
1308/// This can be parsed from an input, after which we can correctly parse something else.
1309///
1310/// When parsed as the last object, makes sure there are no bytes left in the input (fails if there
1311/// are).
1312pub trait ParseInline<I: ParseInput>: Parse<I> {
1313    /// Parse without consuming the whole stream. Errors on unexpected EOF.
1314    fn parse_inline(input: &mut I) -> crate::Result<Self>;
1315    /// For implementing [`Parse::parse`].
1316    fn parse_as_inline(mut input: I) -> crate::Result<Self> {
1317        let object = Self::parse_inline(&mut input)?;
1318        input.empty()?;
1319        Ok(object)
1320    }
1321    /// Parse a `Vec` of `Self`. Customisable for optimisations.
1322    fn parse_vec(input: I) -> crate::Result<Vec<Self>> {
1323        input.parse_collect()
1324    }
1325    /// Parse a `Vec` of `Self` of length `n`. Customisable for optimisations.
1326    fn parse_vec_n(input: &mut I, n: usize) -> crate::Result<Vec<Self>> {
1327        (0..n).map(|_| input.parse_inline()).collect()
1328    }
1329    /// Parse an array of `Self`. Customisable for optimisations.
1330    fn parse_array<const N: usize>(input: &mut I) -> crate::Result<[Self; N]> {
1331        let mut scratch = std::array::from_fn(|_| None);
1332        for item in scratch.iter_mut() {
1333            *item = Some(input.parse_inline()?);
1334        }
1335        Ok(scratch.map(Option::unwrap))
1336    }
1337    /// Parse a [`GenericArray`] of `Self`. Customisable for optimisations.
1338    fn parse_generic_array<N: ArrayLength>(input: &mut I) -> crate::Result<GenericArray<Self, N>> {
1339        let mut scratch = GenericArray::default();
1340        for item in scratch.iter_mut() {
1341            *item = Some(input.parse_inline()?);
1342        }
1343        Ok(scratch.map(Option::unwrap))
1344    }
1345}
1346
1347/// Implemented if both types have the exact same layout.
1348/// This implies having the same [`MaybeHasNiche::MnArray`].
1349///
1350/// This is represented as two-way conversion for two reasons:
1351/// - to highlight that the conversion is actual equivalence
1352/// - to increase flexibility (mostly to go around the orphan rule)
1353pub trait Equivalent<T>: Sized {
1354    /// Inverse of [`Equivalent::from_equivalent`].
1355    fn into_equivalent(self) -> T;
1356    /// Inverse of [`Equivalent::into_equivalent`].
1357    fn from_equivalent(object: T) -> Self;
1358}
1359
1360/// This `Extra` can be used to parse `T` via [`ParseSliceExtra::parse_slice_extra`].
1361pub trait ExtraFor<T> {
1362    /// [`ParseSliceExtra::parse_slice_extra`].
1363    fn parse(&self, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T>;
1364
1365    /// [`Self::parse`], then check that [`FullHash::full_hash`] matches.
1366    fn parse_checked(&self, hash: Hash, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T>
1367    where
1368        T: FullHash,
1369    {
1370        let object = self.parse(data, resolve)?;
1371        if object.full_hash() != hash {
1372            Err(Error::FullHashMismatch)
1373        } else {
1374            Ok(object)
1375        }
1376    }
1377}
1378
1379impl<T: for<'a> Parse<Input<'a, Extra>>, Extra: Clone> ExtraFor<T> for Extra {
1380    fn parse(&self, data: &[u8], resolve: &Arc<dyn Resolve>) -> Result<T> {
1381        T::parse_slice_extra(data, resolve, self)
1382    }
1383}
1384
1385impl<T> ToOutput for dyn Send + Sync + ExtraFor<T> {
1386    fn to_output(&self, _: &mut impl Output) {}
1387}
1388
1389impl<T: Tagged> Tagged for dyn Send + Sync + ExtraFor<T> {
1390    const TAGS: Tags = T::TAGS;
1391    const HASH: Hash = T::HASH;
1392}
1393
1394impl<T> Size for dyn Send + Sync + ExtraFor<T> {
1395    type Size = typenum::U0;
1396    const SIZE: usize = 0;
1397}
1398
1399impl<T> InlineOutput for dyn Send + Sync + ExtraFor<T> {}
1400impl<T> ListHashes for dyn Send + Sync + ExtraFor<T> {}
1401impl<T> Topological for dyn Send + Sync + ExtraFor<T> {}
1402
1403impl<T, I: PointInput<Extra: Send + Sync + ExtraFor<T>>> Parse<I>
1404    for Arc<dyn Send + Sync + ExtraFor<T>>
1405{
1406    fn parse(input: I) -> crate::Result<Self> {
1407        Self::parse_as_inline(input)
1408    }
1409}
1410
1411impl<T, I: PointInput<Extra: Send + Sync + ExtraFor<T>>> ParseInline<I>
1412    for Arc<dyn Send + Sync + ExtraFor<T>>
1413{
1414    fn parse_inline(input: &mut I) -> crate::Result<Self> {
1415        Ok(Arc::new(input.extra().clone()))
1416    }
1417}
1418
1419impl<T> MaybeHasNiche for dyn Send + Sync + ExtraFor<T> {
1420    type MnArray = NoNiche<ZeroNoNiche<<Self as Size>::Size>>;
1421}
1422
1423assert_impl!(
1424    impl<T, E> Inline<E> for Arc<dyn Send + Sync + ExtraFor<T>>
1425    where
1426        T: Object<E>,
1427        E: 'static + Send + Sync + Clone + ExtraFor<T>,
1428    {
1429    }
1430);
1431
1432#[doc(hidden)]
1433pub trait BoundPair: Sized {
1434    type T;
1435    type E;
1436}
1437
1438impl<T, E> BoundPair for (T, E) {
1439    type T = T;
1440    type E = E;
1441}
1442
1443#[test]
1444fn options() {
1445    type T0 = ();
1446    type T1 = Option<T0>;
1447    type T2 = Option<T1>;
1448    type T3 = Option<T2>;
1449    type T4 = Option<T3>;
1450    type T5 = Option<T4>;
1451    assert_eq!(T0::SIZE, 0);
1452    assert_eq!(T1::SIZE, 1);
1453    assert_eq!(T2::SIZE, 1);
1454    assert_eq!(T3::SIZE, 1);
1455    assert_eq!(T4::SIZE, 1);
1456    assert_eq!(T5::SIZE, 1);
1457    assert_eq!(Some(Some(Some(()))).vec(), [0]);
1458    assert_eq!(Some(Some(None::<()>)).vec(), [1]);
1459    assert_eq!(Some(None::<Option<()>>).vec(), [2]);
1460    assert_eq!(None::<Option<Option<()>>>.vec(), [3]);
1461
1462    assert_eq!(false.vec(), [0]);
1463    assert_eq!(true.vec(), [1]);
1464    assert_eq!(Some(false).vec(), [0]);
1465    assert_eq!(Some(true).vec(), [1]);
1466    assert_eq!(None::<bool>.vec(), [2]);
1467    assert_eq!(Some(Some(false)).vec(), [0]);
1468    assert_eq!(Some(Some(true)).vec(), [1]);
1469    assert_eq!(Some(None::<bool>).vec(), [2]);
1470    assert_eq!(None::<Option<bool>>.vec(), [3]);
1471    assert_eq!(Some(Some(Some(false))).vec(), [0]);
1472    assert_eq!(Some(Some(Some(true))).vec(), [1]);
1473    assert_eq!(Some(Some(None::<bool>)).vec(), [2]);
1474    assert_eq!(Some(None::<Option<bool>>).vec(), [3]);
1475    assert_eq!(None::<Option<Option<bool>>>.vec(), [4]);
1476    assert_eq!(Option::<Hash>::SIZE, HASH_SIZE);
1477    assert_eq!(Some(()).vec(), [0]);
1478    assert_eq!(Some(((), ())).vec(), [0]);
1479    assert_eq!(Some(((), true)).vec(), [1]);
1480    assert_eq!(Some((true, true)).vec(), [1, 1]);
1481    assert_eq!(Some((Some(true), true)).vec(), [1, 1]);
1482    assert_eq!(Some((None::<bool>, true)).vec(), [2, 1]);
1483    assert_eq!(Some((true, None::<bool>)).vec(), [1, 2]);
1484    assert_eq!(None::<(Option<bool>, bool)>.vec(), [3, 2]);
1485    assert_eq!(None::<(bool, Option<bool>)>.vec(), [2, 3]);
1486    assert_eq!(Some(Some((Some(true), Some(true)))).vec(), [1, 1],);
1487    assert_eq!(Option::<Hash>::SIZE, HASH_SIZE);
1488    assert_eq!(Option::<Option<Hash>>::SIZE, HASH_SIZE);
1489    assert_eq!(Option::<Option<Option<Hash>>>::SIZE, HASH_SIZE);
1490}