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